1 /* tc-i960.c - All the i80960-specific stuff
2 Copyright (C) 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
4 This file is part of GAS.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* See comment on md_parse_option for 80960-specific invocation options. */
22 /******************************************************************************
24 * Header, symbol, and relocation info will be used on the host machine
25 * only -- only executable code is actually downloaded to the i80960.
26 * Therefore, leave all such information in host byte order.
28 * (That's a slight lie -- we DO download some header information, but
29 * the downloader converts the file format and corrects the byte-ordering
30 * of the relevant fields while doing so.)
32 * ==> THIS IS NO LONGER TRUE USING BFD. WE CAN GENERATE ANY BYTE ORDER
33 * FOR THE HEADER, AND READ ANY BYTE ORDER. PREFERENCE WOULD BE TO
34 * USE LITTLE-ENDIAN BYTE ORDER THROUGHOUT, REGARDLESS OF HOST. <==
36 ***************************************************************************** */
38 /* There are 4 different lengths of (potentially) symbol-based displacements
39 * in the 80960 instruction set, each of which could require address fix-ups
40 * and (in the case of external symbols) emission of relocation directives:
43 * This is a standard length for the base assembler and requires no
47 * This is a non-standard length, but the base assembler has a hook for
48 * bit field address fixups: the fixS structure can point to a descriptor
49 * of the field, in which case our md_number_to_field() routine gets called
52 * I made the hook a little cleaner by having fix_new() (in the base
53 * assembler) return a pointer to the fixS in question. And I made it a
54 * little simpler by storing the field size (in this case 13) instead of
55 * of a pointer to another structure: 80960 displacements are ALWAYS
56 * stored in the low-order bits of a 4-byte word.
58 * Since the target of a COBR cannot be external, no relocation directives
59 * for this size displacement have to be generated. But the base assembler
60 * had to be modified to issue error messages if the symbol did turn out
64 * Fixups are handled as for the 13-bit case (except that 24 is stored
67 * The relocation directive generated is the same as that for the 32-bit
68 * displacement, except that it's PC-relative (the 32-bit displacement
69 * never is). The i80960 version of the linker needs a mod to
70 * distinguish and handle the 24-bit case.
73 * MEMA formats are always promoted to MEMB (32-bit) if the displacement
74 * is based on a symbol, because it could be relocated at link time.
75 * The only time we use the 12-bit format is if an absolute value of
76 * less than 4096 is specified, in which case we need neither a fixup nor
77 * a relocation directive.
87 #include "opcode/i960.h"
89 extern char *input_line_pointer;
90 extern struct hash_control *po_hash;
91 extern char *next_object_file_charP;
94 const int md_reloc_size = sizeof (struct reloc);
96 const int md_reloc_size = sizeof (struct relocation_info);
99 /***************************
100 * Local i80960 routines *
101 ************************** */
103 static void brcnt_emit (); /* Emit branch-prediction instrumentation code */
104 static char *brlab_next (); /* Return next branch local label */
105 void brtab_emit (); /* Emit br-predict instrumentation table */
106 static void cobr_fmt (); /* Generate COBR instruction */
107 static void ctrl_fmt (); /* Generate CTRL instruction */
108 static char *emit (); /* Emit (internally) binary */
109 static int get_args (); /* Break arguments out of comma-separated list */
110 static void get_cdisp (); /* Handle COBR or CTRL displacement */
111 static char *get_ispec (); /* Find index specification string */
112 static int get_regnum (); /* Translate text to register number */
113 static int i_scan (); /* Lexical scan of instruction source */
114 static void mem_fmt (); /* Generate MEMA or MEMB instruction */
115 static void mema_to_memb (); /* Convert MEMA instruction to MEMB format */
116 static void parse_expr (); /* Parse an expression */
117 static int parse_ldconst (); /* Parse and replace a 'ldconst' pseudo-op */
118 static void parse_memop (); /* Parse a memory operand */
119 static void parse_po (); /* Parse machine-dependent pseudo-op */
120 static void parse_regop (); /* Parse a register operand */
121 static void reg_fmt (); /* Generate a REG format instruction */
122 void reloc_callj (); /* Relocate a 'callj' instruction */
123 static void relax_cobr (); /* "De-optimize" cobr into compare/branch */
124 static void s_leafproc (); /* Process '.leafproc' pseudo-op */
125 static void s_sysproc (); /* Process '.sysproc' pseudo-op */
126 static int shift_ok (); /* Will a 'shlo' substiture for a 'ldconst'? */
127 static void syntax (); /* Give syntax error */
128 static int targ_has_sfr (); /* Target chip supports spec-func register? */
129 static int targ_has_iclass (); /* Target chip supports instruction set? */
130 /* static void unlink_sym(); *//* Remove a symbol from the symbol list */
132 /* See md_parse_option() for meanings of these options */
133 static char norelax; /* True if -norelax switch seen */
134 static char instrument_branches;/* True if -b switch seen */
136 /* Characters that always start a comment.
137 * If the pre-processor is disabled, these aren't very useful.
139 const char comment_chars[] = "#";
141 /* Characters that only start a comment at the beginning of
142 * a line. If the line seems to have the form '# 123 filename'
143 * .line and .file directives will appear in the pre-processed output.
145 * Note that input_file.c hand checks for '#' at the beginning of the
146 * first line of the input file. This is because the compiler outputs
147 * #NO_APP at the beginning of its output.
150 /* Also note that comments started like this one will always work. */
152 const char line_comment_chars[1];
154 const char line_separator_chars[1];
156 /* Chars that can be used to separate mant from exp in floating point nums */
157 const char EXP_CHARS[] = "eE";
159 /* Chars that mean this number is a floating point constant,
160 * as in 0f12.456 or 0d1.2345e12
162 const char FLT_CHARS[] = "fFdDtT";
165 /* Table used by base assembler to relax addresses based on varying length
166 * instructions. The fields are:
167 * 1) most positive reach of this state,
168 * 2) most negative reach of this state,
169 * 3) how many bytes this mode will add to the size of the current frag
170 * 4) which index into the table to try if we can't fit into this one.
172 * For i80960, the only application is the (de-)optimization of cobr
173 * instructions into separate compare and branch instructions when a 13-bit
174 * displacement won't hack it.
179 {0, 0, 0, 0}, /* State 0 => no more relaxation possible */
180 {4088, -4096, 0, 2}, /* State 1: conditional branch (cobr) */
181 {0x800000 - 8, -0x800000, 4, 0}, /* State 2: compare (reg) & branch (ctrl) */
185 /* These are the machine dependent pseudo-ops.
187 * This table describes all the machine specific pseudo-ops the assembler
188 * has to support. The fields are:
189 * pseudo-op name without dot
190 * function to call to execute this pseudo-op
191 * integer arg to pass to the function
196 const pseudo_typeS md_pseudo_table[] =
199 {"extended", float_cons, 't'},
200 {"leafproc", parse_po, S_LEAFPROC},
201 {"sysproc", parse_po, S_SYSPROC},
204 {"quad", big_cons, 16},
209 /* Macros to extract info from an 'expressionS' structure 'e' */
210 #define adds(e) e.X_add_symbol
211 #define offs(e) e.X_add_number
214 /* Branch-prediction bits for CTRL/COBR format opcodes */
215 #define BP_MASK 0x00000002 /* Mask for branch-prediction bit */
216 #define BP_TAKEN 0x00000000 /* Value to OR in to predict branch */
217 #define BP_NOT_TAKEN 0x00000002 /* Value to OR in to predict no branch */
220 /* Some instruction opcodes that we need explicitly */
221 #define BE 0x12000000
222 #define BG 0x11000000
223 #define BGE 0x13000000
224 #define BL 0x14000000
225 #define BLE 0x16000000
226 #define BNE 0x15000000
227 #define BNO 0x10000000
228 #define BO 0x17000000
229 #define CHKBIT 0x5a002700
230 #define CMPI 0x5a002080
231 #define CMPO 0x5a002000
234 #define BAL 0x0b000000
235 #define CALL 0x09000000
236 #define CALLS 0x66003800
237 #define RET 0x0a000000
240 /* These masks are used to build up a set of MEMB mode bits. */
243 #define MEMB_BIT 0x1000
247 /* Mask for the only mode bit in a MEMA instruction (if set, abase reg is
249 #define MEMA_ABASE 0x2000
251 /* Info from which a MEMA or MEMB format instruction can be generated */
254 /* (First) 32 bits of instruction */
256 /* 0-(none), 12- or, 32-bit displacement needed */
258 /* The expression in the source instruction from which the
259 displacement should be determined. */
266 /* The two pieces of info we need to generate a register operand */
269 int mode; /* 0 =>local/global/spec reg; 1=> literal or fp reg */
270 int special; /* 0 =>not a sfr; 1=> is a sfr (not valid w/mode=0) */
271 int n; /* Register number or literal value */
275 /* Number and assembler mnemonic for all registers that can appear in operands */
317 /* Numbers for special-function registers are for assembler internal
318 use only: they are scaled back to range [0-31] for binary output. */
354 /* Numbers for floating point registers are for assembler internal use
355 * only: they are scaled back to [0-3] for binary output.
364 { NULL, 0 }, /* END OF LIST */
367 #define IS_RG_REG(n) ((0 <= (n)) && ((n) < SF0))
368 #define IS_SF_REG(n) ((SF0 <= (n)) && ((n) < FP0))
369 #define IS_FP_REG(n) ((n) >= FP0)
371 /* Number and assembler mnemonic for all registers that can appear as 'abase'
372 * (indirect addressing) registers.
416 /* For assembler internal use only: this number never appears in binary
420 { NULL, 0 }, /* END OF LIST */
425 static struct hash_control *op_hash; /* Opcode mnemonics */
426 static struct hash_control *reg_hash; /* Register name hash table */
427 static struct hash_control *areg_hash; /* Abase register hash table */
430 /* Architecture for which we are assembling */
431 #define ARCH_ANY 0 /* Default: no architecture checking done */
436 int architecture = ARCH_ANY; /* Architecture requested on invocation line */
437 int iclasses_seen; /* OR of instruction classes (I_* constants)
438 * for which we've actually assembled
443 /* BRANCH-PREDICTION INSTRUMENTATION
445 * The following supports generation of branch-prediction instrumentation
446 * (turned on by -b switch). The instrumentation collects counts
447 * of branches taken/not-taken for later input to a utility that will
448 * set the branch prediction bits of the instructions in accordance with
449 * the behavior observed. (Note that the KX series does not have
452 * The instrumentation consists of:
454 * (1) before and after each conditional branch, a call to an external
455 * routine that increments and steps over an inline counter. The
456 * counter itself, initialized to 0, immediately follows the call
457 * instruction. For each branch, the counter following the branch
458 * is the number of times the branch was not taken, and the difference
459 * between the counters is the number of times it was taken. An
460 * example of an instrumented conditional branch:
464 * LBRANCH23: be label
468 * (2) a table of pointers to the instrumented branches, so that an
469 * external postprocessing routine can locate all of the counters.
470 * the table begins with a 2-word header: a pointer to the next in
471 * a linked list of such tables (initialized to 0); and a count
472 * of the number of entries in the table (exclusive of the header.
474 * Note that input source code is expected to already contain calls
475 * an external routine that will link the branch local table into a
476 * list of such tables.
479 static int br_cnt; /* Number of branches instrumented so far.
480 * Also used to generate unique local labels
481 * for each instrumented branch
484 #define BR_LABEL_BASE "LBRANCH"
485 /* Basename of local labels on instrumented
486 * branches, to avoid conflict with compiler-
487 * generated local labels.
490 #define BR_CNT_FUNC "__inc_branch"
491 /* Name of the external routine that will
492 * increment (and step over) an inline counter.
495 #define BR_TAB_NAME "__BRANCH_TABLE__"
496 /* Name of the table of pointers to branches.
497 * A local (i.e., non-external) symbol.
500 /*****************************************************************************
501 * md_begin: One-time initialization.
503 * Set up hash tables.
505 **************************************************************************** */
509 int i; /* Loop counter */
510 const struct i960_opcode *oP; /* Pointer into opcode table */
511 char *retval; /* Value returned by hash functions */
513 if (((op_hash = hash_new ()) == 0)
514 || ((reg_hash = hash_new ()) == 0)
515 || ((areg_hash = hash_new ()) == 0))
517 as_fatal ("virtual memory exceeded");
520 /* For some reason, the base assembler uses an empty string for "no
521 error message", instead of a NULL pointer. */
524 for (oP = i960_opcodes; oP->name && !*retval; oP++)
526 retval = hash_insert (op_hash, oP->name, oP);
529 for (i = 0; regnames[i].reg_name && !*retval; i++)
531 retval = hash_insert (reg_hash, regnames[i].reg_name,
532 ®names[i].reg_num);
535 for (i = 0; aregs[i].areg_name && !*retval; i++)
537 retval = hash_insert (areg_hash, aregs[i].areg_name,
543 as_fatal ("Hashing returned \"%s\".", retval);
547 /*****************************************************************************
548 * md_end: One-time final cleanup
552 **************************************************************************** */
558 /*****************************************************************************
559 * md_assemble: Assemble an instruction
561 * Assumptions about the passed-in text:
562 * - all comments, labels removed
563 * - text is an instruction
564 * - all white space compressed to single blanks
565 * - all character constants have been replaced with decimal
567 **************************************************************************** */
570 char *textP; /* Source text of instruction */
572 /* Parsed instruction text, containing NO whitespace:
573 * arg[0]->opcode mnemonic
574 * arg[1-3]->operands, with char constants
575 * replaced by decimal numbers
579 int n_ops; /* Number of instruction operands */
581 /* Pointer to instruction description */
582 struct i960_opcode *oP;
583 /* TRUE iff opcode mnemonic included branch-prediction
584 * suffix (".f" or ".t")
587 /* Setting of branch-prediction bit(s) to be OR'd
588 * into instruction opcode of CTRL/COBR format
593 int n; /* Offset of last character in opcode mnemonic */
595 static const char bp_error_msg[] = "branch prediction invalid on this opcode";
598 /* Parse instruction into opcode and operands */
599 memset (args, '\0', sizeof (args));
600 n_ops = i_scan (textP, args);
603 return; /* Error message already issued */
606 /* Do "macro substitution" (sort of) on 'ldconst' pseudo-instruction */
607 if (!strcmp (args[0], "ldconst"))
609 n_ops = parse_ldconst (args);
618 /* Check for branch-prediction suffix on opcode mnemonic, strip it off */
619 n = strlen (args[0]) - 1;
622 if (args[0][n - 1] == '.' && (args[0][n] == 't' || args[0][n] == 'f'))
624 /* We could check here to see if the target architecture
625 * supports branch prediction, but why bother? The bit
626 * will just be ignored by processors that don't use it.
629 bp_bits = (args[0][n] == 't') ? BP_TAKEN : BP_NOT_TAKEN;
630 args[0][n - 1] = '\0'; /* Strip suffix from opcode mnemonic */
633 /* Look up opcode mnemonic in table and check number of operands.
634 * Check that opcode is legal for the target architecture.
635 * If all looks good, assemble instruction.
637 oP = (struct i960_opcode *) hash_find (op_hash, args[0]);
638 if (!oP || !targ_has_iclass (oP->iclass))
640 as_bad ("invalid opcode, \"%s\".", args[0]);
643 else if (n_ops != oP->num_ops)
645 as_bad ("improper number of operands. expecting %d, got %d",
654 ctrl_fmt (args[1], oP->opcode | bp_bits, oP->num_ops);
655 if (oP->format == FBRA)
657 /* Now generate a 'bno' to same arg */
658 ctrl_fmt (args[1], BNO | bp_bits, 1);
663 cobr_fmt (args, oP->opcode | bp_bits, oP);
668 as_warn (bp_error_msg);
673 if (args[0][0] == 'c' && args[0][1] == 'a')
677 as_warn (bp_error_msg);
679 mem_fmt (args, oP, 1);
689 as_warn (bp_error_msg);
691 mem_fmt (args, oP, 0);
696 as_warn (bp_error_msg);
698 /* Output opcode & set up "fixup" (relocation);
699 * flag relocation as 'callj' type.
701 know (oP->num_ops == 1);
702 get_cdisp (args[1], "CTRL", oP->opcode, 24, 0, 1);
705 BAD_CASE (oP->format);
709 } /* md_assemble() */
711 /*****************************************************************************
712 * md_number_to_chars: convert a number to target byte order
714 **************************************************************************** */
716 md_number_to_chars (buf, value, n)
717 char *buf; /* Put output here */
718 valueT value; /* The integer to be converted */
719 int n; /* Number of bytes to output (significant bytes
729 /* XXX line number probably botched for this warning message. */
730 if (value != 0 && value != -1)
732 as_bad ("Displacement too long for instruction field length.");
736 } /* md_number_to_chars() */
738 /*****************************************************************************
739 * md_chars_to_number: convert from target byte order to host byte order.
741 **************************************************************************** */
743 md_chars_to_number (val, n)
744 unsigned char *val; /* Value in target byte order */
745 int n; /* Number of bytes in the input */
749 for (retval = 0; n--;)
758 #define MAX_LITTLENUMS 6
759 #define LNUM_SIZE sizeof(LITTLENUM_TYPE)
761 /*****************************************************************************
762 * md_atof: convert ascii to floating point
764 * Turn a string at input_line_pointer into a floating point constant of type
765 * 'type', and store the appropriate bytes at *litP. The number of LITTLENUMS
766 * emitted is returned at 'sizeP'. An error message is returned, or a pointer
767 * to an empty message if OK.
769 * Note we call the i386 floating point routine, rather than complicating
770 * things with more files or symbolic links.
772 **************************************************************************** */
774 md_atof (type, litP, sizeP)
779 LITTLENUM_TYPE words[MAX_LITTLENUMS];
780 LITTLENUM_TYPE *wordP;
800 type = 'x'; /* That's what atof_ieee() understands */
805 return "Bad call to md_atof()";
808 t = atof_ieee (input_line_pointer, type, words);
811 input_line_pointer = t;
814 *sizeP = prec * LNUM_SIZE;
816 /* Output the LITTLENUMs in REVERSE order in accord with i80960
817 * word-order. (Dunno why atof_ieee doesn't do it in the right
818 * order in the first place -- probably because it's a hack of
822 for (wordP = words + prec - 1; prec--;)
824 md_number_to_chars (litP, (long) (*wordP--), LNUM_SIZE);
825 litP += sizeof (LITTLENUM_TYPE);
828 return ""; /* Someone should teach Dean about null pointers */
832 /*****************************************************************************
835 **************************************************************************** */
837 md_number_to_imm (buf, val, n)
842 md_number_to_chars (buf, val, n);
846 /*****************************************************************************
849 **************************************************************************** */
851 md_number_to_disp (buf, val, n)
856 md_number_to_chars (buf, val, n);
859 /*****************************************************************************
860 * md_number_to_field:
862 * Stick a value (an address fixup) into a bit field of
863 * previously-generated instruction.
865 **************************************************************************** */
867 md_number_to_field (instrP, val, bfixP)
868 char *instrP; /* Pointer to instruction to be fixed */
869 long val; /* Address fixup value */
870 bit_fixS *bfixP; /* Description of bit field to be fixed up */
872 int numbits; /* Length of bit field to be fixed */
873 long instr; /* 32-bit instruction to be fixed-up */
874 long sign; /* 0 or -1, according to sign bit of 'val' */
876 /* Convert instruction back to host byte order
878 instr = md_chars_to_number (instrP, 4);
880 /* Surprise! -- we stored the number of bits
881 * to be modified rather than a pointer to a structure.
883 numbits = (int) bfixP;
886 /* This is a no-op, stuck here by reloc_callj() */
890 know ((numbits == 13) || (numbits == 24));
892 /* Propagate sign bit of 'val' for the given number of bits.
893 * Result should be all 0 or all 1
895 sign = val >> ((int) numbits - 1);
896 if (((val < 0) && (sign != -1))
897 || ((val > 0) && (sign != 0)))
899 as_bad ("Fixup of %d too large for field width of %d",
904 /* Put bit field into instruction and write back in target
907 val &= ~(-1 << (int) numbits); /* Clear unused sign bits */
909 md_number_to_chars (instrP, instr, 4);
911 } /* md_number_to_field() */
914 /*****************************************************************************
916 * Invocation line includes a switch not recognized by the base assembler.
917 * See if it's a processor-specific option. For the 960, these are:
920 * Conditional branch instructions that require displacements
921 * greater than 13 bits (or that have external targets) should
922 * generate errors. The default is to replace each such
923 * instruction with the corresponding compare (or chkbit) and
924 * branch instructions. Note that the Intel "j" cobr directives
925 * are ALWAYS "de-optimized" in this way when necessary,
926 * regardless of the setting of this option.
929 * Add code to collect information about branches taken, for
930 * later optimization of branch prediction bits by a separate
931 * tool. COBR and CNTL format instructions have branch
932 * prediction bits (in the CX architecture); if "BR" represents
933 * an instruction in one of these classes, the following rep-
934 * resents the code generated by the assembler:
936 * call <increment routine>
937 * .word 0 # pre-counter
939 * call <increment routine>
940 * .word 0 # post-counter
942 * A table of all such "Labels" is also generated.
945 * -AKA, -AKB, -AKC, -ASA, -ASB, -AMC, -ACA:
946 * Select the 80960 architecture. Instructions or features not
947 * supported by the selected architecture cause fatal errors.
948 * The default is to generate code for any instruction or feature
949 * that is supported by SOME version of the 960 (even if this
950 * means mixing architectures!).
952 **************************************************************************** */
954 md_parse_option (argP, cntP, vecP)
965 static struct tabentry arch_tab[] =
969 "SA", ARCH_KA, /* Synonym for KA */
970 "SB", ARCH_KB, /* Synonym for KB */
971 "KC", ARCH_MC, /* Synonym for MC */
977 if (!strcmp (*argP, "linkrelax"))
982 else if (!strcmp (*argP, "norelax"))
987 else if (**argP == 'b')
989 instrument_branches = 1;
992 else if (**argP == 'A')
996 for (tp = arch_tab; tp->flag != NULL; tp++)
998 if (!strcmp (p, tp->flag))
1004 if (tp->flag == NULL)
1006 as_bad ("unknown architecture: %s", p);
1010 architecture = tp->arch;
1015 /* Unknown option */
1019 **argP = '\0'; /* Done parsing this switch */
1023 /*****************************************************************************
1025 * Called by base assembler after address relaxation is finished: modify
1026 * variable fragments according to how much relaxation was done.
1028 * If the fragment substate is still 1, a 13-bit displacement was enough
1029 * to reach the symbol in question. Set up an address fixup, but otherwise
1030 * leave the cobr instruction alone.
1032 * If the fragment substate is 2, a 13-bit displacement was not enough.
1033 * Replace the cobr with a two instructions (a compare and a branch).
1035 **************************************************************************** */
1037 md_convert_frag (headers, fragP)
1038 object_headers *headers;
1041 fixS *fixP; /* Structure describing needed address fix */
1043 switch (fragP->fr_subtype)
1046 /* LEAVE SINGLE COBR INSTRUCTION */
1047 fixP = fix_new (fragP,
1048 fragP->fr_opcode - fragP->fr_literal,
1055 fixP->fx_bit_fixP = (bit_fixS *) 13; /* size of bit field */
1058 /* REPLACE COBR WITH COMPARE/BRANCH INSTRUCTIONS */
1062 BAD_CASE (fragP->fr_subtype);
1067 /*****************************************************************************
1068 * md_estimate_size_before_relax: How much does it look like *fragP will grow?
1070 * Called by base assembler just before address relaxation.
1071 * Return the amount by which the fragment will grow.
1073 * Any symbol that is now undefined will not become defined; cobr's
1074 * based on undefined symbols will have to be replaced with a compare
1075 * instruction and a branch instruction, and the code fragment will grow
1078 **************************************************************************** */
1080 md_estimate_size_before_relax (fragP, segment_type)
1081 register fragS *fragP;
1082 register segT segment_type;
1084 /* If symbol is undefined in this segment, go to "relaxed" state
1085 * (compare and branch instructions instead of cobr) right now.
1087 if (S_GET_SEGMENT (fragP->fr_symbol) != segment_type)
1093 } /* md_estimate_size_before_relax() */
1096 /*****************************************************************************
1098 * This routine exists in order to overcome machine byte-order problems
1099 * when dealing with bit-field entries in the relocation_info struct.
1101 * But relocation info will be used on the host machine only (only
1102 * executable code is actually downloaded to the i80960). Therefore,
1103 * we leave it in host byte order.
1105 * The above comment is no longer true. This routine now really
1106 * does do the reordering (Ian Taylor 28 Aug 92).
1108 **************************************************************************** */
1110 md_ri_to_chars (where, ri)
1112 struct relocation_info *ri;
1114 md_number_to_chars (where, ri->r_address,
1115 sizeof (ri->r_address));
1116 where[4] = ri->r_index & 0x0ff;
1117 where[5] = (ri->r_index >> 8) & 0x0ff;
1118 where[6] = (ri->r_index >> 16) & 0x0ff;
1119 where[7] = ((ri->r_pcrel << 0)
1120 | (ri->r_length << 1)
1121 | (ri->r_extern << 3)
1124 | (ri->r_callj << 6));
1125 } /* md_ri_to_chars() */
1127 #ifndef WORKING_DOT_WORD
1129 int md_short_jump_size = 0;
1130 int md_long_jump_size = 0;
1133 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
1140 as_fatal ("failed sanity check.");
1144 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
1146 addressT from_addr, to_addr;
1150 as_fatal ("failed sanity check.");
1155 /*************************************************************
1157 * FOLLOWING ARE THE LOCAL ROUTINES, IN ALPHABETICAL ORDER *
1159 ************************************************************ */
1163 /*****************************************************************************
1164 * brcnt_emit: Emit code to increment inline branch counter.
1166 * See the comments above the declaration of 'br_cnt' for details on
1167 * branch-prediction instrumentation.
1168 **************************************************************************** */
1172 ctrl_fmt (BR_CNT_FUNC, CALL, 1); /* Emit call to "increment" routine */
1173 emit (0); /* Emit inline counter to be incremented */
1176 /*****************************************************************************
1177 * brlab_next: generate the next branch local label
1179 * See the comments above the declaration of 'br_cnt' for details on
1180 * branch-prediction instrumentation.
1181 **************************************************************************** */
1185 static char buf[20];
1187 sprintf (buf, "%s%d", BR_LABEL_BASE, br_cnt++);
1191 /*****************************************************************************
1192 * brtab_emit: generate the fetch-prediction branch table.
1194 * See the comments above the declaration of 'br_cnt' for details on
1195 * branch-prediction instrumentation.
1197 * The code emitted here would be functionally equivalent to the following
1198 * example assembler source.
1203 * .word 0 # link to next table
1204 * .word 3 # length of table
1205 * .word LBRANCH0 # 1st entry in table proper
1208 ***************************************************************************** */
1214 char *p; /* Where the binary was output to */
1215 fixS *fixP; /*->description of deferred address fixup */
1217 if (!instrument_branches)
1222 subseg_new (SEG_DATA, 0); /* .data */
1223 frag_align (2, 0); /* .align 2 */
1224 record_alignment (now_seg, 2);
1225 colon (BR_TAB_NAME); /* BR_TAB_NAME: */
1226 emit (0); /* .word 0 #link to next table */
1227 emit (br_cnt); /* .word n #length of table */
1229 for (i = 0; i < br_cnt; i++)
1231 sprintf (buf, "%s%d", BR_LABEL_BASE, i);
1233 fixP = fix_new (frag_now,
1234 p - frag_now->fr_literal,
1240 fixP->fx_im_disp = 2; /* 32-bit displacement fix */
1244 /*****************************************************************************
1245 * cobr_fmt: generate a COBR-format instruction
1247 **************************************************************************** */
1250 cobr_fmt (arg, opcode, oP)
1251 char *arg[]; /* arg[0]->opcode mnemonic, arg[1-3]->operands (ascii) */
1252 long opcode; /* Opcode, with branch-prediction bits already set
1255 struct i960_opcode *oP;
1256 /*->description of instruction */
1258 long instr; /* 32-bit instruction */
1259 struct regop regop; /* Description of register operand */
1260 int n; /* Number of operands */
1261 int var_frag; /* 1 if varying length code fragment should
1262 * be emitted; 0 if an address fix
1263 * should be emitted.
1271 /* First operand (if any) of a COBR is always a register
1272 * operand. Parse it.
1274 parse_regop (®op, arg[1], oP->operand[0]);
1275 instr |= (regop.n << 19) | (regop.mode << 13);
1279 /* Second operand (if any) of a COBR is always a register
1280 * operand. Parse it.
1282 parse_regop (®op, arg[2], oP->operand[1]);
1283 instr |= (regop.n << 14) | regop.special;
1294 if (instrument_branches)
1297 colon (brlab_next ());
1300 /* A third operand to a COBR is always a displacement.
1301 * Parse it; if it's relaxable (a cobr "j" directive, or any
1302 * cobr other than bbs/bbc when the "-norelax" option is not in
1303 * use) set up a variable code fragment; otherwise set up an
1306 var_frag = !norelax || (oP->format == COJ); /* TRUE or FALSE */
1307 get_cdisp (arg[3], "COBR", instr, 13, var_frag, 0);
1309 if (instrument_branches)
1317 /*****************************************************************************
1318 * ctrl_fmt: generate a CTRL-format instruction
1320 **************************************************************************** */
1323 ctrl_fmt (targP, opcode, num_ops)
1324 char *targP; /* Pointer to text of lone operand (if any) */
1325 long opcode; /* Template of instruction */
1326 int num_ops; /* Number of operands */
1328 int instrument; /* TRUE iff we should add instrumentation to track
1329 * how often the branch is taken
1335 emit (opcode); /* Output opcode */
1340 instrument = instrument_branches && (opcode != CALL)
1341 && (opcode != B) && (opcode != RET) && (opcode != BAL);
1346 colon (brlab_next ());
1349 /* The operand MUST be an ip-relative displacment. Parse it
1350 * and set up address fix for the instruction we just output.
1352 get_cdisp (targP, "CTRL", opcode, 24, 0, 0);
1363 /*****************************************************************************
1364 * emit: output instruction binary
1366 * Output instruction binary, in target byte order, 4 bytes at a time.
1367 * Return pointer to where it was placed.
1369 **************************************************************************** */
1373 long instr; /* Word to be output, host byte order */
1375 char *toP; /* Where to output it */
1377 toP = frag_more (4); /* Allocate storage */
1378 md_number_to_chars (toP, instr, 4); /* Convert to target byte order */
1383 /*****************************************************************************
1384 * get_args: break individual arguments out of comma-separated list
1386 * Input assumptions:
1387 * - all comments and labels have been removed
1388 * - all strings of whitespace have been collapsed to a single blank.
1389 * - all character constants ('x') have been replaced with decimal
1392 * args[0] is untouched. args[1] points to first operand, etc. All args:
1393 * - are NULL-terminated
1394 * - contain no whitespace
1397 * Number of operands (0,1,2, or 3) or -1 on error.
1399 **************************************************************************** */
1402 register char *p; /* Pointer to comma-separated operands; MUCKED BY US */
1403 char *args[]; /* Output arg: pointers to operands placed in args[1-3].
1404 * MUST ACCOMMODATE 4 ENTRIES (args[0-3]).
1407 register int n; /* Number of operands */
1413 /* Skip lead white space */
1427 /* Squeze blanks out by moving non-blanks toward start of string.
1428 * Isolate operands, whenever comma is found.
1442 /* Start of operand */
1445 as_bad ("too many operands");
1448 *to++ = '\0'; /* Terminate argument */
1449 args[++n] = to; /* Start next argument */
1463 /*****************************************************************************
1464 * get_cdisp: handle displacement for a COBR or CTRL instruction.
1466 * Parse displacement for a COBR or CTRL instruction.
1468 * If successful, output the instruction opcode and set up for it,
1469 * depending on the arg 'var_frag', either:
1470 * o an address fixup to be done when all symbol values are known, or
1471 * o a varying length code fragment, with address fixup info. This
1472 * will be done for cobr instructions that may have to be relaxed
1473 * in to compare/branch instructions (8 bytes) if the final
1474 * address displacement is greater than 13 bits.
1476 *****************************************************************************/
1479 get_cdisp (dispP, ifmtP, instr, numbits, var_frag, callj)
1480 /* displacement as specified in source instruction */
1482 /* "COBR" or "CTRL" (for use in error message) */
1484 /* Instruction needing the displacement */
1486 /* # bits of displacement (13 for COBR, 24 for CTRL) */
1488 /* 1 if varying length code fragment should be emitted;
1489 * 0 if an address fix should be emitted.
1492 /* 1 if callj relocation should be done; else 0 */
1495 expressionS e; /* Parsed expression */
1496 fixS *fixP; /* Structure describing needed address fix */
1497 char *outP; /* Where instruction binary is output to */
1501 parse_expr (dispP, &e);
1505 as_bad ("expression syntax error");
1508 if (S_GET_SEGMENT (e.X_add_symbol) == text_section
1509 || S_GET_SEGMENT (e.X_add_symbol) == undefined_section)
1513 outP = frag_more (8); /* Allocate worst-case storage */
1514 md_number_to_chars (outP, instr, 4);
1515 frag_variant (rs_machine_dependent, 4, 4, 1,
1516 adds (e), offs (e), outP, 0, 0);
1520 /* Set up a new fix structure, so address can be updated
1521 * when all symbol values are known.
1523 outP = emit (instr);
1524 fixP = fix_new (frag_now,
1525 outP - frag_now->fr_literal,
1532 fixP->fx_callj = callj;
1534 /* We want to modify a bit field when the address is
1535 * known. But we don't need all the garbage in the
1536 * bit_fix structure. So we're going to lie and store
1537 * the number of bits affected instead of a pointer.
1539 fixP->fx_bit_fixP = (bit_fixS *) numbits;
1543 as_bad ("attempt to branch into different segment");
1547 as_bad ("target of %s instruction must be a label", ifmtP);
1553 /*****************************************************************************
1554 * get_ispec: parse a memory operand for an index specification
1556 * Here, an "index specification" is taken to be anything surrounded
1557 * by square brackets and NOT followed by anything else.
1559 * If it's found, detach it from the input string, remove the surrounding
1560 * square brackets, and return a pointer to it. Otherwise, return NULL.
1562 **************************************************************************** */
1566 char *textP; /*->memory operand from source instruction, no white space */
1568 char *start; /*->start of index specification */
1569 char *end; /*->end of index specification */
1571 /* Find opening square bracket, if any
1573 start = strchr (textP, '[');
1578 /* Eliminate '[', detach from rest of operand */
1581 end = strchr (start, ']');
1585 as_bad ("unmatched '['");
1590 /* Eliminate ']' and make sure it was the last thing
1594 if (*(end + 1) != '\0')
1596 as_bad ("garbage after index spec ignored");
1603 /*****************************************************************************
1606 * Look up a (suspected) register name in the register table and return the
1607 * associated register number (or -1 if not found).
1609 **************************************************************************** */
1612 get_regnum (regname)
1613 char *regname; /* Suspected register name */
1617 rP = (int *) hash_find (reg_hash, regname);
1618 return (rP == NULL) ? -1 : *rP;
1622 /*****************************************************************************
1623 * i_scan: perform lexical scan of ascii assembler instruction.
1625 * Input assumptions:
1626 * - input string is an i80960 instruction (not a pseudo-op)
1627 * - all comments and labels have been removed
1628 * - all strings of whitespace have been collapsed to a single blank.
1631 * args[0] points to opcode, other entries point to operands. All strings:
1632 * - are NULL-terminated
1633 * - contain no whitespace
1634 * - have character constants ('x') replaced with a decimal number
1637 * Number of operands (0,1,2, or 3) or -1 on error.
1639 **************************************************************************** */
1642 register char *iP; /* Pointer to ascii instruction; MUCKED BY US. */
1643 char *args[]; /* Output arg: pointers to opcode and operands placed
1644 * here. MUST ACCOMMODATE 4 ENTRIES.
1648 /* Isolate opcode */
1652 } /* Skip lead space, if any */
1654 for (; *iP != ' '; iP++)
1658 /* There are no operands */
1661 /* We never moved: there was no opcode either! */
1662 as_bad ("missing opcode");
1668 *iP++ = '\0'; /* Terminate opcode */
1669 return (get_args (iP, args));
1673 /*****************************************************************************
1674 * mem_fmt: generate a MEMA- or MEMB-format instruction
1676 **************************************************************************** */
1678 mem_fmt (args, oP, callx)
1679 char *args[]; /* args[0]->opcode mnemonic, args[1-3]->operands */
1680 struct i960_opcode *oP; /* Pointer to description of instruction */
1681 int callx; /* Is this a callx opcode */
1683 int i; /* Loop counter */
1684 struct regop regop; /* Description of register operand */
1685 char opdesc; /* Operand descriptor byte */
1686 memS instr; /* Description of binary to be output */
1687 char *outP; /* Where the binary was output to */
1688 expressionS expr; /* Parsed expression */
1689 fixS *fixP; /*->description of deferred address fixup */
1691 memset (&instr, '\0', sizeof (memS));
1692 instr.opcode = oP->opcode;
1694 /* Process operands. */
1695 for (i = 1; i <= oP->num_ops; i++)
1697 opdesc = oP->operand[i - 1];
1701 parse_memop (&instr, args[i], oP->format);
1705 parse_regop (®op, args[i], opdesc);
1706 instr.opcode |= regop.n << 19;
1711 outP = emit (instr.opcode);
1713 if (instr.disp == 0)
1718 /* Parse and process the displacement */
1719 parse_expr (instr.e, &expr);
1723 as_bad ("expression syntax error");
1727 if (instr.disp == 32)
1729 (void) emit (offs (expr)); /* Output displacement */
1733 /* 12-bit displacement */
1734 if (offs (expr) & ~0xfff)
1736 /* Won't fit in 12 bits: convert already-output
1737 * instruction to MEMB format, output
1740 mema_to_memb (outP);
1741 (void) emit (offs (expr));
1745 /* WILL fit in 12 bits: OR into opcode and
1746 * overwrite the binary we already put out
1748 instr.opcode |= offs (expr);
1749 md_number_to_chars (outP, instr.opcode, 4);
1755 if (instr.disp == 12)
1757 /* Displacement is dependent on a symbol, whose value
1758 * may change at link time. We HAVE to reserve 32 bits.
1759 * Convert already-output opcode to MEMB format.
1761 mema_to_memb (outP);
1764 /* Output 0 displacement and set up address fixup for when
1765 * this symbol's value becomes known.
1767 outP = emit ((long) 0);
1768 fixP = fix_new_exp (frag_now,
1769 outP - frag_now->fr_literal,
1774 fixP->fx_im_disp = 2; /* 32-bit displacement fix */
1775 fixP->fx_bsr = callx; /*SAC LD RELAX HACK *//* Mark reloc as being in i stream */
1781 /*****************************************************************************
1782 * mema_to_memb: convert a MEMA-format opcode to a MEMB-format opcode.
1784 * There are 2 possible MEMA formats:
1785 * - displacement only
1786 * - displacement + abase
1788 * They are distinguished by the setting of the MEMA_ABASE bit.
1790 **************************************************************************** */
1792 mema_to_memb (opcodeP)
1793 char *opcodeP; /* Where to find the opcode, in target byte order */
1795 long opcode; /* Opcode in host byte order */
1796 long mode; /* Mode bits for MEMB instruction */
1798 opcode = md_chars_to_number (opcodeP, 4);
1799 know (!(opcode & MEMB_BIT));
1801 mode = MEMB_BIT | D_BIT;
1802 if (opcode & MEMA_ABASE)
1807 opcode &= 0xffffc000; /* Clear MEMA offset and mode bits */
1808 opcode |= mode; /* Set MEMB mode bits */
1810 md_number_to_chars (opcodeP, opcode, 4);
1811 } /* mema_to_memb() */
1814 /*****************************************************************************
1815 * parse_expr: parse an expression
1817 * Use base assembler's expression parser to parse an expression.
1818 * It, unfortunately, runs off a global which we have to save/restore
1819 * in order to make it work for us.
1821 * An empty expression string is treated as an absolute 0.
1823 * Sets O_illegal regardless of expression evaluation if entire input
1824 * string is not consumed in the evaluation -- tolerate no dangling junk!
1826 **************************************************************************** */
1828 parse_expr (textP, expP)
1829 char *textP; /* Text of expression to be parsed */
1830 expressionS *expP; /* Where to put the results of parsing */
1832 char *save_in; /* Save global here */
1839 /* Treat empty string as absolute 0 */
1840 expP->X_add_symbol = expP->X_op_symbol = NULL;
1841 expP->X_add_number = 0;
1842 exp->X_op = O_constant;
1846 save_in = input_line_pointer; /* Save global */
1847 input_line_pointer = textP; /* Make parser work for us */
1849 seg = expression (expP);
1850 if (input_line_pointer - textP != strlen (textP))
1852 /* Did not consume all of the input */
1853 expP->X_op = O_illegal;
1855 symP = expP->X_add_symbol;
1856 if (symP && (hash_find (reg_hash, S_GET_NAME (symP))))
1858 /* Register name in an expression */
1859 /* FIXME: this isn't much of a check any more. */
1860 expP->X_op = O_illegal;
1863 input_line_pointer = save_in; /* Restore global */
1869 /*****************************************************************************
1871 * Parse and replace a 'ldconst' pseudo-instruction with an appropriate
1872 * i80960 instruction.
1874 * Assumes the input consists of:
1875 * arg[0] opcode mnemonic ('ldconst')
1876 * arg[1] first operand (constant)
1877 * arg[2] name of register to be loaded
1879 * Replaces opcode and/or operands as appropriate.
1881 * Returns the new number of arguments, or -1 on failure.
1883 **************************************************************************** */
1887 char *arg[]; /* See above */
1889 int n; /* Constant to be loaded */
1890 int shift; /* Shift count for "shlo" instruction */
1891 static char buf[5]; /* Literal for first operand */
1892 static char buf2[5]; /* Literal for second operand */
1893 expressionS e; /* Parsed expression */
1896 arg[3] = NULL; /* So we can tell at the end if it got used or not */
1898 parse_expr (arg[1], &e);
1902 /* We're dependent on one or more symbols -- use "lda" */
1907 /* Try the following mappings:
1908 * ldconst 0,<reg> ->mov 0,<reg>
1909 * ldconst 31,<reg> ->mov 31,<reg>
1910 * ldconst 32,<reg> ->addo 1,31,<reg>
1911 * ldconst 62,<reg> ->addo 31,31,<reg>
1912 * ldconst 64,<reg> ->shlo 8,3,<reg>
1913 * ldconst -1,<reg> ->subo 1,0,<reg>
1914 * ldconst -31,<reg>->subo 31,0,<reg>
1916 * anthing else becomes:
1920 if ((0 <= n) && (n <= 31))
1925 else if ((-31 <= n) && (n <= -1))
1929 sprintf (buf, "%d", -n);
1934 else if ((32 <= n) && (n <= 62))
1939 sprintf (buf, "%d", n - 31);
1943 else if ((shift = shift_ok (n)) != 0)
1947 sprintf (buf, "%d", shift);
1949 sprintf (buf2, "%d", n >> shift);
1960 as_bad ("invalid constant");
1964 return (arg[3] == 0) ? 2 : 3;
1967 /*****************************************************************************
1968 * parse_memop: parse a memory operand
1970 * This routine is based on the observation that the 4 mode bits of the
1971 * MEMB format, taken individually, have fairly consistent meaning:
1973 * M3 (bit 13): 1 if displacement is present (D_BIT)
1974 * M2 (bit 12): 1 for MEMB instructions (MEMB_BIT)
1975 * M1 (bit 11): 1 if index is present (I_BIT)
1976 * M0 (bit 10): 1 if abase is present (A_BIT)
1978 * So we parse the memory operand and set bits in the mode as we find
1979 * things. Then at the end, if we go to MEMB format, we need only set
1980 * the MEMB bit (M2) and our mode is built for us.
1982 * Unfortunately, I said "fairly consistent". The exceptions:
1985 * 0100 Would seem illegal, but means "abase-only".
1987 * 0101 Would seem to mean "abase-only" -- it means IP-relative.
1988 * Must be converted to 0100.
1990 * 0110 Would seem to mean "index-only", but is reserved.
1991 * We turn on the D bit and provide a 0 displacement.
1993 * The other thing to observe is that we parse from the right, peeling
1994 * things * off as we go: first any index spec, then any abase, then
1997 **************************************************************************** */
2000 parse_memop (memP, argP, optype)
2001 memS *memP; /* Where to put the results */
2002 char *argP; /* Text of the operand to be parsed */
2003 int optype; /* MEM1, MEM2, MEM4, MEM8, MEM12, or MEM16 */
2005 char *indexP; /* Pointer to index specification with "[]" removed */
2006 char *p; /* Temp char pointer */
2007 char iprel_flag; /* True if this is an IP-relative operand */
2008 int regnum; /* Register number */
2009 int scale; /* Scale factor: 1,2,4,8, or 16. Later converted
2010 * to internal format (0,1,2,3,4 respectively).
2012 int mode; /* MEMB mode bits */
2013 int *intP; /* Pointer to register number */
2015 /* The following table contains the default scale factors for each
2016 * type of memory instruction. It is accessed using (optype-MEM1)
2017 * as an index -- thus it assumes the 'optype' constants are assigned
2018 * consecutive values, in the order they appear in this table
2020 static int def_scale[] =
2026 -1, /* MEM12 -- no valid default */
2031 iprel_flag = mode = 0;
2033 /* Any index present? */
2034 indexP = get_ispec (argP);
2037 p = strchr (indexP, '*');
2040 /* No explicit scale -- use default for this
2043 scale = def_scale[optype - MEM1];
2047 *p++ = '\0'; /* Eliminate '*' */
2049 /* Now indexP->a '\0'-terminated register name,
2050 * and p->a scale factor.
2053 if (!strcmp (p, "16"))
2057 else if (strchr ("1248", *p) && (p[1] == '\0'))
2067 regnum = get_regnum (indexP); /* Get index reg. # */
2068 if (!IS_RG_REG (regnum))
2070 as_bad ("invalid index register");
2074 /* Convert scale to its binary encoding */
2093 as_bad ("invalid scale factor");
2097 memP->opcode |= scale | regnum; /* Set index bits in opcode */
2098 mode |= I_BIT; /* Found a valid index spec */
2101 /* Any abase (Register Indirect) specification present? */
2102 if ((p = strrchr (argP, '(')) != NULL)
2104 /* "(" is there -- does it start a legal abase spec?
2105 * (If not it could be part of a displacement expression.)
2107 intP = (int *) hash_find (areg_hash, p);
2110 /* Got an abase here */
2112 *p = '\0'; /* discard register spec */
2113 if (regnum == IPREL)
2115 /* We have to specialcase ip-rel mode */
2120 memP->opcode |= regnum << 14;
2126 /* Any expression present? */
2133 /* Special-case ip-relative addressing */
2142 memP->opcode |= 5 << 10; /* IP-relative mode */
2148 /* Handle all other modes */
2152 /* Go with MEMA instruction format for now (grow to MEMB later
2153 * if 12 bits is not enough for the displacement).
2154 * MEMA format has a single mode bit: set it to indicate
2155 * that abase is present.
2157 memP->opcode |= MEMA_ABASE;
2162 /* Go with MEMA instruction format for now (grow to MEMB later
2163 * if 12 bits is not enough for the displacement).
2169 /* For some reason, the bit string for this mode is not
2170 * consistent: it should be 0 (exclusive of the MEMB bit),
2171 * so we set it "by hand" here.
2173 memP->opcode |= MEMB_BIT;
2177 /* set MEMB bit in mode, and OR in mode bits */
2178 memP->opcode |= mode | MEMB_BIT;
2182 /* Treat missing displacement as displacement of 0 */
2184 /***********************
2185 * Fall into next case *
2186 ********************** */
2187 case D_BIT | A_BIT | I_BIT:
2189 /* set MEMB bit in mode, and OR in mode bits */
2190 memP->opcode |= mode | MEMB_BIT;
2200 /*****************************************************************************
2201 * parse_po: parse machine-dependent pseudo-op
2203 * This is a top-level routine for machine-dependent pseudo-ops. It slurps
2204 * up the rest of the input line, breaks out the individual arguments,
2205 * and dispatches them to the correct handler.
2206 **************************************************************************** */
2210 int po_num; /* Pseudo-op number: currently S_LEAFPROC or S_SYSPROC */
2212 char *args[4]; /* Pointers operands, with no embedded whitespace.
2214 * arg[1-3]->operands
2216 int n_ops; /* Number of operands */
2217 char *p; /* Pointer to beginning of unparsed argument string */
2218 char eol; /* Character that indicated end of line */
2220 extern char is_end_of_line[];
2222 /* Advance input pointer to end of line. */
2223 p = input_line_pointer;
2224 while (!is_end_of_line[*input_line_pointer])
2226 input_line_pointer++;
2228 eol = *input_line_pointer; /* Save end-of-line char */
2229 *input_line_pointer = '\0'; /* Terminate argument list */
2231 /* Parse out operands */
2232 n_ops = get_args (p, args);
2238 /* Dispatch to correct handler */
2242 s_sysproc (n_ops, args);
2245 s_leafproc (n_ops, args);
2252 /* Restore eol, so line numbers get updated correctly. Base assembler
2253 * assumes we leave input pointer pointing at char following the eol.
2255 *input_line_pointer++ = eol;
2258 /*****************************************************************************
2259 * parse_regop: parse a register operand.
2261 * In case of illegal operand, issue a message and return some valid
2262 * information so instruction processing can continue.
2263 **************************************************************************** */
2266 parse_regop (regopP, optext, opdesc)
2267 struct regop *regopP; /* Where to put description of register operand */
2268 char *optext; /* Text of operand */
2269 char opdesc; /* Descriptor byte: what's legal for this operand */
2271 int n; /* Register number */
2272 expressionS e; /* Parsed expression */
2274 /* See if operand is a register */
2275 n = get_regnum (optext);
2280 /* global or local register */
2281 if (!REG_ALIGN (opdesc, n))
2283 as_bad ("unaligned register");
2287 regopP->special = 0;
2290 else if (IS_FP_REG (n) && FP_OK (opdesc))
2292 /* Floating point register, and it's allowed */
2293 regopP->n = n - FP0;
2295 regopP->special = 0;
2298 else if (IS_SF_REG (n) && SFR_OK (opdesc))
2300 /* Special-function register, and it's allowed */
2301 regopP->n = n - SF0;
2303 regopP->special = 1;
2304 if (!targ_has_sfr (regopP->n))
2306 as_bad ("no such sfr in this architecture");
2311 else if (LIT_OK (opdesc))
2314 * How about a literal?
2317 regopP->special = 0;
2319 { /* floating point literal acceptable */
2320 /* Skip over 0f, 0d, or 0e prefix */
2321 if ((optext[0] == '0')
2322 && (optext[1] >= 'd')
2323 && (optext[1] <= 'f'))
2328 if (!strcmp (optext, "0.0") || !strcmp (optext, "0"))
2333 if (!strcmp (optext, "1.0") || !strcmp (optext, "1"))
2341 { /* fixed point literal acceptable */
2342 parse_expr (optext, &e);
2343 if (e.X_op != O_constant
2344 || (offs (e) < 0) || (offs (e) > 31))
2346 as_bad ("illegal literal");
2349 regopP->n = offs (e);
2354 /* Nothing worked */
2356 regopP->mode = 0; /* Register r0 is always a good one */
2358 regopP->special = 0;
2359 } /* parse_regop() */
2361 /*****************************************************************************
2362 * reg_fmt: generate a REG-format instruction
2364 **************************************************************************** */
2367 char *args[]; /* args[0]->opcode mnemonic, args[1-3]->operands */
2368 struct i960_opcode *oP; /* Pointer to description of instruction */
2370 long instr; /* Binary to be output */
2371 struct regop regop; /* Description of register operand */
2372 int n_ops; /* Number of operands */
2376 n_ops = oP->num_ops;
2380 parse_regop (®op, args[1], oP->operand[0]);
2382 if ((n_ops == 1) && !(instr & M3))
2384 /* 1-operand instruction in which the dst field should
2385 * be used (instead of src1).
2390 regop.mode = regop.special;
2397 /* regop.n goes in bit 0, needs no shifting */
2399 regop.special <<= 5;
2401 instr |= regop.n | regop.mode | regop.special;
2406 parse_regop (®op, args[2], oP->operand[1]);
2408 if ((n_ops == 2) && !(instr & M3))
2410 /* 2-operand instruction in which the dst field should
2411 * be used instead of src2).
2416 regop.mode = regop.special;
2425 regop.special <<= 6;
2427 instr |= regop.n | regop.mode | regop.special;
2431 parse_regop (®op, args[3], oP->operand[2]);
2434 regop.mode = regop.special;
2436 instr |= (regop.n <<= 19) | (regop.mode <<= 13);
2442 /*****************************************************************************
2444 * Replace cobr instruction in a code fragment with equivalent branch and
2445 * compare instructions, so it can reach beyond a 13-bit displacement.
2446 * Set up an address fix/relocation for the new branch instruction.
2448 **************************************************************************** */
2450 /* This "conditional jump" table maps cobr instructions into equivalent
2451 * compare and branch opcodes.
2461 { /* COBR OPCODE: */
2462 CHKBIT, BNO, /* 0x30 - bbc */
2463 CMPO, BG, /* 0x31 - cmpobg */
2464 CMPO, BE, /* 0x32 - cmpobe */
2465 CMPO, BGE, /* 0x33 - cmpobge */
2466 CMPO, BL, /* 0x34 - cmpobl */
2467 CMPO, BNE, /* 0x35 - cmpobne */
2468 CMPO, BLE, /* 0x36 - cmpoble */
2469 CHKBIT, BO, /* 0x37 - bbs */
2470 CMPI, BNO, /* 0x38 - cmpibno */
2471 CMPI, BG, /* 0x39 - cmpibg */
2472 CMPI, BE, /* 0x3a - cmpibe */
2473 CMPI, BGE, /* 0x3b - cmpibge */
2474 CMPI, BL, /* 0x3c - cmpibl */
2475 CMPI, BNE, /* 0x3d - cmpibne */
2476 CMPI, BLE, /* 0x3e - cmpible */
2477 CMPI, BO, /* 0x3f - cmpibo */
2483 register fragS *fragP; /* fragP->fr_opcode is assumed to point to
2484 * the cobr instruction, which comes at the
2485 * end of the code fragment.
2488 int opcode, src1, src2, m1, s2;
2489 /* Bit fields from cobr instruction */
2490 long bp_bits; /* Branch prediction bits from cobr instruction */
2491 long instr; /* A single i960 instruction */
2492 char *iP; /*->instruction to be replaced */
2493 fixS *fixP; /* Relocation that can be done at assembly time */
2495 /* PICK UP & PARSE COBR INSTRUCTION */
2496 iP = fragP->fr_opcode;
2497 instr = md_chars_to_number (iP, 4);
2498 opcode = ((instr >> 24) & 0xff) - 0x30; /* "-0x30" for table index */
2499 src1 = (instr >> 19) & 0x1f;
2500 m1 = (instr >> 13) & 1;
2502 src2 = (instr >> 14) & 0x1f;
2503 bp_bits = instr & BP_MASK;
2505 /* GENERATE AND OUTPUT COMPARE INSTRUCTION */
2506 instr = coj[opcode].compare
2507 | src1 | (m1 << 11) | (s2 << 6) | (src2 << 14);
2508 md_number_to_chars (iP, instr, 4);
2510 /* OUTPUT BRANCH INSTRUCTION */
2511 md_number_to_chars (iP + 4, coj[opcode].branch | bp_bits, 4);
2513 /* SET UP ADDRESS FIXUP/RELOCATION */
2514 fixP = fix_new (fragP,
2515 iP + 4 - fragP->fr_literal,
2522 fixP->fx_bit_fixP = (bit_fixS *) 24; /* Store size of bit field */
2529 /*****************************************************************************
2530 * reloc_callj: Relocate a 'callj' instruction
2532 * This is a "non-(GNU)-standard" machine-dependent hook. The base
2533 * assembler calls it when it decides it can relocate an address at
2534 * assembly time instead of emitting a relocation directive.
2536 * Check to see if the relocation involves a 'callj' instruction to a:
2537 * sysproc: Replace the default 'call' instruction with a 'calls'
2538 * leafproc: Replace the default 'call' instruction with a 'bal'.
2539 * other proc: Do nothing.
2541 * See b.out.h for details on the 'n_other' field in a symbol structure.
2544 * Assumes the caller has already figured out, in the case of a leafproc,
2545 * to use the 'bal' entry point, and has substituted that symbol into the
2546 * passed fixup structure.
2548 **************************************************************************** */
2551 fixS *fixP; /* Relocation that can be done at assembly time */
2553 char *where; /*->the binary for the instruction being relocated */
2555 if (!fixP->fx_callj)
2558 } /* This wasn't a callj instruction in the first place */
2560 where = fixP->fx_frag->fr_literal + fixP->fx_where;
2562 if (TC_S_IS_SYSPROC (fixP->fx_addsy))
2564 /* Symbol is a .sysproc: replace 'call' with 'calls'.
2565 * System procedure number is (other-1).
2567 md_number_to_chars (where, CALLS | TC_S_GET_SYSPROC (fixP->fx_addsy), 4);
2569 /* Nothing else needs to be done for this instruction.
2570 * Make sure 'md_number_to_field()' will perform a no-op.
2572 fixP->fx_bit_fixP = (bit_fixS *) 1;
2575 else if (TC_S_IS_CALLNAME (fixP->fx_addsy))
2577 /* Should not happen: see block comment above */
2578 as_fatal ("Trying to 'bal' to %s", S_GET_NAME (fixP->fx_addsy));
2581 else if (TC_S_IS_BALNAME (fixP->fx_addsy))
2583 /* Replace 'call' with 'bal'; both instructions have
2584 * the same format, so calling code should complete
2585 * relocation as if nothing happened here.
2587 md_number_to_chars (where, BAL, 4);
2589 else if (TC_S_IS_BADPROC (fixP->fx_addsy))
2591 as_bad ("Looks like a proc, but can't tell what kind.\n");
2592 } /* switch on proc type */
2594 /* else Symbol is neither a sysproc nor a leafproc */
2597 } /* reloc_callj() */
2600 /*****************************************************************************
2601 * s_leafproc: process .leafproc pseudo-op
2603 * .leafproc takes two arguments, the second one is optional:
2604 * arg[1]: name of 'call' entry point to leaf procedure
2605 * arg[2]: name of 'bal' entry point to leaf procedure
2607 * If the two arguments are identical, or if the second one is missing,
2608 * the first argument is taken to be the 'bal' entry point.
2610 * If there are 2 distinct arguments, we must make sure that the 'bal'
2611 * entry point immediately follows the 'call' entry point in the linked
2614 **************************************************************************** */
2616 s_leafproc (n_ops, args)
2617 int n_ops; /* Number of operands */
2618 char *args[]; /* args[1]->1st operand, args[2]->2nd operand */
2620 symbolS *callP; /* Pointer to leafproc 'call' entry point symbol */
2621 symbolS *balP; /* Pointer to leafproc 'bal' entry point symbol */
2623 if ((n_ops != 1) && (n_ops != 2))
2625 as_bad ("should have 1 or 2 operands");
2627 } /* Check number of arguments */
2629 /* Find or create symbol for 'call' entry point. */
2630 callP = symbol_find_or_make (args[1]);
2632 if (TC_S_IS_CALLNAME (callP))
2634 as_warn ("Redefining leafproc %s", S_GET_NAME (callP));
2637 /* If that was the only argument, use it as the 'bal' entry point.
2638 * Otherwise, mark it as the 'call' entry point and find or create
2639 * another symbol for the 'bal' entry point.
2641 if ((n_ops == 1) || !strcmp (args[1], args[2]))
2643 TC_S_FORCE_TO_BALNAME (callP);
2648 TC_S_FORCE_TO_CALLNAME (callP);
2650 balP = symbol_find_or_make (args[2]);
2651 if (TC_S_IS_CALLNAME (balP))
2653 as_warn ("Redefining leafproc %s", S_GET_NAME (balP));
2655 TC_S_FORCE_TO_BALNAME (balP);
2657 tc_set_bal_of_call (callP, balP);
2658 } /* if only one arg, or the args are the same */
2661 } /* s_leafproc() */
2665 * s_sysproc: process .sysproc pseudo-op
2667 * .sysproc takes two arguments:
2668 * arg[1]: name of entry point to system procedure
2669 * arg[2]: 'entry_num' (index) of system procedure in the range
2672 * For [ab].out, we store the 'entrynum' in the 'n_other' field of
2673 * the symbol. Since that entry is normally 0, we bias 'entrynum'
2674 * by adding 1 to it. It must be unbiased before it is used.
2677 s_sysproc (n_ops, args)
2678 int n_ops; /* Number of operands */
2679 char *args[]; /* args[1]->1st operand, args[2]->2nd operand */
2686 as_bad ("should have two operands");
2688 } /* bad arg count */
2690 /* Parse "entry_num" argument and check it for validity. */
2691 parse_expr (args[2], &exp);
2692 if (exp.X_op != O_constant
2694 || (offs (exp) > 31))
2696 as_bad ("'entry_num' must be absolute number in [0,31]");
2700 /* Find/make symbol and stick entry number (biased by +1) into it */
2701 symP = symbol_find_or_make (args[1]);
2703 if (TC_S_IS_SYSPROC (symP))
2705 as_warn ("Redefining entrynum for sysproc %s", S_GET_NAME (symP));
2708 TC_S_SET_SYSPROC (symP, offs (exp)); /* encode entry number */
2709 TC_S_FORCE_TO_SYSPROC (symP);
2715 /*****************************************************************************
2717 * Determine if a "shlo" instruction can be used to implement a "ldconst".
2718 * This means that some number X < 32 can be shifted left to produce the
2719 * constant of interest.
2721 * Return the shift count, or 0 if we can't do it.
2722 * Caller calculates X by shifting original constant right 'shift' places.
2724 **************************************************************************** */
2728 int n; /* The constant of interest */
2730 int shift; /* The shift count */
2734 /* Can't do it for negative numbers */
2738 /* Shift 'n' right until a 1 is about to be lost */
2739 for (shift = 0; (n & 1) == 0; shift++)
2752 /*****************************************************************************
2753 * syntax: issue syntax error
2755 **************************************************************************** */
2759 as_bad ("syntax error");
2763 /*****************************************************************************
2765 * Return TRUE iff the target architecture supports the specified
2766 * special-function register (sfr).
2768 **************************************************************************** */
2772 int n; /* Number (0-31) of sfr */
2774 switch (architecture)
2782 return ((0 <= n) && (n <= 2));
2787 /*****************************************************************************
2789 * Return TRUE iff the target architecture supports the indicated
2790 * class of instructions.
2792 **************************************************************************** */
2795 targ_has_iclass (ic)
2796 int ic; /* Instruction class; one of:
2797 * I_BASE, I_CX, I_DEC, I_KX, I_FP, I_MIL, I_CASIM
2800 iclasses_seen |= ic;
2801 switch (architecture)
2804 return ic & (I_BASE | I_KX);
2806 return ic & (I_BASE | I_KX | I_FP | I_DEC);
2808 return ic & (I_BASE | I_KX | I_FP | I_DEC | I_MIL);
2810 return ic & (I_BASE | I_CX | I_CASIM);
2812 if ((iclasses_seen & (I_KX | I_FP | I_DEC | I_MIL))
2813 && (iclasses_seen & I_CX))
2815 as_warn ("architecture of opcode conflicts with that of earlier instruction(s)");
2816 iclasses_seen &= ~ic;
2823 /* Parse an operand that is machine-specific.
2824 We just return without modifying the expression if we have nothing
2829 md_operand (expressionP)
2830 expressionS *expressionP;
2834 /* We have no need to default values of symbols. */
2838 md_undefined_symbol (name)
2842 } /* md_undefined_symbol() */
2844 /* Exactly what point is a PC-relative offset relative TO?
2845 On the i960, they're relative to the address of the instruction,
2846 which we have set up as the address of the fixup too. */
2848 md_pcrel_from (fixP)
2851 return fixP->fx_where + fixP->fx_frag->fr_address;
2855 md_apply_fix (fixP, val)
2859 char *place = fixP->fx_where + fixP->fx_frag->fr_literal;
2861 if (!fixP->fx_bit_fixP)
2864 switch (fixP->fx_im_disp)
2867 fixP->fx_addnumber = val;
2868 md_number_to_imm (place, val, fixP->fx_size, fixP);
2871 md_number_to_disp (place,
2873 ? val + fixP->fx_pcrel_adjust
2877 case 2: /* fix requested for .long .word etc */
2878 md_number_to_chars (place, val, fixP->fx_size);
2881 as_fatal ("Internal error in md_apply_fix() in file \"%s\"",
2887 md_number_to_field (place, val, fixP->fx_bit_fixP);
2891 } /* md_apply_fix() */
2893 #if defined(OBJ_AOUT) | defined(OBJ_BOUT)
2895 tc_bout_fix_to_chars (where, fixP, segment_address_in_file)
2898 relax_addressT segment_address_in_file;
2900 static unsigned char nbytes_r_length[] =
2902 struct relocation_info ri;
2905 /* JF this is for paranoia */
2906 memset ((char *) &ri, '\0', sizeof (ri));
2907 symbolP = fixP->fx_addsy;
2908 know (symbolP != 0 || fixP->fx_r_type != NO_RELOC);
2909 ri.r_bsr = fixP->fx_bsr; /*SAC LD RELAX HACK */
2910 /* These two 'cuz of NS32K */
2911 ri.r_callj = fixP->fx_callj;
2912 if (fixP->fx_bit_fixP)
2918 ri.r_length = nbytes_r_length[fixP->fx_size];
2920 ri.r_pcrel = fixP->fx_pcrel;
2921 ri.r_address = fixP->fx_frag->fr_address + fixP->fx_where - segment_address_in_file;
2923 if (fixP->fx_r_type != NO_RELOC)
2925 switch (fixP->fx_r_type)
2930 ri.r_length = fixP->fx_size - 1;
2944 else if (linkrelax || !S_IS_DEFINED (symbolP))
2947 ri.r_index = symbolP->sy_number;
2952 ri.r_index = S_GET_TYPE (symbolP);
2955 /* Output the relocation information in machine-dependent form. */
2956 md_ri_to_chars (where, &ri);
2959 } /* tc_bout_fix_to_chars() */
2961 #endif /* OBJ_AOUT or OBJ_BOUT */
2963 /* Align an address by rounding it up to the specified boundary.
2966 md_section_align (seg, addr)
2968 valueT addr; /* Address to be rounded up */
2970 return ((addr + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg]));
2971 } /* md_section_align() */
2975 tc_headers_hook (headers)
2976 object_headers *headers;
2978 /* FIXME: remove this line *//* unsigned short arch_flag = 0; */
2980 if (iclasses_seen == I_BASE)
2982 headers->filehdr.f_flags |= F_I960CORE;
2984 else if (iclasses_seen & I_CX)
2986 headers->filehdr.f_flags |= F_I960CA;
2988 else if (iclasses_seen & I_MIL)
2990 headers->filehdr.f_flags |= F_I960MC;
2992 else if (iclasses_seen & (I_DEC | I_FP))
2994 headers->filehdr.f_flags |= F_I960KB;
2998 headers->filehdr.f_flags |= F_I960KA;
2999 } /* set arch flag */
3003 headers->filehdr.f_magic = I960RWMAGIC;
3004 headers->aouthdr.magic = OMAGIC;
3008 headers->filehdr.f_magic = I960ROMAGIC;
3009 headers->aouthdr.magic = NMAGIC;
3010 } /* set magic numbers */
3013 } /* tc_headers_hook() */
3015 #endif /* OBJ_COFF */
3018 * Things going on here:
3020 * For bout, We need to assure a couple of simplifying
3021 * assumptions about leafprocs for the linker: the leafproc
3022 * entry symbols will be defined in the same assembly in
3023 * which they're declared with the '.leafproc' directive;
3024 * and if a leafproc has both 'call' and 'bal' entry points
3025 * they are both global or both local.
3027 * For coff, the call symbol has a second aux entry that
3028 * contains the bal entry point. The bal symbol becomes a
3031 * For coff representation, the call symbol has a second aux entry that
3032 * contains the bal entry point. The bal symbol becomes a label.
3037 tc_crawl_symbol_chain (headers)
3038 object_headers *headers;
3042 for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
3045 if (TC_S_IS_SYSPROC (symbolP))
3047 /* second aux entry already contains the sysproc number */
3048 S_SET_NUMBER_AUXILIARY (symbolP, 2);
3049 S_SET_STORAGE_CLASS (symbolP, C_SCALL);
3050 S_SET_DATA_TYPE (symbolP, S_GET_DATA_TYPE (symbolP) | (DT_FCN << N_BTSHFT));
3052 } /* rewrite sysproc */
3053 #endif /* OBJ_COFF */
3055 if (!TC_S_IS_BALNAME (symbolP) && !TC_S_IS_CALLNAME (symbolP))
3058 } /* Not a leafproc symbol */
3060 if (!S_IS_DEFINED (symbolP))
3062 as_bad ("leafproc symbol '%s' undefined", S_GET_NAME (symbolP));
3063 } /* undefined leaf */
3065 if (TC_S_IS_CALLNAME (symbolP))
3067 symbolS *balP = tc_get_bal_of_call (symbolP);
3068 if (S_IS_EXTERNAL (symbolP) != S_IS_EXTERNAL (balP))
3070 S_SET_EXTERNAL (symbolP);
3071 S_SET_EXTERNAL (balP);
3072 as_warn ("Warning: making leafproc entries %s and %s both global\n",
3073 S_GET_NAME (symbolP), S_GET_NAME (balP));
3074 } /* externality mismatch */
3076 } /* walk the symbol chain */
3079 } /* tc_crawl_symbol_chain() */
3082 * For aout or bout, the bal immediately follows the call.
3084 * For coff, we cheat and store a pointer to the bal symbol
3085 * in the second aux entry of the call.
3097 tc_set_bal_of_call (callP, balP)
3101 know (TC_S_IS_CALLNAME (callP));
3102 know (TC_S_IS_BALNAME (balP));
3106 callP->sy_symbol.ost_auxent[1].x_bal.x_balntry = (int) balP;
3107 S_SET_NUMBER_AUXILIARY (callP, 2);
3109 #else /* ! OBJ_COFF */
3112 /* If the 'bal' entry doesn't immediately follow the 'call'
3113 * symbol, unlink it from the symbol list and re-insert it.
3115 if (symbol_next (callP) != balP)
3117 symbol_remove (balP, &symbol_rootP, &symbol_lastP);
3118 symbol_append (balP, callP, &symbol_rootP, &symbol_lastP);
3119 } /* if not in order */
3121 #else /* ! OBJ_ABOUT */
3122 (as yet unwritten.);
3123 #endif /* ! OBJ_ABOUT */
3124 #endif /* ! OBJ_COFF */
3127 } /* tc_set_bal_of_call() */
3130 _tc_get_bal_of_call (callP)
3135 know (TC_S_IS_CALLNAME (callP));
3138 retval = (symbolS *) (callP->sy_symbol.ost_auxent[1].x_bal.x_balntry);
3141 retval = symbol_next (callP);
3143 (as yet unwritten.);
3144 #endif /* ! OBJ_ABOUT */
3145 #endif /* ! OBJ_COFF */
3147 know (TC_S_IS_BALNAME (retval));
3148 return ((char *) retval);
3149 } /* _tc_get_bal_of_call() */
3152 tc_coff_symbol_emit_hook (symbolP)
3155 if (TC_S_IS_CALLNAME (symbolP))
3158 symbolS *balP = tc_get_bal_of_call (symbolP);
3160 /* second aux entry contains the bal entry point */
3161 /* S_SET_NUMBER_AUXILIARY(symbolP, 2); */
3162 symbolP->sy_symbol.ost_auxent[1].x_bal.x_balntry = S_GET_VALUE (balP);
3163 S_SET_STORAGE_CLASS (symbolP, (!SF_GET_LOCAL (symbolP) ? C_LEAFEXT : C_LEAFSTAT));
3164 S_SET_DATA_TYPE (symbolP, S_GET_DATA_TYPE (symbolP) | (DT_FCN << N_BTSHFT));
3165 /* fix up the bal symbol */
3166 S_SET_STORAGE_CLASS (balP, C_LABEL);
3167 #endif /* OBJ_COFF */
3168 } /* only on calls */
3171 } /* tc_coff_symbol_emit_hook() */
3174 i960_handle_align (fragp)
3178 segT old_seg = now_seg, this_seg;
3179 int old_subseg = now_subseg;
3181 extern struct frag *text_last_frag, *data_last_frag;
3186 /* The text section "ends" with another alignment reloc, to which we
3187 aren't adding padding. */
3188 if (fragp->fr_next == text_last_frag
3189 || fragp->fr_next == data_last_frag)
3194 /* alignment directive */
3195 fixp = fix_new (fragp, fragp->fr_fix, fragp->fr_offset, 0, 0, 0,
3196 (int) fragp->fr_type);
3199 /* end of tc-i960.c */