1 /* tc-i960.c - All the i80960-specific stuff
2 Copyright 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2005
4 Free Software Foundation, Inc.
6 This file is part of GAS.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23 /* See comment on md_parse_option for 80960-specific invocation options. */
25 /* There are 4 different lengths of (potentially) symbol-based displacements
26 in the 80960 instruction set, each of which could require address fix-ups
27 and (in the case of external symbols) emission of relocation directives:
30 This is a standard length for the base assembler and requires no
34 This is a non-standard length, but the base assembler has a
35 hook for bit field address fixups: the fixS structure can
36 point to a descriptor of the field, in which case our
37 md_number_to_field() routine gets called to process it.
39 I made the hook a little cleaner by having fix_new() (in the base
40 assembler) return a pointer to the fixS in question. And I made it a
41 little simpler by storing the field size (in this case 13) instead of
42 of a pointer to another structure: 80960 displacements are ALWAYS
43 stored in the low-order bits of a 4-byte word.
45 Since the target of a COBR cannot be external, no relocation
46 directives for this size displacement have to be generated.
47 But the base assembler had to be modified to issue error
48 messages if the symbol did turn out to be external.
51 Fixups are handled as for the 13-bit case (except that 24 is stored
54 The relocation directive generated is the same as that for the 32-bit
55 displacement, except that it's PC-relative (the 32-bit displacement
56 never is). The i80960 version of the linker needs a mod to
57 distinguish and handle the 24-bit case.
60 MEMA formats are always promoted to MEMB (32-bit) if the displacement
61 is based on a symbol, because it could be relocated at link time.
62 The only time we use the 12-bit format is if an absolute value of
63 less than 4096 is specified, in which case we need neither a fixup nor
64 a relocation directive. */
70 #include "safe-ctype.h"
73 #include "opcode/i960.h"
75 #if defined (OBJ_AOUT) || defined (OBJ_BOUT)
77 #define TC_S_IS_SYSPROC(s) ((1 <= S_GET_OTHER (s)) && (S_GET_OTHER (s) <= 32))
78 #define TC_S_IS_BALNAME(s) (S_GET_OTHER (s) == N_BALNAME)
79 #define TC_S_IS_CALLNAME(s) (S_GET_OTHER (s) == N_CALLNAME)
80 #define TC_S_IS_BADPROC(s) ((S_GET_OTHER (s) != 0) && !TC_S_IS_CALLNAME (s) && !TC_S_IS_BALNAME (s) && !TC_S_IS_SYSPROC (s))
82 #define TC_S_SET_SYSPROC(s, p) (S_SET_OTHER ((s), (p) + 1))
83 #define TC_S_GET_SYSPROC(s) (S_GET_OTHER (s) - 1)
85 #define TC_S_FORCE_TO_BALNAME(s) (S_SET_OTHER ((s), N_BALNAME))
86 #define TC_S_FORCE_TO_CALLNAME(s) (S_SET_OTHER ((s), N_CALLNAME))
87 #define TC_S_FORCE_TO_SYSPROC(s) {;}
89 #else /* ! OBJ_A/BOUT */
92 #define TC_S_IS_SYSPROC(s) (S_GET_STORAGE_CLASS (s) == C_SCALL)
93 #define TC_S_IS_BALNAME(s) (SF_GET_BALNAME (s))
94 #define TC_S_IS_CALLNAME(s) (SF_GET_CALLNAME (s))
95 #define TC_S_IS_BADPROC(s) (TC_S_IS_SYSPROC (s) && TC_S_GET_SYSPROC (s) < 0 && 31 < TC_S_GET_SYSPROC (s))
97 #define TC_S_SET_SYSPROC(s, p) ((s)->sy_symbol.ost_auxent[1].x_sc.x_stindx = (p))
98 #define TC_S_GET_SYSPROC(s) ((s)->sy_symbol.ost_auxent[1].x_sc.x_stindx)
100 #define TC_S_FORCE_TO_BALNAME(s) (SF_SET_BALNAME (s))
101 #define TC_S_FORCE_TO_CALLNAME(s) (SF_SET_CALLNAME (s))
102 #define TC_S_FORCE_TO_SYSPROC(s) (S_SET_STORAGE_CLASS ((s), C_SCALL))
104 #else /* ! OBJ_COFF */
106 #define TC_S_IS_SYSPROC(s) 0
108 #define TC_S_IS_BALNAME(s) 0
109 #define TC_S_IS_CALLNAME(s) 0
110 #define TC_S_IS_BADPROC(s) 0
112 #define TC_S_SET_SYSPROC(s, p)
113 #define TC_S_GET_SYSPROC(s) 0
115 #define TC_S_FORCE_TO_BALNAME(s)
116 #define TC_S_FORCE_TO_CALLNAME(s)
117 #define TC_S_FORCE_TO_SYSPROC(s)
119 #error COFF, a.out, b.out, and ELF are the only supported formats.
120 #endif /* ! OBJ_ELF */
121 #endif /* ! OBJ_COFF */
122 #endif /* ! OBJ_A/BOUT */
124 extern char *input_line_pointer
;
126 /* Local i80960 routines. */
130 /* See md_parse_option() for meanings of these options. */
131 static char norelax
; /* True if -norelax switch seen. */
132 static char instrument_branches
; /* True if -b switch seen. */
134 /* Characters that always start a comment.
135 If the pre-processor is disabled, these aren't very useful. */
136 const char comment_chars
[] = "#";
138 /* Characters that only start a comment at the beginning of
139 a line. If the line seems to have the form '# 123 filename'
140 .line and .file directives will appear in the pre-processed output.
142 Note that input_file.c hand checks for '#' at the beginning of the
143 first line of the input file. This is because the compiler outputs
144 #NO_APP at the beginning of its output. */
146 /* Also note that comments started like this one will always work. */
148 const char line_comment_chars
[] = "#";
149 const char line_separator_chars
[] = ";";
151 /* Chars that can be used to separate mant from exp in floating point nums. */
152 const char EXP_CHARS
[] = "eE";
154 /* Chars that mean this number is a floating point constant,
155 as in 0f12.456 or 0d1.2345e12. */
156 const char FLT_CHARS
[] = "fFdDtT";
158 /* Table used by base assembler to relax addresses based on varying length
159 instructions. The fields are:
160 1) most positive reach of this state,
161 2) most negative reach of this state,
162 3) how many bytes this mode will add to the size of the current frag
163 4) which index into the table to try if we can't fit into this one.
165 For i80960, the only application is the (de-)optimization of cobr
166 instructions into separate compare and branch instructions when a 13-bit
167 displacement won't hack it. */
168 const relax_typeS md_relax_table
[] =
170 {0, 0, 0, 0}, /* State 0 => no more relaxation possible. */
171 {4088, -4096, 0, 2}, /* State 1: conditional branch (cobr). */
172 {0x800000 - 8, -0x800000, 4, 0}, /* State 2: compare (reg) & branch (ctrl). */
175 /* These are the machine dependent pseudo-ops.
177 This table describes all the machine specific pseudo-ops the assembler
178 has to support. The fields are:
179 pseudo-op name without dot
180 function to call to execute this pseudo-op
181 integer arg to pass to the function. */
185 /* Macros to extract info from an 'expressionS' structure 'e'. */
186 #define adds(e) e.X_add_symbol
187 #define offs(e) e.X_add_number
189 /* Branch-prediction bits for CTRL/COBR format opcodes. */
190 #define BP_MASK 0x00000002 /* Mask for branch-prediction bit. */
191 #define BP_TAKEN 0x00000000 /* Value to OR in to predict branch. */
192 #define BP_NOT_TAKEN 0x00000002 /* Value to OR in to predict no branch. */
194 /* Some instruction opcodes that we need explicitly. */
195 #define BE 0x12000000
196 #define BG 0x11000000
197 #define BGE 0x13000000
198 #define BL 0x14000000
199 #define BLE 0x16000000
200 #define BNE 0x15000000
201 #define BNO 0x10000000
202 #define BO 0x17000000
203 #define CHKBIT 0x5a002700
204 #define CMPI 0x5a002080
205 #define CMPO 0x5a002000
208 #define BAL 0x0b000000
209 #define CALL 0x09000000
210 #define CALLS 0x66003800
211 #define RET 0x0a000000
213 /* These masks are used to build up a set of MEMB mode bits. */
216 #define MEMB_BIT 0x1000
219 /* Mask for the only mode bit in a MEMA instruction (if set, abase reg is
221 #define MEMA_ABASE 0x2000
223 /* Info from which a MEMA or MEMB format instruction can be generated. */
226 /* (First) 32 bits of instruction. */
228 /* 0-(none), 12- or, 32-bit displacement needed. */
230 /* The expression in the source instruction from which the
231 displacement should be determined. */
236 /* The two pieces of info we need to generate a register operand. */
239 int mode
; /* 0 =>local/global/spec reg; 1=> literal or fp reg. */
240 int special
; /* 0 =>not a sfr; 1=> is a sfr (not valid w/mode=0). */
241 int n
; /* Register number or literal value. */
244 /* Number and assembler mnemonic for all registers that can appear in
286 /* Numbers for special-function registers are for assembler internal
287 use only: they are scaled back to range [0-31] for binary output. */
323 /* Numbers for floating point registers are for assembler internal
324 use only: they are scaled back to [0-3] for binary output. */
332 { NULL
, 0 }, /* END OF LIST */
335 #define IS_RG_REG(n) ((0 <= (n)) && ((n) < SF0))
336 #define IS_SF_REG(n) ((SF0 <= (n)) && ((n) < FP0))
337 #define IS_FP_REG(n) ((n) >= FP0)
339 /* Number and assembler mnemonic for all registers that can appear as
340 'abase' (indirect addressing) registers. */
382 /* For assembler internal use only: this number never appears in binary
386 { NULL
, 0 }, /* END OF LIST */
390 static struct hash_control
*op_hash
; /* Opcode mnemonics. */
391 static struct hash_control
*reg_hash
; /* Register name hash table. */
392 static struct hash_control
*areg_hash
; /* Abase register hash table. */
394 /* Architecture for which we are assembling. */
395 #define ARCH_ANY 0 /* Default: no architecture checking done. */
402 int architecture
= ARCH_ANY
; /* Architecture requested on invocation line. */
403 int iclasses_seen
; /* OR of instruction classes (I_* constants)
404 for which we've actually assembled
407 /* BRANCH-PREDICTION INSTRUMENTATION
409 The following supports generation of branch-prediction instrumentation
410 (turned on by -b switch). The instrumentation collects counts
411 of branches taken/not-taken for later input to a utility that will
412 set the branch prediction bits of the instructions in accordance with
413 the behavior observed. (Note that the KX series does not have
416 The instrumentation consists of:
418 (1) before and after each conditional branch, a call to an external
419 routine that increments and steps over an inline counter. The
420 counter itself, initialized to 0, immediately follows the call
421 instruction. For each branch, the counter following the branch
422 is the number of times the branch was not taken, and the difference
423 between the counters is the number of times it was taken. An
424 example of an instrumented conditional branch:
432 (2) a table of pointers to the instrumented branches, so that an
433 external postprocessing routine can locate all of the counters.
434 the table begins with a 2-word header: a pointer to the next in
435 a linked list of such tables (initialized to 0); and a count
436 of the number of entries in the table (exclusive of the header.
438 Note that input source code is expected to already contain calls
439 an external routine that will link the branch local table into a
440 list of such tables. */
442 /* Number of branches instrumented so far. Also used to generate
443 unique local labels for each instrumented branch. */
446 #define BR_LABEL_BASE "LBRANCH"
447 /* Basename of local labels on instrumented branches, to avoid
448 conflict with compiler- generated local labels. */
450 #define BR_CNT_FUNC "__inc_branch"
451 /* Name of the external routine that will increment (and step over) an
454 #define BR_TAB_NAME "__BRANCH_TABLE__"
455 /* Name of the table of pointers to branches. A local (i.e.,
456 non-external) symbol. */
458 static void ctrl_fmt (char *, long, int);
464 int i
; /* Loop counter. */
465 const struct i960_opcode
*oP
; /* Pointer into opcode table. */
466 const char *retval
; /* Value returned by hash functions. */
468 op_hash
= hash_new ();
469 reg_hash
= hash_new ();
470 areg_hash
= hash_new ();
472 /* For some reason, the base assembler uses an empty string for "no
473 error message", instead of a NULL pointer. */
476 for (oP
= i960_opcodes
; oP
->name
&& !retval
; oP
++)
477 retval
= hash_insert (op_hash
, oP
->name
, (void *) oP
);
479 for (i
= 0; regnames
[i
].reg_name
&& !retval
; i
++)
480 retval
= hash_insert (reg_hash
, regnames
[i
].reg_name
,
481 (char *) ®names
[i
].reg_num
);
483 for (i
= 0; aregs
[i
].areg_name
&& !retval
; i
++)
484 retval
= hash_insert (areg_hash
, aregs
[i
].areg_name
,
485 (char *) &aregs
[i
].areg_num
);
488 as_fatal (_("Hashing returned \"%s\"."), retval
);
491 /* parse_expr: parse an expression
493 Use base assembler's expression parser to parse an expression.
494 It, unfortunately, runs off a global which we have to save/restore
495 in order to make it work for us.
497 An empty expression string is treated as an absolute 0.
499 Sets O_illegal regardless of expression evaluation if entire input
500 string is not consumed in the evaluation -- tolerate no dangling junk! */
503 parse_expr (char *textP
, /* Text of expression to be parsed. */
504 expressionS
*expP
) /* Where to put the results of parsing. */
506 char *save_in
; /* Save global here. */
513 /* Treat empty string as absolute 0. */
514 expP
->X_add_symbol
= expP
->X_op_symbol
= NULL
;
515 expP
->X_add_number
= 0;
516 expP
->X_op
= O_constant
;
520 save_in
= input_line_pointer
; /* Save global. */
521 input_line_pointer
= textP
; /* Make parser work for us. */
523 (void) expression (expP
);
524 if ((size_t) (input_line_pointer
- textP
) != strlen (textP
))
525 /* Did not consume all of the input. */
526 expP
->X_op
= O_illegal
;
528 symP
= expP
->X_add_symbol
;
529 if (symP
&& (hash_find (reg_hash
, S_GET_NAME (symP
))))
530 /* Register name in an expression. */
531 /* FIXME: this isn't much of a check any more. */
532 expP
->X_op
= O_illegal
;
534 input_line_pointer
= save_in
; /* Restore global. */
538 /* emit: output instruction binary
540 Output instruction binary, in target byte order, 4 bytes at a time.
541 Return pointer to where it was placed. */
544 emit (long instr
) /* Word to be output, host byte order. */
546 char *toP
; /* Where to output it. */
548 toP
= frag_more (4); /* Allocate storage. */
549 md_number_to_chars (toP
, instr
, 4); /* Convert to target byte order. */
553 /* get_cdisp: handle displacement for a COBR or CTRL instruction.
555 Parse displacement for a COBR or CTRL instruction.
557 If successful, output the instruction opcode and set up for it,
558 depending on the arg 'var_frag', either:
559 o an address fixup to be done when all symbol values are known, or
560 o a varying length code fragment, with address fixup info. This
561 will be done for cobr instructions that may have to be relaxed
562 in to compare/branch instructions (8 bytes) if the final
563 address displacement is greater than 13 bits. */
566 get_cdisp (char *dispP
, /* Displacement as specified in source instruction. */
567 char *ifmtP
, /* "COBR" or "CTRL" (for use in error message). */
568 long instr
, /* Instruction needing the displacement. */
569 int numbits
, /* # bits of displacement (13 for COBR, 24 for CTRL). */
570 int var_frag
,/* 1 if varying length code fragment should be emitted;
571 0 if an address fix should be emitted. */
572 int callj
) /* 1 if callj relocation should be done; else 0. */
574 expressionS e
; /* Parsed expression. */
575 fixS
*fixP
; /* Structure describing needed address fix. */
576 char *outP
; /* Where instruction binary is output to. */
580 parse_expr (dispP
, &e
);
584 as_bad (_("expression syntax error"));
587 if (S_GET_SEGMENT (e
.X_add_symbol
) == now_seg
588 || S_GET_SEGMENT (e
.X_add_symbol
) == undefined_section
)
592 outP
= frag_more (8); /* Allocate worst-case storage. */
593 md_number_to_chars (outP
, instr
, 4);
594 frag_variant (rs_machine_dependent
, 4, 4, 1,
595 adds (e
), offs (e
), outP
);
599 /* Set up a new fix structure, so address can be updated
600 when all symbol values are known. */
602 fixP
= fix_new (frag_now
,
603 outP
- frag_now
->fr_literal
,
610 fixP
->fx_tcbit
= callj
;
612 /* We want to modify a bit field when the address is
613 known. But we don't need all the garbage in the
614 bit_fix structure. So we're going to lie and store
615 the number of bits affected instead of a pointer. */
616 fixP
->fx_bit_fixP
= (bit_fixS
*) (size_t) numbits
;
620 as_bad (_("attempt to branch into different segment"));
624 as_bad (_("target of %s instruction must be a label"), ifmtP
);
630 md_chars_to_number (char * val
, /* Value in target byte order. */
631 int n
) /* Number of bytes in the input. */
635 for (retval
= 0; n
--;)
638 retval
|= (unsigned char) val
[n
];
643 /* mema_to_memb: convert a MEMA-format opcode to a MEMB-format opcode.
645 There are 2 possible MEMA formats:
647 - displacement + abase
649 They are distinguished by the setting of the MEMA_ABASE bit. */
652 mema_to_memb (char * opcodeP
) /* Where to find the opcode, in target byte order. */
654 long opcode
; /* Opcode in host byte order. */
655 long mode
; /* Mode bits for MEMB instruction. */
657 opcode
= md_chars_to_number (opcodeP
, 4);
658 know (!(opcode
& MEMB_BIT
));
660 mode
= MEMB_BIT
| D_BIT
;
661 if (opcode
& MEMA_ABASE
)
664 opcode
&= 0xffffc000; /* Clear MEMA offset and mode bits. */
665 opcode
|= mode
; /* Set MEMB mode bits. */
667 md_number_to_chars (opcodeP
, opcode
, 4);
672 Return TRUE iff the target architecture supports the specified
673 special-function register (sfr). */
676 targ_has_sfr (int n
) /* Number (0-31) of sfr. */
678 switch (architecture
)
686 return ((0 <= n
) && (n
<= 4));
689 return ((0 <= n
) && (n
<= 2));
693 /* Look up a (suspected) register name in the register table and return the
694 associated register number (or -1 if not found). */
697 get_regnum (char *regname
) /* Suspected register name. */
701 rP
= (int *) hash_find (reg_hash
, regname
);
702 return (rP
== NULL
) ? -1 : *rP
;
705 /* syntax: Issue a syntax error. */
710 as_bad (_("syntax error"));
713 /* parse_regop: parse a register operand.
715 In case of illegal operand, issue a message and return some valid
716 information so instruction processing can continue. */
719 parse_regop (struct regop
*regopP
, /* Where to put description of register operand. */
720 char *optext
, /* Text of operand. */
721 char opdesc
) /* Descriptor byte: what's legal for this operand. */
723 int n
; /* Register number. */
724 expressionS e
; /* Parsed expression. */
726 /* See if operand is a register. */
727 n
= get_regnum (optext
);
732 /* Global or local register. */
733 if (!REG_ALIGN (opdesc
, n
))
734 as_bad (_("unaligned register"));
741 else if (IS_FP_REG (n
) && FP_OK (opdesc
))
743 /* Floating point register, and it's allowed. */
749 else if (IS_SF_REG (n
) && SFR_OK (opdesc
))
751 /* Special-function register, and it's allowed. */
755 if (!targ_has_sfr (regopP
->n
))
756 as_bad (_("no such sfr in this architecture"));
761 else if (LIT_OK (opdesc
))
763 /* How about a literal? */
768 /* Floating point literal acceptable. */
769 /* Skip over 0f, 0d, or 0e prefix. */
770 if ((optext
[0] == '0')
771 && (optext
[1] >= 'd')
772 && (optext
[1] <= 'f'))
775 if (!strcmp (optext
, "0.0") || !strcmp (optext
, "0"))
781 if (!strcmp (optext
, "1.0") || !strcmp (optext
, "1"))
789 /* Fixed point literal acceptable. */
790 parse_expr (optext
, &e
);
791 if (e
.X_op
!= O_constant
792 || (offs (e
) < 0) || (offs (e
) > 31))
794 as_bad (_("illegal literal"));
797 regopP
->n
= offs (e
);
802 /* Nothing worked. */
804 regopP
->mode
= 0; /* Register r0 is always a good one. */
809 /* get_ispec: parse a memory operand for an index specification
811 Here, an "index specification" is taken to be anything surrounded
812 by square brackets and NOT followed by anything else.
814 If it's found, detach it from the input string, remove the surrounding
815 square brackets, and return a pointer to it. Otherwise, return NULL. */
818 get_ispec (char *textP
) /* Pointer to memory operand from source instruction, no white space. */
821 /* Points to start of index specification. */
823 /* Points to end of index specification. */
826 /* Find opening square bracket, if any. */
827 start
= strchr (textP
, '[');
831 /* Eliminate '[', detach from rest of operand. */
834 end
= strchr (start
, ']');
837 as_bad (_("unmatched '['"));
840 /* Eliminate ']' and make sure it was the last thing
843 if (*(end
+ 1) != '\0')
844 as_bad (_("garbage after index spec ignored"));
850 /* parse_memop: parse a memory operand
852 This routine is based on the observation that the 4 mode bits of the
853 MEMB format, taken individually, have fairly consistent meaning:
855 M3 (bit 13): 1 if displacement is present (D_BIT)
856 M2 (bit 12): 1 for MEMB instructions (MEMB_BIT)
857 M1 (bit 11): 1 if index is present (I_BIT)
858 M0 (bit 10): 1 if abase is present (A_BIT)
860 So we parse the memory operand and set bits in the mode as we find
861 things. Then at the end, if we go to MEMB format, we need only set
862 the MEMB bit (M2) and our mode is built for us.
864 Unfortunately, I said "fairly consistent". The exceptions:
867 0100 Would seem illegal, but means "abase-only".
869 0101 Would seem to mean "abase-only" -- it means IP-relative.
870 Must be converted to 0100.
872 0110 Would seem to mean "index-only", but is reserved.
873 We turn on the D bit and provide a 0 displacement.
875 The other thing to observe is that we parse from the right, peeling
876 things * off as we go: first any index spec, then any abase, then
880 parse_memop (memS
*memP
, /* Where to put the results. */
881 char *argP
, /* Text of the operand to be parsed. */
882 int optype
) /* MEM1, MEM2, MEM4, MEM8, MEM12, or MEM16. */
884 char *indexP
; /* Pointer to index specification with "[]" removed. */
885 char *p
; /* Temp char pointer. */
886 char iprel_flag
; /* True if this is an IP-relative operand. */
887 int regnum
; /* Register number. */
888 /* Scale factor: 1,2,4,8, or 16. Later converted to internal format
889 (0,1,2,3,4 respectively). */
891 int mode
; /* MEMB mode bits. */
892 int *intP
; /* Pointer to register number. */
894 /* The following table contains the default scale factors for each
895 type of memory instruction. It is accessed using (optype-MEM1)
896 as an index -- thus it assumes the 'optype' constants are
897 assigned consecutive values, in the order they appear in this
899 static const int def_scale
[] =
905 -1, /* MEM12 -- no valid default */
909 iprel_flag
= mode
= 0;
911 /* Any index present? */
912 indexP
= get_ispec (argP
);
915 p
= strchr (indexP
, '*');
918 /* No explicit scale -- use default for this instruction
919 type and assembler mode. */
923 /* GNU960 compatibility */
924 scale
= def_scale
[optype
- MEM1
];
928 *p
++ = '\0'; /* Eliminate '*' */
930 /* Now indexP->a '\0'-terminated register name,
931 and p->a scale factor. */
933 if (!strcmp (p
, "16"))
935 else if (strchr ("1248", *p
) && (p
[1] == '\0'))
941 regnum
= get_regnum (indexP
); /* Get index reg. # */
942 if (!IS_RG_REG (regnum
))
944 as_bad (_("invalid index register"));
948 /* Convert scale to its binary encoding. */
967 as_bad (_("invalid scale factor"));
971 memP
->opcode
|= scale
| regnum
; /* Set index bits in opcode. */
972 mode
|= I_BIT
; /* Found a valid index spec. */
975 /* Any abase (Register Indirect) specification present? */
976 if ((p
= strrchr (argP
, '(')) != NULL
)
978 /* "(" is there -- does it start a legal abase spec? If not, it
979 could be part of a displacement expression. */
980 intP
= (int *) hash_find (areg_hash
, p
);
983 /* Got an abase here. */
985 *p
= '\0'; /* Discard register spec. */
987 /* We have to specialcase ip-rel mode. */
991 memP
->opcode
|= regnum
<< 14;
997 /* Any expression present? */
1002 /* Special-case ip-relative addressing. */
1009 memP
->opcode
|= 5 << 10; /* IP-relative mode. */
1015 /* Handle all other modes. */
1019 /* Go with MEMA instruction format for now (grow to MEMB later
1020 if 12 bits is not enough for the displacement). MEMA format
1021 has a single mode bit: set it to indicate that abase is
1023 memP
->opcode
|= MEMA_ABASE
;
1028 /* Go with MEMA instruction format for now (grow to MEMB later
1029 if 12 bits is not enough for the displacement). */
1034 /* For some reason, the bit string for this mode is not
1035 consistent: it should be 0 (exclusive of the MEMB bit), so we
1036 set it "by hand" here. */
1037 memP
->opcode
|= MEMB_BIT
;
1041 /* set MEMB bit in mode, and OR in mode bits. */
1042 memP
->opcode
|= mode
| MEMB_BIT
;
1046 /* Treat missing displacement as displacement of 0. */
1048 /* Fall into next case. */
1049 case D_BIT
| A_BIT
| I_BIT
:
1051 /* Set MEMB bit in mode, and OR in mode bits. */
1052 memP
->opcode
|= mode
| MEMB_BIT
;
1062 /* Generate a MEMA- or MEMB-format instruction. */
1065 mem_fmt (char *args
[], /* args[0]->opcode mnemonic, args[1-3]->operands. */
1066 struct i960_opcode
*oP
,/* Pointer to description of instruction. */
1067 int callx
) /* Is this a callx opcode. */
1069 int i
; /* Loop counter. */
1070 struct regop regop
; /* Description of register operand. */
1071 char opdesc
; /* Operand descriptor byte. */
1072 memS instr
; /* Description of binary to be output. */
1073 char *outP
; /* Where the binary was output to. */
1074 expressionS expr
; /* Parsed expression. */
1075 /* ->description of deferred address fixup. */
1079 /* COFF support isn't in place yet for callx relaxing. */
1083 memset (&instr
, '\0', sizeof (memS
));
1084 instr
.opcode
= oP
->opcode
;
1086 /* Process operands. */
1087 for (i
= 1; i
<= oP
->num_ops
; i
++)
1089 opdesc
= oP
->operand
[i
- 1];
1092 parse_memop (&instr
, args
[i
], oP
->format
);
1095 parse_regop (®op
, args
[i
], opdesc
);
1096 instr
.opcode
|= regop
.n
<< 19;
1100 /* Parse the displacement; this must be done before emitting the
1101 opcode, in case it is an expression using `.'. */
1102 parse_expr (instr
.e
, &expr
);
1104 /* Output opcode. */
1105 outP
= emit (instr
.opcode
);
1107 if (instr
.disp
== 0)
1110 /* Process the displacement. */
1114 as_bad (_("expression syntax error"));
1118 if (instr
.disp
== 32)
1119 (void) emit (offs (expr
)); /* Output displacement. */
1122 /* 12-bit displacement. */
1123 if (offs (expr
) & ~0xfff)
1125 /* Won't fit in 12 bits: convert already-output
1126 instruction to MEMB format, output
1128 mema_to_memb (outP
);
1129 (void) emit (offs (expr
));
1133 /* WILL fit in 12 bits: OR into opcode and
1134 overwrite the binary we already put out. */
1135 instr
.opcode
|= offs (expr
);
1136 md_number_to_chars (outP
, instr
.opcode
, 4);
1142 if (instr
.disp
== 12)
1143 /* Displacement is dependent on a symbol, whose value
1144 may change at link time. We HAVE to reserve 32 bits.
1145 Convert already-output opcode to MEMB format. */
1146 mema_to_memb (outP
);
1148 /* Output 0 displacement and set up address fixup for when
1149 this symbol's value becomes known. */
1150 outP
= emit ((long) 0);
1151 fixP
= fix_new_exp (frag_now
,
1152 outP
- frag_now
->fr_literal
,
1153 4, & expr
, 0, NO_RELOC
);
1154 /* Steve's linker relaxing hack. Mark this 32-bit relocation as
1155 being in the instruction stream, specifically as part of a callx
1157 fixP
->fx_bsr
= callx
;
1164 Return TRUE iff the target architecture supports the indicated
1165 class of instructions. */
1168 targ_has_iclass (int ic
) /* Instruction class; one of:
1169 I_BASE, I_CX, I_DEC, I_KX, I_FP, I_MIL, I_CASIM, I_CX2, I_HX, I_HX2. */
1171 iclasses_seen
|= ic
;
1173 switch (architecture
)
1176 return ic
& (I_BASE
| I_KX
);
1178 return ic
& (I_BASE
| I_KX
| I_FP
| I_DEC
);
1180 return ic
& (I_BASE
| I_KX
| I_FP
| I_DEC
| I_MIL
);
1182 return ic
& (I_BASE
| I_CX
| I_CX2
| I_CASIM
);
1184 return ic
& (I_BASE
| I_CX2
| I_JX
);
1186 return ic
& (I_BASE
| I_CX2
| I_JX
| I_HX
);
1188 if ((iclasses_seen
& (I_KX
| I_FP
| I_DEC
| I_MIL
))
1189 && (iclasses_seen
& (I_CX
| I_CX2
)))
1191 as_warn (_("architecture of opcode conflicts with that of earlier instruction(s)"));
1192 iclasses_seen
&= ~ic
;
1199 Determine if a "shlo" instruction can be used to implement a "ldconst".
1200 This means that some number X < 32 can be shifted left to produce the
1201 constant of interest.
1203 Return the shift count, or 0 if we can't do it.
1204 Caller calculates X by shifting original constant right 'shift' places. */
1207 shift_ok (int n
) /* The constant of interest. */
1209 int shift
; /* The shift count. */
1212 /* Can't do it for negative numbers. */
1215 /* Shift 'n' right until a 1 is about to be lost. */
1216 for (shift
= 0; (n
& 1) == 0; shift
++)
1226 Parse and replace a 'ldconst' pseudo-instruction with an appropriate
1229 Assumes the input consists of:
1230 arg[0] opcode mnemonic ('ldconst')
1231 arg[1] first operand (constant)
1232 arg[2] name of register to be loaded
1234 Replaces opcode and/or operands as appropriate.
1236 Returns the new number of arguments, or -1 on failure. */
1239 parse_ldconst (char *arg
[]) /* See above. */
1241 int n
; /* Constant to be loaded. */
1242 int shift
; /* Shift count for "shlo" instruction. */
1243 static char buf
[5]; /* Literal for first operand. */
1244 static char buf2
[5]; /* Literal for second operand. */
1245 expressionS e
; /* Parsed expression. */
1247 arg
[3] = NULL
; /* So we can tell at the end if it got used or not. */
1249 parse_expr (arg
[1], &e
);
1253 /* We're dependent on one or more symbols -- use "lda". */
1258 /* Try the following mappings:
1259 ldconst 0,<reg> -> mov 0,<reg>
1260 ldconst 31,<reg> -> mov 31,<reg>
1261 ldconst 32,<reg> -> addo 1,31,<reg>
1262 ldconst 62,<reg> -> addo 31,31,<reg>
1263 ldconst 64,<reg> -> shlo 8,3,<reg>
1264 ldconst -1,<reg> -> subo 1,0,<reg>
1265 ldconst -31,<reg> -> subo 31,0,<reg>
1267 Anything else becomes:
1270 if ((0 <= n
) && (n
<= 31))
1272 else if ((-31 <= n
) && (n
<= -1))
1276 sprintf (buf
, "%d", -n
);
1280 else if ((32 <= n
) && (n
<= 62))
1285 sprintf (buf
, "%d", n
- 31);
1288 else if ((shift
= shift_ok (n
)) != 0)
1292 sprintf (buf
, "%d", shift
);
1294 sprintf (buf2
, "%d", n
>> shift
);
1302 as_bad (_("invalid constant"));
1306 return (arg
[3] == 0) ? 2 : 3;
1309 /* reg_fmt: generate a REG-format instruction. */
1312 reg_fmt (char *args
[], /* args[0]->opcode mnemonic, args[1-3]->operands. */
1313 struct i960_opcode
*oP
)/* Pointer to description of instruction. */
1315 long instr
; /* Binary to be output. */
1316 struct regop regop
; /* Description of register operand. */
1317 int n_ops
; /* Number of operands. */
1320 n_ops
= oP
->num_ops
;
1324 parse_regop (®op
, args
[1], oP
->operand
[0]);
1326 if ((n_ops
== 1) && !(instr
& M3
))
1328 /* 1-operand instruction in which the dst field should
1329 be used (instead of src1). */
1332 regop
.mode
= regop
.special
;
1338 /* regop.n goes in bit 0, needs no shifting. */
1340 regop
.special
<<= 5;
1342 instr
|= regop
.n
| regop
.mode
| regop
.special
;
1347 parse_regop (®op
, args
[2], oP
->operand
[1]);
1349 if ((n_ops
== 2) && !(instr
& M3
))
1351 /* 2-operand instruction in which the dst field should
1352 be used instead of src2). */
1355 regop
.mode
= regop
.special
;
1363 regop
.special
<<= 6;
1365 instr
|= regop
.n
| regop
.mode
| regop
.special
;
1369 parse_regop (®op
, args
[3], oP
->operand
[2]);
1371 regop
.mode
= regop
.special
;
1372 instr
|= (regop
.n
<<= 19) | (regop
.mode
<<= 13);
1377 /* get_args: break individual arguments out of comma-separated list
1380 - all comments and labels have been removed
1381 - all strings of whitespace have been collapsed to a single blank.
1382 - all character constants ('x') have been replaced with decimal
1385 args[0] is untouched. args[1] points to first operand, etc. All args:
1386 - are NULL-terminated
1387 - contain no whitespace
1390 Number of operands (0,1,2, or 3) or -1 on error. */
1393 get_args (char *p
, /* Pointer to comma-separated operands; Mucked by us. */
1394 char *args
[]) /* Output arg: pointers to operands placed in args[1-3].
1395 Must accommodate 4 entries (args[0-3]). */
1398 int n
; /* Number of operands. */
1401 /* Skip lead white space. */
1411 /* Squeze blanks out by moving non-blanks toward start of string.
1412 Isolate operands, whenever comma is found. */
1417 && (! ISALNUM (p
[1])
1418 || ! ISALNUM (p
[-1])))
1422 /* Start of operand. */
1425 as_bad (_("too many operands"));
1428 *to
++ = '\0'; /* Terminate argument. */
1429 args
[++n
] = to
; /* Start next argument. */
1439 /* i_scan: perform lexical scan of ascii assembler instruction.
1442 - input string is an i80960 instruction (not a pseudo-op)
1443 - all comments and labels have been removed
1444 - all strings of whitespace have been collapsed to a single blank.
1447 args[0] points to opcode, other entries point to operands. All strings:
1448 - are NULL-terminated
1449 - contain no whitespace
1450 - have character constants ('x') replaced with a decimal number
1453 Number of operands (0,1,2, or 3) or -1 on error. */
1456 i_scan (char *iP
, /* Pointer to ascii instruction; Mucked by us. */
1457 char *args
[]) /* Output arg: pointers to opcode and operands placed here.
1458 Must accommodate 4 entries. */
1460 /* Isolate opcode. */
1465 for (; *iP
!= ' '; iP
++)
1469 /* There are no operands. */
1472 /* We never moved: there was no opcode either! */
1473 as_bad (_("missing opcode"));
1480 return (get_args (iP
, args
));
1486 /* Emit call to "increment" routine. */
1487 ctrl_fmt (BR_CNT_FUNC
, CALL
, 1);
1488 /* Emit inline counter to be incremented. */
1495 static char buf
[20];
1497 sprintf (buf
, "%s%d", BR_LABEL_BASE
, br_cnt
++);
1502 ctrl_fmt (char *targP
, /* Pointer to text of lone operand (if any). */
1503 long opcode
, /* Template of instruction. */
1504 int num_ops
) /* Number of operands. */
1506 int instrument
; /* TRUE iff we should add instrumentation to track
1507 how often the branch is taken. */
1510 emit (opcode
); /* Output opcode. */
1513 instrument
= instrument_branches
&& (opcode
!= CALL
)
1514 && (opcode
!= B
) && (opcode
!= RET
) && (opcode
!= BAL
);
1519 colon (brlab_next ());
1522 /* The operand MUST be an ip-relative displacement. Parse it
1523 and set up address fix for the instruction we just output. */
1524 get_cdisp (targP
, "CTRL", opcode
, 24, 0, 0);
1532 cobr_fmt (/* arg[0]->opcode mnemonic, arg[1-3]->operands (ascii) */
1534 /* Opcode, with branch-prediction bits already set if necessary. */
1536 /* Pointer to description of instruction. */
1537 struct i960_opcode
*oP
)
1539 long instr
; /* 32-bit instruction. */
1540 struct regop regop
; /* Description of register operand. */
1541 int n
; /* Number of operands. */
1542 int var_frag
; /* 1 if varying length code fragment should
1543 be emitted; 0 if an address fix
1544 should be emitted. */
1551 /* First operand (if any) of a COBR is always a register
1552 operand. Parse it. */
1553 parse_regop (®op
, arg
[1], oP
->operand
[0]);
1554 instr
|= (regop
.n
<< 19) | (regop
.mode
<< 13);
1559 /* Second operand (if any) of a COBR is always a register
1560 operand. Parse it. */
1561 parse_regop (®op
, arg
[2], oP
->operand
[1]);
1562 instr
|= (regop
.n
<< 14) | regop
.special
;
1569 if (instrument_branches
)
1572 colon (brlab_next ());
1575 /* A third operand to a COBR is always a displacement. Parse
1576 it; if it's relaxable (a cobr "j" directive, or any cobr
1577 other than bbs/bbc when the "-norelax" option is not in use)
1578 set up a variable code fragment; otherwise set up an address
1580 var_frag
= !norelax
|| (oP
->format
== COJ
); /* TRUE or FALSE */
1581 get_cdisp (arg
[3], "COBR", instr
, 13, var_frag
, 0);
1583 if (instrument_branches
)
1588 /* Assumptions about the passed-in text:
1589 - all comments, labels removed
1590 - text is an instruction
1591 - all white space compressed to single blanks
1592 - all character constants have been replaced with decimal. */
1595 md_assemble (char *textP
)
1597 /* Parsed instruction text, containing NO whitespace: arg[0]->opcode
1598 mnemonic arg[1-3]->operands, with char constants replaced by
1601 /* Number of instruction operands. */
1603 /* Pointer to instruction description. */
1604 struct i960_opcode
*oP
;
1605 /* TRUE iff opcode mnemonic included branch-prediction suffix (".f"
1608 /* Setting of branch-prediction bit(s) to be OR'd into instruction
1609 opcode of CTRL/COBR format instructions. */
1611 /* Offset of last character in opcode mnemonic. */
1613 const char *bp_error_msg
= _("branch prediction invalid on this opcode");
1615 /* Parse instruction into opcode and operands. */
1616 memset (args
, '\0', sizeof (args
));
1618 n_ops
= i_scan (textP
, args
);
1621 return; /* Error message already issued. */
1623 /* Do "macro substitution" (sort of) on 'ldconst' pseudo-instruction. */
1624 if (!strcmp (args
[0], "ldconst"))
1626 n_ops
= parse_ldconst (args
);
1631 /* Check for branch-prediction suffix on opcode mnemonic, strip it off. */
1632 n
= strlen (args
[0]) - 1;
1636 if (args
[0][n
- 1] == '.' && (args
[0][n
] == 't' || args
[0][n
] == 'f'))
1638 /* We could check here to see if the target architecture
1639 supports branch prediction, but why bother? The bit will
1640 just be ignored by processors that don't use it. */
1642 bp_bits
= (args
[0][n
] == 't') ? BP_TAKEN
: BP_NOT_TAKEN
;
1643 args
[0][n
- 1] = '\0'; /* Strip suffix from opcode mnemonic */
1646 /* Look up opcode mnemonic in table and check number of operands.
1647 Check that opcode is legal for the target architecture. If all
1648 looks good, assemble instruction. */
1649 oP
= (struct i960_opcode
*) hash_find (op_hash
, args
[0]);
1650 if (!oP
|| !targ_has_iclass (oP
->iclass
))
1651 as_bad (_("invalid opcode, \"%s\"."), args
[0]);
1652 else if (n_ops
!= oP
->num_ops
)
1653 as_bad (_("improper number of operands. expecting %d, got %d"),
1654 oP
->num_ops
, n_ops
);
1661 ctrl_fmt (args
[1], oP
->opcode
| bp_bits
, oP
->num_ops
);
1662 if (oP
->format
== FBRA
)
1663 /* Now generate a 'bno' to same arg */
1664 ctrl_fmt (args
[1], BNO
| bp_bits
, 1);
1668 cobr_fmt (args
, oP
->opcode
| bp_bits
, oP
);
1672 as_warn (bp_error_msg
);
1676 if (args
[0][0] == 'c' && args
[0][1] == 'a')
1679 as_warn (bp_error_msg
);
1680 mem_fmt (args
, oP
, 1);
1689 as_warn (bp_error_msg
);
1690 mem_fmt (args
, oP
, 0);
1694 as_warn (bp_error_msg
);
1695 /* Output opcode & set up "fixup" (relocation); flag
1696 relocation as 'callj' type. */
1697 know (oP
->num_ops
== 1);
1698 get_cdisp (args
[1], "CTRL", oP
->opcode
, 24, 0, 1);
1701 BAD_CASE (oP
->format
);
1708 md_number_to_chars (char *buf
,
1712 number_to_chars_littleendian (buf
, value
, n
);
1715 #define MAX_LITTLENUMS 6
1716 #define LNUM_SIZE sizeof (LITTLENUM_TYPE)
1718 /* md_atof: convert ascii to floating point
1720 Turn a string at input_line_pointer into a floating point constant of type
1721 'type', and store the appropriate bytes at *litP. The number of LITTLENUMS
1722 emitted is returned at 'sizeP'. An error message is returned, or a pointer
1723 to an empty message if OK.
1725 Note we call the i386 floating point routine, rather than complicating
1726 things with more files or symbolic links. */
1729 md_atof (int type
, char *litP
, int *sizeP
)
1731 LITTLENUM_TYPE words
[MAX_LITTLENUMS
];
1732 LITTLENUM_TYPE
*wordP
;
1751 type
= 'x'; /* That's what atof_ieee() understands. */
1756 return _("Bad call to md_atof()");
1759 t
= atof_ieee (input_line_pointer
, type
, words
);
1761 input_line_pointer
= t
;
1763 *sizeP
= prec
* LNUM_SIZE
;
1765 /* Output the LITTLENUMs in REVERSE order in accord with i80960
1766 word-order. (Dunno why atof_ieee doesn't do it in the right
1767 order in the first place -- probably because it's a hack of
1769 for (wordP
= words
+ prec
- 1; prec
--;)
1771 md_number_to_chars (litP
, (long) (*wordP
--), LNUM_SIZE
);
1772 litP
+= sizeof (LITTLENUM_TYPE
);
1779 md_number_to_imm (char *buf
, long val
, int n
)
1781 md_number_to_chars (buf
, val
, n
);
1785 md_number_to_field (char *instrP
, /* Pointer to instruction to be fixed. */
1786 long val
, /* Address fixup value. */
1787 bit_fixS
*bfixP
) /* Description of bit field to be fixed up. */
1789 int numbits
; /* Length of bit field to be fixed. */
1790 long instr
; /* 32-bit instruction to be fixed-up. */
1791 long sign
; /* 0 or -1, according to sign bit of 'val'. */
1793 /* Convert instruction back to host byte order. */
1794 instr
= md_chars_to_number (instrP
, 4);
1796 /* Surprise! -- we stored the number of bits to be modified rather
1797 than a pointer to a structure. */
1798 numbits
= (int) (size_t) bfixP
;
1800 /* This is a no-op, stuck here by reloc_callj(). */
1803 know ((numbits
== 13) || (numbits
== 24));
1805 /* Propagate sign bit of 'val' for the given number of bits. Result
1806 should be all 0 or all 1. */
1807 sign
= val
>> ((int) numbits
- 1);
1808 if (((val
< 0) && (sign
!= -1))
1809 || ((val
> 0) && (sign
!= 0)))
1810 as_bad (_("Fixup of %ld too large for field width of %d"),
1814 /* Put bit field into instruction and write back in target
1816 val
&= ~(-1 << (int) numbits
); /* Clear unused sign bits. */
1818 md_number_to_chars (instrP
, instr
, 4);
1824 Invocation line includes a switch not recognized by the base assembler.
1825 See if it's a processor-specific option. For the 960, these are:
1828 Conditional branch instructions that require displacements
1829 greater than 13 bits (or that have external targets) should
1830 generate errors. The default is to replace each such
1831 instruction with the corresponding compare (or chkbit) and
1832 branch instructions. Note that the Intel "j" cobr directives
1833 are ALWAYS "de-optimized" in this way when necessary,
1834 regardless of the setting of this option.
1837 Add code to collect information about branches taken, for
1838 later optimization of branch prediction bits by a separate
1839 tool. COBR and CNTL format instructions have branch
1840 prediction bits (in the CX architecture); if "BR" represents
1841 an instruction in one of these classes, the following rep-
1842 resents the code generated by the assembler:
1844 call <increment routine>
1845 .word 0 # pre-counter
1847 call <increment routine>
1848 .word 0 # post-counter
1850 A table of all such "Labels" is also generated.
1852 -AKA, -AKB, -AKC, -ASA, -ASB, -AMC, -ACA:
1853 Select the 80960 architecture. Instructions or features not
1854 supported by the selected architecture cause fatal errors.
1855 The default is to generate code for any instruction or feature
1856 that is supported by SOME version of the 960 (even if this
1857 means mixing architectures!). */
1859 const char *md_shortopts
= "A:b";
1860 struct option md_longopts
[] =
1862 #define OPTION_LINKRELAX (OPTION_MD_BASE)
1863 {"linkrelax", no_argument
, NULL
, OPTION_LINKRELAX
},
1864 {"link-relax", no_argument
, NULL
, OPTION_LINKRELAX
},
1865 #define OPTION_NORELAX (OPTION_MD_BASE + 1)
1866 {"norelax", no_argument
, NULL
, OPTION_NORELAX
},
1867 {"no-relax", no_argument
, NULL
, OPTION_NORELAX
},
1868 {NULL
, no_argument
, NULL
, 0}
1870 size_t md_longopts_size
= sizeof (md_longopts
);
1877 static const struct tabentry arch_tab
[] =
1881 {"SA", ARCH_KA
}, /* Synonym for KA. */
1882 {"SB", ARCH_KB
}, /* Synonym for KB. */
1883 {"KC", ARCH_MC
}, /* Synonym for MC. */
1892 md_parse_option (int c
, char *arg
)
1896 case OPTION_LINKRELAX
:
1898 flag_keep_locals
= 1;
1901 case OPTION_NORELAX
:
1906 instrument_branches
= 1;
1911 const struct tabentry
*tp
;
1914 for (tp
= arch_tab
; tp
->flag
!= NULL
; tp
++)
1915 if (!strcmp (p
, tp
->flag
))
1918 if (tp
->flag
== NULL
)
1920 as_bad (_("invalid architecture %s"), p
);
1924 architecture
= tp
->arch
;
1936 md_show_usage (FILE *stream
)
1940 fprintf (stream
, _("I960 options:\n"));
1941 for (i
= 0; arch_tab
[i
].flag
; i
++)
1942 fprintf (stream
, "%s-A%s", i
? " | " : "", arch_tab
[i
].flag
);
1943 fprintf (stream
, _("\n\
1944 specify variant of 960 architecture\n\
1945 -b add code to collect statistics about branches taken\n\
1946 -link-relax preserve individual alignment directives so linker\n\
1947 can do relaxing (b.out format only)\n\
1948 -no-relax don't alter compare-and-branch instructions for\n\
1949 long displacements\n"));
1953 Replace cobr instruction in a code fragment with equivalent branch and
1954 compare instructions, so it can reach beyond a 13-bit displacement.
1955 Set up an address fix/relocation for the new branch instruction. */
1957 /* This "conditional jump" table maps cobr instructions into
1958 equivalent compare and branch opcodes. */
1968 { /* COBR OPCODE: */
1969 { CHKBIT
, BNO
}, /* 0x30 - bbc */
1970 { CMPO
, BG
}, /* 0x31 - cmpobg */
1971 { CMPO
, BE
}, /* 0x32 - cmpobe */
1972 { CMPO
, BGE
}, /* 0x33 - cmpobge */
1973 { CMPO
, BL
}, /* 0x34 - cmpobl */
1974 { CMPO
, BNE
}, /* 0x35 - cmpobne */
1975 { CMPO
, BLE
}, /* 0x36 - cmpoble */
1976 { CHKBIT
, BO
}, /* 0x37 - bbs */
1977 { CMPI
, BNO
}, /* 0x38 - cmpibno */
1978 { CMPI
, BG
}, /* 0x39 - cmpibg */
1979 { CMPI
, BE
}, /* 0x3a - cmpibe */
1980 { CMPI
, BGE
}, /* 0x3b - cmpibge */
1981 { CMPI
, BL
}, /* 0x3c - cmpibl */
1982 { CMPI
, BNE
}, /* 0x3d - cmpibne */
1983 { CMPI
, BLE
}, /* 0x3e - cmpible */
1984 { CMPI
, BO
}, /* 0x3f - cmpibo */
1988 relax_cobr (fragS
*fragP
) /* fragP->fr_opcode is assumed to point to
1989 the cobr instruction, which comes at the
1990 end of the code fragment. */
1992 int opcode
, src1
, src2
, m1
, s2
;
1993 /* Bit fields from cobr instruction. */
1994 long bp_bits
; /* Branch prediction bits from cobr instruction. */
1995 long instr
; /* A single i960 instruction. */
1996 /* ->instruction to be replaced. */
1998 fixS
*fixP
; /* Relocation that can be done at assembly time. */
2000 /* Pick up & parse cobr instruction. */
2001 iP
= fragP
->fr_opcode
;
2002 instr
= md_chars_to_number (iP
, 4);
2003 opcode
= ((instr
>> 24) & 0xff) - 0x30; /* "-0x30" for table index. */
2004 src1
= (instr
>> 19) & 0x1f;
2005 m1
= (instr
>> 13) & 1;
2007 src2
= (instr
>> 14) & 0x1f;
2008 bp_bits
= instr
& BP_MASK
;
2010 /* Generate and output compare instruction. */
2011 instr
= coj
[opcode
].compare
2012 | src1
| (m1
<< 11) | (s2
<< 6) | (src2
<< 14);
2013 md_number_to_chars (iP
, instr
, 4);
2015 /* Output branch instruction. */
2016 md_number_to_chars (iP
+ 4, coj
[opcode
].branch
| bp_bits
, 4);
2018 /* Set up address fixup/relocation. */
2019 fixP
= fix_new (fragP
,
2020 iP
+ 4 - fragP
->fr_literal
,
2027 fixP
->fx_bit_fixP
= (bit_fixS
*) 24; /* Store size of bit field. */
2035 Called by base assembler after address relaxation is finished: modify
2036 variable fragments according to how much relaxation was done.
2038 If the fragment substate is still 1, a 13-bit displacement was enough
2039 to reach the symbol in question. Set up an address fixup, but otherwise
2040 leave the cobr instruction alone.
2042 If the fragment substate is 2, a 13-bit displacement was not enough.
2043 Replace the cobr with a two instructions (a compare and a branch). */
2046 md_convert_frag (bfd
*abfd ATTRIBUTE_UNUSED
,
2047 segT sec ATTRIBUTE_UNUSED
,
2050 /* Structure describing needed address fix. */
2053 switch (fragP
->fr_subtype
)
2056 /* Leave single cobr instruction. */
2057 fixP
= fix_new (fragP
,
2058 fragP
->fr_opcode
- fragP
->fr_literal
,
2065 fixP
->fx_bit_fixP
= (bit_fixS
*) 13; /* Size of bit field. */
2068 /* Replace cobr with compare/branch instructions. */
2072 BAD_CASE (fragP
->fr_subtype
);
2077 /* md_estimate_size_before_relax: How much does it look like *fragP will grow?
2079 Called by base assembler just before address relaxation.
2080 Return the amount by which the fragment will grow.
2082 Any symbol that is now undefined will not become defined; cobr's
2083 based on undefined symbols will have to be replaced with a compare
2084 instruction and a branch instruction, and the code fragment will grow
2088 md_estimate_size_before_relax (fragS
*fragP
, segT segment_type
)
2090 /* If symbol is undefined in this segment, go to "relaxed" state
2091 (compare and branch instructions instead of cobr) right now. */
2092 if (S_GET_SEGMENT (fragP
->fr_symbol
) != segment_type
)
2098 return md_relax_table
[fragP
->fr_subtype
].rlx_length
;
2101 #if defined(OBJ_AOUT) | defined(OBJ_BOUT)
2104 This routine exists in order to overcome machine byte-order problems
2105 when dealing with bit-field entries in the relocation_info struct.
2107 But relocation info will be used on the host machine only (only
2108 executable code is actually downloaded to the i80960). Therefore,
2109 we leave it in host byte order. */
2112 md_ri_to_chars (char *where
, struct relocation_info
*ri
)
2114 host_number_to_chars (where
, ri
->r_address
, 4);
2115 host_number_to_chars (where
+ 4, ri
->r_index
, 3);
2117 where
[7] = (ri
->r_pcrel
<< 7
2125 where
[7] = (ri
->r_pcrel
<< 0
2135 #endif /* defined(OBJ_AOUT) | defined(OBJ_BOUT) */
2138 /* brtab_emit: generate the fetch-prediction branch table.
2140 See the comments above the declaration of 'br_cnt' for details on
2141 branch-prediction instrumentation.
2143 The code emitted here would be functionally equivalent to the following
2144 example assembler source.
2149 .word 0 # link to next table
2150 .word 3 # length of table
2151 .word LBRANCH0 # 1st entry in table proper
2160 /* Where the binary was output to. */
2162 /* Pointer to description of deferred address fixup. */
2165 if (!instrument_branches
)
2168 subseg_set (data_section
, 0); /* .data */
2169 frag_align (2, 0, 0); /* .align 2 */
2170 record_alignment (now_seg
, 2);
2171 colon (BR_TAB_NAME
); /* BR_TAB_NAME: */
2172 emit (0); /* .word 0 #link to next table */
2173 emit (br_cnt
); /* .word n #length of table */
2175 for (i
= 0; i
< br_cnt
; i
++)
2177 sprintf (buf
, "%s%d", BR_LABEL_BASE
, i
);
2179 fixP
= fix_new (frag_now
,
2180 p
- frag_now
->fr_literal
,
2181 4, symbol_find (buf
), 0, 0, NO_RELOC
);
2185 /* s_leafproc: process .leafproc pseudo-op
2187 .leafproc takes two arguments, the second one is optional:
2188 arg[1]: name of 'call' entry point to leaf procedure
2189 arg[2]: name of 'bal' entry point to leaf procedure
2191 If the two arguments are identical, or if the second one is missing,
2192 the first argument is taken to be the 'bal' entry point.
2194 If there are 2 distinct arguments, we must make sure that the 'bal'
2195 entry point immediately follows the 'call' entry point in the linked
2199 s_leafproc (int n_ops
, /* Number of operands. */
2200 char *args
[]) /* args[1]->1st operand, args[2]->2nd operand. */
2202 symbolS
*callP
; /* Pointer to leafproc 'call' entry point symbol. */
2203 symbolS
*balP
; /* Pointer to leafproc 'bal' entry point symbol. */
2205 if ((n_ops
!= 1) && (n_ops
!= 2))
2207 as_bad (_("should have 1 or 2 operands"));
2211 /* Find or create symbol for 'call' entry point. */
2212 callP
= symbol_find_or_make (args
[1]);
2214 if (TC_S_IS_CALLNAME (callP
))
2215 as_warn (_("Redefining leafproc %s"), S_GET_NAME (callP
));
2217 /* If that was the only argument, use it as the 'bal' entry point.
2218 Otherwise, mark it as the 'call' entry point and find or create
2219 another symbol for the 'bal' entry point. */
2220 if ((n_ops
== 1) || !strcmp (args
[1], args
[2]))
2222 TC_S_FORCE_TO_BALNAME (callP
);
2226 TC_S_FORCE_TO_CALLNAME (callP
);
2228 balP
= symbol_find_or_make (args
[2]);
2229 if (TC_S_IS_CALLNAME (balP
))
2230 as_warn (_("Redefining leafproc %s"), S_GET_NAME (balP
));
2232 TC_S_FORCE_TO_BALNAME (balP
);
2235 tc_set_bal_of_call (callP
, balP
);
2240 /* s_sysproc: process .sysproc pseudo-op
2242 .sysproc takes two arguments:
2243 arg[1]: name of entry point to system procedure
2244 arg[2]: 'entry_num' (index) of system procedure in the range
2247 For [ab].out, we store the 'entrynum' in the 'n_other' field of
2248 the symbol. Since that entry is normally 0, we bias 'entrynum'
2249 by adding 1 to it. It must be unbiased before it is used. */
2252 s_sysproc (int n_ops
, /* Number of operands. */
2253 char *args
[]) /* args[1]->1st operand, args[2]->2nd operand. */
2260 as_bad (_("should have two operands"));
2264 /* Parse "entry_num" argument and check it for validity. */
2265 parse_expr (args
[2], &exp
);
2266 if (exp
.X_op
!= O_constant
2268 || (offs (exp
) > 31))
2270 as_bad (_("'entry_num' must be absolute number in [0,31]"));
2274 /* Find/make symbol and stick entry number (biased by +1) into it. */
2275 symP
= symbol_find_or_make (args
[1]);
2277 if (TC_S_IS_SYSPROC (symP
))
2278 as_warn (_("Redefining entrynum for sysproc %s"), S_GET_NAME (symP
));
2280 TC_S_SET_SYSPROC (symP
, offs (exp
)); /* Encode entry number. */
2281 TC_S_FORCE_TO_SYSPROC (symP
);
2284 /* parse_po: parse machine-dependent pseudo-op
2286 This is a top-level routine for machine-dependent pseudo-ops. It slurps
2287 up the rest of the input line, breaks out the individual arguments,
2288 and dispatches them to the correct handler. */
2291 parse_po (int po_num
) /* Pseudo-op number: currently S_LEAFPROC or S_SYSPROC. */
2293 /* Pointers operands, with no embedded whitespace.
2294 arg[0] unused, arg[1-3]->operands. */
2296 int n_ops
; /* Number of operands. */
2297 char *p
; /* Pointer to beginning of unparsed argument string. */
2298 char eol
; /* Character that indicated end of line. */
2300 extern char is_end_of_line
[];
2302 /* Advance input pointer to end of line. */
2303 p
= input_line_pointer
;
2304 while (!is_end_of_line
[(unsigned char) *input_line_pointer
])
2305 input_line_pointer
++;
2307 eol
= *input_line_pointer
; /* Save end-of-line char. */
2308 *input_line_pointer
= '\0'; /* Terminate argument list. */
2310 /* Parse out operands. */
2311 n_ops
= get_args (p
, args
);
2315 /* Dispatch to correct handler. */
2319 s_sysproc (n_ops
, args
);
2322 s_leafproc (n_ops
, args
);
2329 /* Restore eol, so line numbers get updated correctly. Base
2330 assembler assumes we leave input pointer pointing at char
2331 following the eol. */
2332 *input_line_pointer
++ = eol
;
2335 /* reloc_callj: Relocate a 'callj' instruction
2337 This is a "non-(GNU)-standard" machine-dependent hook. The base
2338 assembler calls it when it decides it can relocate an address at
2339 assembly time instead of emitting a relocation directive.
2341 Check to see if the relocation involves a 'callj' instruction to a:
2342 sysproc: Replace the default 'call' instruction with a 'calls'
2343 leafproc: Replace the default 'call' instruction with a 'bal'.
2344 other proc: Do nothing.
2346 See b.out.h for details on the 'n_other' field in a symbol structure.
2349 Assumes the caller has already figured out, in the case of a leafproc,
2350 to use the 'bal' entry point, and has substituted that symbol into the
2351 passed fixup structure. */
2354 reloc_callj (fixS
*fixP
) /* Relocation that can be done at assembly time. */
2356 /* Points to the binary for the instruction being relocated. */
2359 if (!fixP
->fx_tcbit
)
2360 /* This wasn't a callj instruction in the first place. */
2363 where
= fixP
->fx_frag
->fr_literal
+ fixP
->fx_where
;
2365 if (TC_S_IS_SYSPROC (fixP
->fx_addsy
))
2367 /* Symbol is a .sysproc: replace 'call' with 'calls'. System
2368 procedure number is (other-1). */
2369 md_number_to_chars (where
, CALLS
| TC_S_GET_SYSPROC (fixP
->fx_addsy
), 4);
2371 /* Nothing else needs to be done for this instruction. Make
2372 sure 'md_number_to_field()' will perform a no-op. */
2373 fixP
->fx_bit_fixP
= (bit_fixS
*) 1;
2375 else if (TC_S_IS_CALLNAME (fixP
->fx_addsy
))
2377 /* Should not happen: see block comment above. */
2378 as_fatal (_("Trying to 'bal' to %s"), S_GET_NAME (fixP
->fx_addsy
));
2380 else if (TC_S_IS_BALNAME (fixP
->fx_addsy
))
2382 /* Replace 'call' with 'bal'; both instructions have the same
2383 format, so calling code should complete relocation as if
2384 nothing happened here. */
2385 md_number_to_chars (where
, BAL
, 4);
2387 else if (TC_S_IS_BADPROC (fixP
->fx_addsy
))
2388 as_bad (_("Looks like a proc, but can't tell what kind.\n"));
2390 /* Otherwise Symbol is neither a sysproc nor a leafproc. */
2394 /* Handle the MRI .endian pseudo-op. */
2397 s_endian (int ignore ATTRIBUTE_UNUSED
)
2402 name
= input_line_pointer
;
2403 c
= get_symbol_end ();
2404 if (strcasecmp (name
, "little") == 0)
2406 else if (strcasecmp (name
, "big") == 0)
2407 as_bad (_("big endian mode is not supported"));
2409 as_warn (_("ignoring unrecognized .endian type `%s'"), name
);
2411 *input_line_pointer
= c
;
2413 demand_empty_rest_of_line ();
2416 /* We have no need to default values of symbols. */
2419 md_undefined_symbol (char *name ATTRIBUTE_UNUSED
)
2424 /* Exactly what point is a PC-relative offset relative TO?
2425 On the i960, they're relative to the address of the instruction,
2426 which we have set up as the address of the fixup too. */
2428 md_pcrel_from (fixS
*fixP
)
2430 return fixP
->fx_where
+ fixP
->fx_frag
->fr_address
;
2434 md_apply_fix (fixS
*fixP
,
2436 segT seg ATTRIBUTE_UNUSED
)
2439 char *place
= fixP
->fx_where
+ fixP
->fx_frag
->fr_literal
;
2441 if (!fixP
->fx_bit_fixP
)
2443 md_number_to_imm (place
, val
, fixP
->fx_size
);
2445 else if ((int) (size_t) fixP
->fx_bit_fixP
== 13
2446 && fixP
->fx_addsy
!= NULL
2447 && S_GET_SEGMENT (fixP
->fx_addsy
) == undefined_section
)
2449 /* This is a COBR instruction. They have only a
2450 13-bit displacement and are only to be used
2451 for local branches: flag as error, don't generate
2453 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2454 _("can't use COBR format with external label"));
2455 fixP
->fx_addsy
= NULL
;
2458 md_number_to_field (place
, val
, fixP
->fx_bit_fixP
);
2460 if (fixP
->fx_addsy
== NULL
)
2464 #if defined(OBJ_AOUT) | defined(OBJ_BOUT)
2466 tc_bout_fix_to_chars (char *where
,
2468 relax_addressT segment_address_in_file
)
2470 static const unsigned char nbytes_r_length
[] = {42, 0, 1, 42, 2};
2471 struct relocation_info ri
;
2474 memset ((char *) &ri
, '\0', sizeof (ri
));
2475 symbolP
= fixP
->fx_addsy
;
2476 know (symbolP
!= 0 || fixP
->fx_r_type
!= NO_RELOC
);
2477 ri
.r_bsr
= fixP
->fx_bsr
; /*SAC LD RELAX HACK */
2478 /* These two 'cuz of NS32K */
2479 ri
.r_callj
= fixP
->fx_tcbit
;
2480 if (fixP
->fx_bit_fixP
)
2483 ri
.r_length
= nbytes_r_length
[fixP
->fx_size
];
2484 ri
.r_pcrel
= fixP
->fx_pcrel
;
2485 ri
.r_address
= fixP
->fx_frag
->fr_address
+ fixP
->fx_where
- segment_address_in_file
;
2487 if (fixP
->fx_r_type
!= NO_RELOC
)
2489 switch (fixP
->fx_r_type
)
2494 ri
.r_length
= fixP
->fx_size
- 1;
2508 else if (linkrelax
|| !S_IS_DEFINED (symbolP
) || fixP
->fx_bsr
)
2511 ri
.r_index
= symbolP
->sy_number
;
2516 ri
.r_index
= S_GET_TYPE (symbolP
);
2519 /* Output the relocation information in machine-dependent form. */
2520 md_ri_to_chars (where
, &ri
);
2523 #endif /* OBJ_AOUT or OBJ_BOUT */
2525 /* Align an address by rounding it up to the specified boundary. */
2528 md_section_align (segT seg
,
2529 valueT addr
) /* Address to be rounded up. */
2533 align
= bfd_get_section_alignment (stdoutput
, seg
);
2534 return (addr
+ (1 << align
) - 1) & (-1 << align
);
2537 extern int coff_flags
;
2539 /* For aout or bout, the bal immediately follows the call.
2541 For coff, we cheat and store a pointer to the bal symbol in the
2542 second aux entry of the call. */
2553 tc_set_bal_of_call (symbolS
*callP ATTRIBUTE_UNUSED
,
2554 symbolS
*balP ATTRIBUTE_UNUSED
)
2556 know (TC_S_IS_CALLNAME (callP
));
2557 know (TC_S_IS_BALNAME (balP
));
2561 callP
->sy_tc
= balP
;
2562 S_SET_NUMBER_AUXILIARY (callP
, 2);
2564 #else /* ! OBJ_COFF */
2567 /* If the 'bal' entry doesn't immediately follow the 'call'
2568 symbol, unlink it from the symbol list and re-insert it. */
2569 if (symbol_next (callP
) != balP
)
2571 symbol_remove (balP
, &symbol_rootP
, &symbol_lastP
);
2572 symbol_append (balP
, callP
, &symbol_rootP
, &symbol_lastP
);
2573 } /* if not in order */
2575 #else /* ! OBJ_ABOUT */
2576 as_fatal ("Only supported for a.out, b.out, or COFF");
2577 #endif /* ! OBJ_ABOUT */
2578 #endif /* ! OBJ_COFF */
2582 tc_get_bal_of_call (symbolS
*callP ATTRIBUTE_UNUSED
)
2586 know (TC_S_IS_CALLNAME (callP
));
2589 retval
= callP
->sy_tc
;
2592 retval
= symbol_next (callP
);
2594 as_fatal ("Only supported for a.out, b.out, or COFF");
2595 #endif /* ! OBJ_ABOUT */
2596 #endif /* ! OBJ_COFF */
2598 know (TC_S_IS_BALNAME (retval
));
2604 tc_coff_symbol_emit_hook (symbolS
*symbolP ATTRIBUTE_UNUSED
)
2606 if (TC_S_IS_CALLNAME (symbolP
))
2608 symbolS
*balP
= tc_get_bal_of_call (symbolP
);
2610 symbolP
->sy_symbol
.ost_auxent
[1].x_bal
.x_balntry
= S_GET_VALUE (balP
);
2611 if (S_GET_STORAGE_CLASS (symbolP
) == C_EXT
)
2612 S_SET_STORAGE_CLASS (symbolP
, C_LEAFEXT
);
2614 S_SET_STORAGE_CLASS (symbolP
, C_LEAFSTAT
);
2615 S_SET_DATA_TYPE (symbolP
, S_GET_DATA_TYPE (symbolP
) | (DT_FCN
<< N_BTSHFT
));
2616 /* Fix up the bal symbol. */
2617 S_SET_STORAGE_CLASS (balP
, C_LABEL
);
2620 #endif /* OBJ_COFF */
2623 i960_handle_align (fragS
*fragp ATTRIBUTE_UNUSED
)
2629 as_bad (_("option --link-relax is only supported in b.out format"));
2634 /* The text section "ends" with another alignment reloc, to which we
2635 aren't adding padding. */
2636 if (fragp
->fr_next
== text_last_frag
2637 || fragp
->fr_next
== data_last_frag
)
2640 /* alignment directive */
2641 fix_new (fragp
, fragp
->fr_fix
, fragp
->fr_offset
, 0, 0, 0,
2642 (int) fragp
->fr_type
);
2643 #endif /* OBJ_BOUT */
2647 i960_validate_fix (fixS
*fixP
, segT this_segment_type ATTRIBUTE_UNUSED
)
2649 if (fixP
->fx_tcbit
&& TC_S_IS_CALLNAME (fixP
->fx_addsy
))
2651 /* Relocation should be done via the associated 'bal'
2652 entry point symbol. */
2653 if (!TC_S_IS_BALNAME (tc_get_bal_of_call (fixP
->fx_addsy
)))
2655 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2656 _("No 'bal' entry point for leafproc %s"),
2657 S_GET_NAME (fixP
->fx_addsy
));
2660 fixP
->fx_addsy
= tc_get_bal_of_call (fixP
->fx_addsy
);
2669 tc_bfd_fix2rtype (fixS
*fixP
)
2671 if (fixP
->fx_pcrel
== 0 && fixP
->fx_size
== 4)
2672 return BFD_RELOC_32
;
2674 if (fixP
->fx_pcrel
!= 0 && fixP
->fx_size
== 4)
2675 return BFD_RELOC_24_PCREL
;
2681 /* Translate internal representation of relocation info to BFD target
2684 FIXME: To what extent can we get all relevant targets to use this? */
2687 tc_gen_reloc (asection
*section ATTRIBUTE_UNUSED
, fixS
*fixP
)
2691 reloc
= xmalloc (sizeof (arelent
));
2693 /* HACK: Is this right? */
2694 fixP
->fx_r_type
= tc_bfd_fix2rtype (fixP
);
2696 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixP
->fx_r_type
);
2697 if (reloc
->howto
== NULL
)
2699 as_bad_where (fixP
->fx_file
, fixP
->fx_line
,
2700 "internal error: can't export reloc type %d (`%s')",
2702 bfd_get_reloc_code_name (fixP
->fx_r_type
));
2706 assert (!fixP
->fx_pcrel
== !reloc
->howto
->pc_relative
);
2708 reloc
->sym_ptr_ptr
= xmalloc (sizeof (asymbol
*));
2709 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixP
->fx_addsy
);
2710 reloc
->address
= fixP
->fx_frag
->fr_address
+ fixP
->fx_where
;
2711 reloc
->addend
= fixP
->fx_addnumber
;
2716 /* end from cgen.c */
2718 const pseudo_typeS md_pseudo_table
[] =
2720 {"bss", s_lcomm
, 1},
2721 {"endian", s_endian
, 0},
2722 {"extended", float_cons
, 't'},
2723 {"leafproc", parse_po
, S_LEAFPROC
},
2724 {"sysproc", parse_po
, S_SYSPROC
},