Ran "indent", for GNU coding style; some code & comments still need fixup.
[external/binutils.git] / gas / config / tc-i960.c
1 /* tc-i960.c - All the i80960-specific stuff
2    Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
3
4    This file is part of GAS.
5
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)
9    any later version.
10
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.
15
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.  */
19
20 /* See comment on md_parse_option for 80960-specific invocation options. */
21
22 /******************************************************************************
23  * i80690 NOTE!!!:
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.
27  *
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.)
31  *
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.  <==
35  *
36  ***************************************************************************** */
37
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:
41  *
42  * 32-bit (MEMB)
43  *      This is a standard length for the base assembler and requires no
44  *      special action.
45  *
46  * 13-bit (COBR)
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
50  *      to process it.
51  *
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.
57  *
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
61  *      to be external.
62  *
63  * 24-bit (CTRL)
64  *      Fixups are handled as for the 13-bit case (except that 24 is stored
65  *      in the fixS).
66  *
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.
71  *
72  * 12-bit (MEMA)
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.
78  */
79
80 #include <stdio.h>
81 #include <ctype.h>
82
83 #include "as.h"
84 #include "read.h"
85
86 #include "obstack.h"
87
88 #include "opcode/i960.h"
89
90 extern char *input_line_pointer;
91 extern struct hash_control *po_hash;
92 extern char *next_object_file_charP;
93
94 #ifdef OBJ_COFF
95 int md_reloc_size = sizeof (struct reloc);
96 #else /* OBJ_COFF */
97 int md_reloc_size = sizeof (struct relocation_info);
98 #endif /* OBJ_COFF */
99
100 /***************************
101  *  Local i80960 routines  *
102  ************************** */
103
104 static void brcnt_emit ();      /* Emit branch-prediction instrumentation code */
105 static char *brlab_next ();     /* Return next branch local label */
106 void brtab_emit ();             /* Emit br-predict instrumentation table */
107 static void cobr_fmt ();        /* Generate COBR instruction */
108 static void ctrl_fmt ();        /* Generate CTRL instruction */
109 static char *emit ();           /* Emit (internally) binary */
110 static int get_args ();         /* Break arguments out of comma-separated list */
111 static void get_cdisp ();       /* Handle COBR or CTRL displacement */
112 static char *get_ispec ();      /* Find index specification string */
113 static int get_regnum ();       /* Translate text to register number */
114 static int i_scan ();           /* Lexical scan of instruction source */
115 static void mem_fmt ();         /* Generate MEMA or MEMB instruction */
116 static void mema_to_memb ();    /* Convert MEMA instruction to MEMB format */
117 static segT parse_expr ();      /* Parse an expression */
118 static int parse_ldconst ();    /* Parse and replace a 'ldconst' pseudo-op */
119 static void parse_memop ();     /* Parse a memory operand */
120 static void parse_po ();        /* Parse machine-dependent pseudo-op */
121 static void parse_regop ();     /* Parse a register operand */
122 static void reg_fmt ();         /* Generate a REG format instruction */
123 void reloc_callj ();            /* Relocate a 'callj' instruction */
124 static void relax_cobr ();      /* "De-optimize" cobr into compare/branch */
125 static void s_leafproc ();      /* Process '.leafproc' pseudo-op */
126 static void s_sysproc ();       /* Process '.sysproc' pseudo-op */
127 static int shift_ok ();         /* Will a 'shlo' substiture for a 'ldconst'? */
128 static void syntax ();          /* Give syntax error */
129 static int targ_has_sfr ();     /* Target chip supports spec-func register? */
130 static int targ_has_iclass ();  /* Target chip supports instruction set? */
131 /* static void  unlink_sym(); *//* Remove a symbol from the symbol list */
132
133 /* See md_parse_option() for meanings of these options */
134 static char norelax;            /* True if -norelax switch seen */
135 static char instrument_branches;/* True if -b switch seen */
136
137 /* Characters that always start a comment.
138  * If the pre-processor is disabled, these aren't very useful.
139  */
140 const char comment_chars[] = "#";
141
142 /* Characters that only start a comment at the beginning of
143  * a line.  If the line seems to have the form '# 123 filename'
144  * .line and .file directives will appear in the pre-processed output.
145  *
146  * Note that input_file.c hand checks for '#' at the beginning of the
147  * first line of the input file.  This is because the compiler outputs
148  * #NO_APP at the beginning of its output.
149  */
150
151 /* Also note that comments started like this one will always work. */
152
153 const char line_comment_chars[] = "";
154
155 const char line_separator_chars[] = "";
156
157 /* Chars that can be used to separate mant from exp in floating point nums */
158 const char EXP_CHARS[] = "eE";
159
160 /* Chars that mean this number is a floating point constant,
161  * as in 0f12.456 or 0d1.2345e12
162  */
163 const char FLT_CHARS[] = "fFdDtT";
164
165
166 /* Table used by base assembler to relax addresses based on varying length
167  * instructions.  The fields are:
168  *   1) most positive reach of this state,
169  *   2) most negative reach of this state,
170  *   3) how many bytes this mode will add to the size of the current frag
171  *   4) which index into the table to try if we can't fit into this one.
172  *
173  * For i80960, the only application is the (de-)optimization of cobr
174  * instructions into separate compare and branch instructions when a 13-bit
175  * displacement won't hack it.
176  */
177 const relax_typeS
178   md_relax_table[] =
179 {
180   {0, 0, 0, 0},                 /* State 0 => no more relaxation possible */
181   {4088, -4096, 0, 2},          /* State 1: conditional branch (cobr) */
182   {0x800000 - 8, -0x800000, 4, 0},      /* State 2: compare (reg) & branch (ctrl) */
183 };
184
185
186 /* These are the machine dependent pseudo-ops.
187  *
188  * This table describes all the machine specific pseudo-ops the assembler
189  * has to support.  The fields are:
190  *      pseudo-op name without dot
191  *      function to call to execute this pseudo-op
192  *      integer arg to pass to the function
193  */
194 #define S_LEAFPROC      1
195 #define S_SYSPROC       2
196
197 const pseudo_typeS md_pseudo_table[] =
198 {
199   {"bss", s_lcomm, 1},
200   {"extended", float_cons, 't'},
201   {"leafproc", parse_po, S_LEAFPROC},
202   {"sysproc", parse_po, S_SYSPROC},
203
204   {"word", cons, 4},
205   {"quad", big_cons, 16},
206
207   {0, 0, 0}
208 };
209 \f
210 /* Macros to extract info from an 'expressionS' structure 'e' */
211 #define adds(e) e.X_add_symbol
212 #define subs(e) e.X_subtract_symbol
213 #define offs(e) e.X_add_number
214 #define segs(e) e.X_seg
215
216
217 /* Branch-prediction bits for CTRL/COBR format opcodes */
218 #define BP_MASK         0x00000002      /* Mask for branch-prediction bit */
219 #define BP_TAKEN        0x00000000      /* Value to OR in to predict branch */
220 #define BP_NOT_TAKEN    0x00000002      /* Value to OR in to predict no branch */
221
222
223 /* Some instruction opcodes that we need explicitly */
224 #define BE      0x12000000
225 #define BG      0x11000000
226 #define BGE     0x13000000
227 #define BL      0x14000000
228 #define BLE     0x16000000
229 #define BNE     0x15000000
230 #define BNO     0x10000000
231 #define BO      0x17000000
232 #define CHKBIT  0x5a002700
233 #define CMPI    0x5a002080
234 #define CMPO    0x5a002000
235
236 #define B       0x08000000
237 #define BAL     0x0b000000
238 #define CALL    0x09000000
239 #define CALLS   0x66003800
240 #define RET     0x0a000000
241
242
243 /* These masks are used to build up a set of MEMB mode bits. */
244 #define A_BIT           0x0400
245 #define I_BIT           0x0800
246 #define MEMB_BIT        0x1000
247 #define D_BIT           0x2000
248
249
250 /* Mask for the only mode bit in a MEMA instruction (if set, abase reg is used) */
251 #define MEMA_ABASE      0x2000
252
253 /* Info from which a MEMA or MEMB format instruction can be generated */
254 typedef struct
255   {
256     long opcode;                /* (First) 32 bits of instruction */
257     int disp;                   /* 0-(none), 12- or, 32-bit displacement needed */
258     char *e;                    /* The expression in the source instruction from
259                          *      which the displacement should be determined
260                          */
261   }
262
263 memS;
264
265
266 /* The two pieces of info we need to generate a register operand */
267 struct regop
268   {
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 */
272   };
273
274
275 /* Number and assembler mnemonic for all registers that can appear in operands */
276 static struct
277   {
278     char *reg_name;
279     int reg_num;
280   }
281
282 regnames[] =
283 {
284   { "pfp", 0 },
285   { "sp", 1 },
286   { "rip", 2 },
287   { "r3", 3 },
288   { "r4", 4 },
289   { "r5", 5 },
290   { "r6", 6 },
291   { "r7", 7 },
292   { "r8", 8 },
293   { "r9", 9 },
294   { "r10", 10 },
295   { "r11", 11 },
296   { "r12", 12 },
297   { "r13", 13 },
298   { "r14", 14 },
299   { "r15", 15 },
300   { "g0", 16 },
301   { "g1", 17 },
302   { "g2", 18 },
303   { "g3", 19 },
304   { "g4", 20 },
305   { "g5", 21 },
306   { "g6", 22 },
307   { "g7", 23 },
308   { "g8", 24 },
309   { "g9", 25 },
310   { "g10", 26 },
311   { "g11", 27 },
312   { "g12", 28 },
313   { "g13", 29 },
314   { "g14", 30 },
315   { "fp", 31 },
316
317   /* Numbers for special-function registers are for assembler internal
318      use only: they are scaled back to range [0-31] for binary output.  */
319 #define SF0     32
320
321   { "sf0", 32 },
322   { "sf1", 33 },
323   { "sf2", 34 },
324   { "sf3", 35 },
325   { "sf4", 36 },
326   { "sf5", 37 },
327   { "sf6", 38 },
328   { "sf7", 39 },
329   { "sf8", 40 },
330   { "sf9", 41 },
331   { "sf10", 42 },
332   { "sf11", 43 },
333   { "sf12", 44 },
334   { "sf13", 45 },
335   { "sf14", 46 },
336   { "sf15", 47 },
337   { "sf16", 48 },
338   { "sf17", 49 },
339   { "sf18", 50 },
340   { "sf19", 51 },
341   { "sf20", 52 },
342   { "sf21", 53 },
343   { "sf22", 54 },
344   { "sf23", 55 },
345   { "sf24", 56 },
346   { "sf25", 57 },
347   { "sf26", 58 },
348   { "sf27", 59 },
349   { "sf28", 60 },
350   { "sf29", 61 },
351   { "sf30", 62 },
352   { "sf31", 63 },
353
354   /* Numbers for floating point registers are for assembler internal use
355          * only: they are scaled back to [0-3] for binary output.
356          */
357 #define FP0     64
358
359   { "fp0", 64 },
360   { "fp1", 65 },
361   { "fp2", 66 },
362   { "fp3", 67 },
363
364   { NULL, 0 },                          /* END OF LIST */
365 };
366
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)
370
371 /* Number and assembler mnemonic for all registers that can appear as 'abase'
372  * (indirect addressing) registers.
373  */
374 static struct
375   {
376     char *areg_name;
377     int areg_num;
378   }
379
380 aregs[] =
381 {
382   { "(pfp)", 0 },
383   { "(sp)", 1 },
384   { "(rip)", 2 },
385   { "(r3)", 3 },
386   { "(r4)", 4 },
387   { "(r5)", 5 },
388   { "(r6)", 6 },
389   { "(r7)", 7 },
390   { "(r8)", 8 },
391   { "(r9)", 9 },
392   { "(r10)", 10 },
393   { "(r11)", 11 },
394   { "(r12)", 12 },
395   { "(r13)", 13 },
396   { "(r14)", 14 },
397   { "(r15)", 15 },
398   { "(g0)", 16 },
399   { "(g1)", 17 },
400   { "(g2)", 18 },
401   { "(g3)", 19 },
402   { "(g4)", 20 },
403   { "(g5)", 21 },
404   { "(g6)", 22 },
405   { "(g7)", 23 },
406   { "(g8)", 24 },
407   { "(g9)", 25 },
408   { "(g10)", 26 },
409   { "(g11)", 27 },
410   { "(g12)", 28 },
411   { "(g13)", 29 },
412   { "(g14)", 30 },
413   { "(fp)", 31 },
414
415 #define IPREL   32
416   /* For assembler internal use only: this number never appears in binary
417      output.  */
418   { "(ip)", IPREL },
419
420   { NULL, 0 },                          /* END OF LIST */
421 };
422
423
424 /* Hash tables */
425 static struct hash_control *op_hash = NULL;     /* Opcode mnemonics */
426 static struct hash_control *reg_hash = NULL;    /* Register name hash table */
427 static struct hash_control *areg_hash = NULL;   /* Abase register hash table */
428
429
430 /* Architecture for which we are assembling */
431 #define ARCH_ANY        0       /* Default: no architecture checking done */
432 #define ARCH_KA         1
433 #define ARCH_KB         2
434 #define ARCH_MC         3
435 #define ARCH_CA         4
436 int architecture = ARCH_ANY;    /* Architecture requested on invocation line */
437 int iclasses_seen = 0;          /* OR of instruction classes (I_* constants)
438                                  *      for which we've actually assembled
439                                  *      instructions.
440                                  */
441
442
443 /* BRANCH-PREDICTION INSTRUMENTATION
444  *
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
450  *      brach-prediction.)
451  *
452  *      The instrumentation consists of:
453  *
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:
461  *
462  *                              call    BR_CNT_FUNC
463  *                              .word   0
464  *              LBRANCH23:      be      label
465  *                              call    BR_CNT_FUNC
466  *                              .word   0
467  *
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.
473  *
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.
477  */
478
479 static int br_cnt = 0;          /* Number of branches instrumented so far.
480                                  * Also used to generate unique local labels
481                                  * for each instrumented branch
482                                  */
483
484 #define BR_LABEL_BASE   "LBRANCH"
485 /* Basename of local labels on instrumented
486  * branches, to avoid conflict with compiler-
487  * generated local labels.
488  */
489
490 #define BR_CNT_FUNC     "__inc_branch"
491 /* Name of the external routine that will
492  * increment (and step over) an inline counter.
493  */
494
495 #define BR_TAB_NAME     "__BRANCH_TABLE__"
496 /* Name of the table of pointers to branches.
497  * A local (i.e., non-external) symbol.
498  */
499 \f
500 /*****************************************************************************
501  * md_begin:  One-time initialization.
502  *
503  *      Set up hash tables.
504  *
505  **************************************************************************** */
506 void
507 md_begin ()
508 {
509   int i;                        /* Loop counter */
510   const struct i960_opcode *oP; /* Pointer into opcode table */
511   char *retval;                 /* Value returned by hash functions */
512
513   if (((op_hash = hash_new ()) == 0)
514       || ((reg_hash = hash_new ()) == 0)
515       || ((areg_hash = hash_new ()) == 0))
516     {
517       as_fatal ("virtual memory exceeded");
518     }
519
520   retval = "";                  /* For some reason, the base assembler uses an empty
521                          * string for "no error message", instead of a NULL
522                          * pointer.
523                          */
524
525   for (oP = i960_opcodes; oP->name && !*retval; oP++)
526     {
527       retval = hash_insert (op_hash, oP->name, oP);
528     }
529
530   for (i = 0; regnames[i].reg_name && !*retval; i++)
531     {
532       retval = hash_insert (reg_hash, regnames[i].reg_name,
533                             &regnames[i].reg_num);
534     }
535
536   for (i = 0; aregs[i].areg_name && !*retval; i++)
537     {
538       retval = hash_insert (areg_hash, aregs[i].areg_name,
539                             &aregs[i].areg_num);
540     }
541
542   if (*retval)
543     {
544       as_fatal ("Hashing returned \"%s\".", retval);
545     }
546 }                               /* md_begin() */
547
548 /*****************************************************************************
549  * md_end:  One-time final cleanup
550  *
551  *      None necessary
552  *
553  **************************************************************************** */
554 void
555 md_end ()
556 {
557 }
558
559 /*****************************************************************************
560  * md_assemble:  Assemble an instruction
561  *
562  * Assumptions about the passed-in text:
563  *      - all comments, labels removed
564  *      - text is an instruction
565  *      - all white space compressed to single blanks
566  *      - all character constants have been replaced with decimal
567  *
568  **************************************************************************** */
569 void
570 md_assemble (textP)
571      char *textP;               /* Source text of instruction */
572 {
573   char *args[4];                /* Parsed instruction text, containing NO whitespace:
574                          *      arg[0]->opcode mnemonic
575                          *      arg[1-3]->operands, with char constants
576                          *                      replaced by decimal numbers
577                          */
578   int n_ops;                    /* Number of instruction operands */
579   int callx;
580   struct i960_opcode *oP;
581   /* Pointer to instruction description */
582   int branch_predict;
583   /* TRUE iff opcode mnemonic included branch-prediction
584          *      suffix (".f" or ".t")
585          */
586   long bp_bits;                 /* Setting of branch-prediction bit(s) to be OR'd
587                          *      into instruction opcode of CTRL/COBR format
588                          *      instructions.
589                          */
590   int n;                        /* Offset of last character in opcode mnemonic */
591
592   static const char bp_error_msg[] = "branch prediction invalid on this opcode";
593
594
595   /* Parse instruction into opcode and operands */
596   memset (args, '\0', sizeof (args));
597   n_ops = i_scan (textP, args);
598   if (n_ops == -1)
599     {
600       return;                   /* Error message already issued */
601     }
602
603   /* Do "macro substitution" (sort of) on 'ldconst' pseudo-instruction */
604   if (!strcmp (args[0], "ldconst"))
605     {
606       n_ops = parse_ldconst (args);
607       if (n_ops == -1)
608         {
609           return;
610         }
611     }
612
613
614
615   /* Check for branch-prediction suffix on opcode mnemonic, strip it off */
616   n = strlen (args[0]) - 1;
617   branch_predict = 0;
618   bp_bits = 0;
619   if (args[0][n - 1] == '.' && (args[0][n] == 't' || args[0][n] == 'f'))
620     {
621       /* We could check here to see if the target architecture
622                  * supports branch prediction, but why bother?  The bit
623                  * will just be ignored by processors that don't use it.
624                  */
625       branch_predict = 1;
626       bp_bits = (args[0][n] == 't') ? BP_TAKEN : BP_NOT_TAKEN;
627       args[0][n - 1] = '\0';    /* Strip suffix from opcode mnemonic */
628     }
629
630   /* Look up opcode mnemonic in table and check number of operands.
631          * Check that opcode is legal for the target architecture.
632          * If all looks good, assemble instruction.
633          */
634   oP = (struct i960_opcode *) hash_find (op_hash, args[0]);
635   if (!oP || !targ_has_iclass (oP->iclass))
636     {
637       as_bad ("invalid opcode, \"%s\".", args[0]);
638
639     }
640   else if (n_ops != oP->num_ops)
641     {
642       as_bad ("improper number of operands.  expecting %d, got %d", oP->num_ops, n_ops);
643
644     }
645   else
646     {
647       switch (oP->format)
648         {
649         case FBRA:
650         case CTRL:
651           ctrl_fmt (args[1], oP->opcode | bp_bits, oP->num_ops);
652           if (oP->format == FBRA)
653             {
654               /* Now generate a 'bno' to same arg */
655               ctrl_fmt (args[1], BNO | bp_bits, 1);
656             }
657           break;
658         case COBR:
659         case COJ:
660           cobr_fmt (args, oP->opcode | bp_bits, oP);
661           break;
662         case REG:
663           if (branch_predict)
664             {
665               as_warn (bp_error_msg);
666             }
667           reg_fmt (args, oP);
668           break;
669         case MEM1:
670           if (args[0][0] == 'c' && args[0][1] == 'a')
671             {
672               if (branch_predict)
673                 {
674                   as_warn (bp_error_msg);
675                 }
676               mem_fmt (args, oP, 1);
677               break;
678             }
679         case MEM2:
680         case MEM4:
681         case MEM8:
682         case MEM12:
683         case MEM16:
684           if (branch_predict)
685             {
686               as_warn (bp_error_msg);
687             }
688           mem_fmt (args, oP, 0);
689           break;
690         case CALLJ:
691           if (branch_predict)
692             {
693               as_warn (bp_error_msg);
694             }
695           /* Output opcode & set up "fixup" (relocation);
696                          * flag relocation as 'callj' type.
697                          */
698           know (oP->num_ops == 1);
699           get_cdisp (args[1], "CTRL", oP->opcode, 24, 0, 1);
700           break;
701         default:
702           BAD_CASE (oP->format);
703           break;
704         }
705     }
706 }                               /* md_assemble() */
707
708 /*****************************************************************************
709  * md_number_to_chars:  convert a number to target byte order
710  *
711  **************************************************************************** */
712 void
713 md_number_to_chars (buf, value, n)
714      char *buf;                 /* Put output here */
715      long value;                /* The integer to be converted */
716      int n;                     /* Number of bytes to output (significant bytes
717                  *      in 'value')
718                  */
719 {
720   while (n--)
721     {
722       *buf++ = value;
723       value >>= 8;
724     }
725
726   /* XXX line number probably botched for this warning message. */
727   if (value != 0 && value != -1)
728     {
729       as_bad ("Displacement too long for instruction field length.");
730     }
731
732   return;
733 }                               /* md_number_to_chars() */
734
735 /*****************************************************************************
736  * md_chars_to_number:  convert from target byte order to host byte order.
737  *
738  **************************************************************************** */
739 int
740 md_chars_to_number (val, n)
741      unsigned char *val;        /* Value in target byte order */
742      int n;                     /* Number of bytes in the input */
743 {
744   int retval;
745
746   for (retval = 0; n--;)
747     {
748       retval <<= 8;
749       retval |= val[n];
750     }
751   return retval;
752 }
753
754
755 #define MAX_LITTLENUMS  6
756 #define LNUM_SIZE       sizeof(LITTLENUM_TYPE)
757
758 /*****************************************************************************
759  * md_atof:     convert ascii to floating point
760  *
761  * Turn a string at input_line_pointer into a floating point constant of type
762  * 'type', and store the appropriate bytes at *litP.  The number of LITTLENUMS
763  * emitted is returned at 'sizeP'.  An error message is returned, or a pointer
764  * to an empty message if OK.
765  *
766  * Note we call the i386 floating point routine, rather than complicating
767  * things with more files or symbolic links.
768  *
769  **************************************************************************** */
770 char *
771 md_atof (type, litP, sizeP)
772      int type;
773      char *litP;
774      int *sizeP;
775 {
776   LITTLENUM_TYPE words[MAX_LITTLENUMS];
777   LITTLENUM_TYPE *wordP;
778   int prec;
779   char *t;
780   char *atof_ieee ();
781
782   switch (type)
783     {
784     case 'f':
785     case 'F':
786       prec = 2;
787       break;
788
789     case 'd':
790     case 'D':
791       prec = 4;
792       break;
793
794     case 't':
795     case 'T':
796       prec = 5;
797       type = 'x';               /* That's what atof_ieee() understands */
798       break;
799
800     default:
801       *sizeP = 0;
802       return "Bad call to md_atof()";
803     }
804
805   t = atof_ieee (input_line_pointer, type, words);
806   if (t)
807     {
808       input_line_pointer = t;
809     }
810
811   *sizeP = prec * LNUM_SIZE;
812
813   /* Output the LITTLENUMs in REVERSE order in accord with i80960
814          * word-order.  (Dunno why atof_ieee doesn't do it in the right
815          * order in the first place -- probably because it's a hack of
816          * atof_m68k.)
817          */
818
819   for (wordP = words + prec - 1; prec--;)
820     {
821       md_number_to_chars (litP, (long) (*wordP--), LNUM_SIZE);
822       litP += sizeof (LITTLENUM_TYPE);
823     }
824
825   return "";                    /* Someone should teach Dean about null pointers */
826 }
827
828
829 /*****************************************************************************
830  * md_number_to_imm
831  *
832  **************************************************************************** */
833 void
834 md_number_to_imm (buf, val, n)
835      char *buf;
836      long val;
837      int n;
838 {
839   md_number_to_chars (buf, val, n);
840 }
841
842
843 /*****************************************************************************
844  * md_number_to_disp
845  *
846  **************************************************************************** */
847 void
848 md_number_to_disp (buf, val, n)
849      char *buf;
850      long val;
851      int n;
852 {
853   md_number_to_chars (buf, val, n);
854 }
855
856 /*****************************************************************************
857  * md_number_to_field:
858  *
859  *      Stick a value (an address fixup) into a bit field of
860  *      previously-generated instruction.
861  *
862  **************************************************************************** */
863 void
864 md_number_to_field (instrP, val, bfixP)
865      char *instrP;              /* Pointer to instruction to be fixed */
866      long val;                  /* Address fixup value */
867      bit_fixS *bfixP;           /* Description of bit field to be fixed up */
868 {
869   int numbits;                  /* Length of bit field to be fixed */
870   long instr;                   /* 32-bit instruction to be fixed-up */
871   long sign;                    /* 0 or -1, according to sign bit of 'val' */
872
873   /* Convert instruction back to host byte order
874          */
875   instr = md_chars_to_number (instrP, 4);
876
877   /* Surprise! -- we stored the number of bits
878          * to be modified rather than a pointer to a structure.
879          */
880   numbits = (int) bfixP;
881   if (numbits == 1)
882     {
883       /* This is a no-op, stuck here by reloc_callj() */
884       return;
885     }
886
887   know ((numbits == 13) || (numbits == 24));
888
889   /* Propagate sign bit of 'val' for the given number of bits.
890          * Result should be all 0 or all 1
891          */
892   sign = val >> ((int) numbits - 1);
893   if (((val < 0) && (sign != -1))
894       || ((val > 0) && (sign != 0)))
895     {
896       as_bad ("Fixup of %d too large for field width of %d",
897               val, numbits);
898     }
899   else
900     {
901       /* Put bit field into instruction and write back in target
902                  * byte order.
903                  */
904       val &= ~(-1 << (int) numbits);    /* Clear unused sign bits */
905       instr |= val;
906       md_number_to_chars (instrP, instr, 4);
907     }
908 }                               /* md_number_to_field() */
909
910
911 /*****************************************************************************
912  * md_parse_option
913  *      Invocation line includes a switch not recognized by the base assembler.
914  *      See if it's a processor-specific option.  For the 960, these are:
915  *
916  *      -norelax:
917  *              Conditional branch instructions that require displacements
918  *              greater than 13 bits (or that have external targets) should
919  *              generate errors.  The default is to replace each such
920  *              instruction with the corresponding compare (or chkbit) and
921  *              branch instructions.  Note that the Intel "j" cobr directives
922  *              are ALWAYS "de-optimized" in this way when necessary,
923  *              regardless of the setting of this option.
924  *
925  *      -b:
926  *              Add code to collect information about branches taken, for
927  *              later optimization of branch prediction bits by a separate
928  *              tool.  COBR and CNTL format instructions have branch
929  *              prediction bits (in the CX architecture);  if "BR" represents
930  *              an instruction in one of these classes, the following rep-
931  *              resents the code generated by the assembler:
932  *
933  *                      call    <increment routine>
934  *                      .word   0       # pre-counter
935  *              Label:  BR
936  *                      call    <increment routine>
937  *                      .word   0       # post-counter
938  *
939  *              A table of all such "Labels" is also generated.
940  *
941  *
942  *      -AKA, -AKB, -AKC, -ASA, -ASB, -AMC, -ACA:
943  *              Select the 80960 architecture.  Instructions or features not
944  *              supported by the selected architecture cause fatal errors.
945  *              The default is to generate code for any instruction or feature
946  *              that is supported by SOME version of the 960 (even if this
947  *              means mixing architectures!).
948  *
949  **************************************************************************** */
950 int
951 md_parse_option (argP, cntP, vecP)
952      char **argP;
953      int *cntP;
954      char ***vecP;
955 {
956   char *p;
957   struct tabentry
958     {
959       char *flag;
960       int arch;
961     };
962   static struct tabentry arch_tab[] =
963   {
964     "KA", ARCH_KA,
965     "KB", ARCH_KB,
966     "SA", ARCH_KA,              /* Synonym for KA */
967     "SB", ARCH_KB,              /* Synonym for KB */
968     "KC", ARCH_MC,              /* Synonym for MC */
969     "MC", ARCH_MC,
970     "CA", ARCH_CA,
971     NULL, 0
972   };
973   struct tabentry *tp;
974   if (!strcmp (*argP, "linkrelax"))
975     {
976       linkrelax = 1;
977       flagseen['L'] = 1;
978     }
979   else if (!strcmp (*argP, "norelax"))
980     {
981       norelax = 1;
982
983     }
984   else if (**argP == 'b')
985     {
986       instrument_branches = 1;
987
988     }
989   else if (**argP == 'A')
990     {
991       p = (*argP) + 1;
992
993       for (tp = arch_tab; tp->flag != NULL; tp++)
994         {
995           if (!strcmp (p, tp->flag))
996             {
997               break;
998             }
999         }
1000
1001       if (tp->flag == NULL)
1002         {
1003           as_bad ("unknown architecture: %s", p);
1004         }
1005       else
1006         {
1007           architecture = tp->arch;
1008         }
1009     }
1010   else
1011     {
1012       /* Unknown option */
1013       (*argP)++;
1014       return 0;
1015     }
1016   **argP = '\0';                /* Done parsing this switch */
1017   return 1;
1018 }
1019
1020 /*****************************************************************************
1021  * md_convert_frag:
1022  *      Called by base assembler after address relaxation is finished:  modify
1023  *      variable fragments according to how much relaxation was done.
1024  *
1025  *      If the fragment substate is still 1, a 13-bit displacement was enough
1026  *      to reach the symbol in question.  Set up an address fixup, but otherwise
1027  *      leave the cobr instruction alone.
1028  *
1029  *      If the fragment substate is 2, a 13-bit displacement was not enough.
1030  *      Replace the cobr with a two instructions (a compare and a branch).
1031  *
1032  **************************************************************************** */
1033 void
1034 md_convert_frag (headers, fragP)
1035      object_headers *headers;
1036      fragS *fragP;
1037 {
1038   fixS *fixP;                   /* Structure describing needed address fix */
1039
1040   switch (fragP->fr_subtype)
1041     {
1042     case 1:
1043       /* LEAVE SINGLE COBR INSTRUCTION */
1044       fixP = fix_new (fragP,
1045                       fragP->fr_opcode - fragP->fr_literal,
1046                       4,
1047                       fragP->fr_symbol,
1048                       0,
1049                       fragP->fr_offset,
1050                       1,
1051                       NO_RELOC);
1052
1053       fixP->fx_bit_fixP = (bit_fixS *) 13;      /* size of bit field */
1054       break;
1055     case 2:
1056       /* REPLACE COBR WITH COMPARE/BRANCH INSTRUCTIONS */
1057       relax_cobr (fragP);
1058       break;
1059     default:
1060       BAD_CASE (fragP->fr_subtype);
1061       break;
1062     }
1063 }
1064
1065 /*****************************************************************************
1066  * md_estimate_size_before_relax:  How much does it look like *fragP will grow?
1067  *
1068  *      Called by base assembler just before address relaxation.
1069  *      Return the amount by which the fragment will grow.
1070  *
1071  *      Any symbol that is now undefined will not become defined; cobr's
1072  *      based on undefined symbols will have to be replaced with a compare
1073  *      instruction and a branch instruction, and the code fragment will grow
1074  *      by 4 bytes.
1075  *
1076  **************************************************************************** */
1077 int
1078 md_estimate_size_before_relax (fragP, segment_type)
1079      register fragS *fragP;
1080      register segT segment_type;
1081 {
1082   /* If symbol is undefined in this segment, go to "relaxed" state
1083          * (compare and branch instructions instead of cobr) right now.
1084          */
1085   if (S_GET_SEGMENT (fragP->fr_symbol) != segment_type)
1086     {
1087       relax_cobr (fragP);
1088       return 4;
1089     }
1090   return 0;
1091 }                               /* md_estimate_size_before_relax() */
1092
1093
1094 /*****************************************************************************
1095  * md_ri_to_chars:
1096  *      This routine exists in order to overcome machine byte-order problems
1097  *      when dealing with bit-field entries in the relocation_info struct.
1098  *
1099  *      But relocation info will be used on the host machine only (only
1100  *      executable code is actually downloaded to the i80960).  Therefore,
1101  *      we leave it in host byte order.
1102  *
1103  *      The above comment is no longer true.  This routine now really
1104  *      does do the reordering (Ian Taylor 28 Aug 92).
1105  *
1106  **************************************************************************** */
1107 void
1108 md_ri_to_chars (where, ri)
1109      char *where;
1110      struct relocation_info *ri;
1111 {
1112   md_number_to_chars (where, ri->r_address,
1113                       sizeof (ri->r_address));
1114   where[4] = ri->r_index & 0x0ff;
1115   where[5] = (ri->r_index >> 8) & 0x0ff;
1116   where[6] = (ri->r_index >> 16) & 0x0ff;
1117   where[7] = ((ri->r_pcrel << 0)
1118               | (ri->r_length << 1)
1119               | (ri->r_extern << 3)
1120               | (ri->r_bsr << 4)
1121               | (ri->r_disp << 5)
1122               | (ri->r_callj << 6));
1123 }                               /* md_ri_to_chars() */
1124
1125 #ifndef WORKING_DOT_WORD
1126
1127 int md_short_jump_size = 0;
1128 int md_long_jump_size = 0;
1129
1130 void
1131 md_create_short_jump (ptr, from_addr, to_addr, frag, to_symbol)
1132      char *ptr;
1133      long from_addr;
1134      long to_addr;
1135      fragS *frag;
1136      symbolS *to_symbol;
1137 {
1138   as_fatal ("failed sanity check.");
1139 }
1140
1141 void
1142 md_create_long_jump (ptr, from_addr, to_addr, frag, to_symbol)
1143      char *ptr;
1144      long from_addr, to_addr;
1145      fragS *frag;
1146      symbolS *to_symbol;
1147 {
1148   as_fatal ("failed sanity check.");
1149 }
1150
1151 #endif
1152 \f
1153 /*************************************************************
1154  *                                                           *
1155  *  FOLLOWING ARE THE LOCAL ROUTINES, IN ALPHABETICAL ORDER  *
1156  *                                                           *
1157  ************************************************************ */
1158
1159
1160
1161 /*****************************************************************************
1162  * brcnt_emit:  Emit code to increment inline branch counter.
1163  *
1164  *      See the comments above the declaration of 'br_cnt' for details on
1165  *      branch-prediction instrumentation.
1166  **************************************************************************** */
1167 static void
1168 brcnt_emit ()
1169 {
1170   ctrl_fmt (BR_CNT_FUNC, CALL, 1);      /* Emit call to "increment" routine */
1171   emit (0);                     /* Emit inline counter to be incremented */
1172 }
1173
1174 /*****************************************************************************
1175  * brlab_next:  generate the next branch local label
1176  *
1177  *      See the comments above the declaration of 'br_cnt' for details on
1178  *      branch-prediction instrumentation.
1179  **************************************************************************** */
1180 static char *
1181 brlab_next ()
1182 {
1183   static char buf[20];
1184
1185   sprintf (buf, "%s%d", BR_LABEL_BASE, br_cnt++);
1186   return buf;
1187 }
1188
1189 /*****************************************************************************
1190  * brtab_emit:  generate the fetch-prediction branch table.
1191  *
1192  *      See the comments above the declaration of 'br_cnt' for details on
1193  *      branch-prediction instrumentation.
1194  *
1195  *      The code emitted here would be functionally equivalent to the following
1196  *      example assembler source.
1197  *
1198  *                      .data
1199  *                      .align  2
1200  *         BR_TAB_NAME:
1201  *                      .word   0               # link to next table
1202  *                      .word   3               # length of table
1203  *                      .word   LBRANCH0        # 1st entry in table proper
1204  *                      .word   LBRANCH1
1205  *                      .word   LBRANCH2
1206  ***************************************************************************** */
1207 void
1208 brtab_emit ()
1209 {
1210   int i;
1211   char buf[20];
1212   char *p;                      /* Where the binary was output to */
1213   fixS *fixP;                   /*->description of deferred address fixup */
1214
1215   if (!instrument_branches)
1216     {
1217       return;
1218     }
1219
1220   subseg_new (SEG_DATA, 0);     /*      .data */
1221   frag_align (2, 0);            /*      .align 2 */
1222   record_alignment (now_seg, 2);
1223   colon (BR_TAB_NAME);          /* BR_TAB_NAME: */
1224   emit (0);                     /*      .word 0 #link to next table */
1225   emit (br_cnt);                /*      .word n #length of table */
1226
1227   for (i = 0; i < br_cnt; i++)
1228     {
1229       sprintf (buf, "%s%d", BR_LABEL_BASE, i);
1230       p = emit (0);
1231       fixP = fix_new (frag_now,
1232                       p - frag_now->fr_literal,
1233                       4,
1234                       symbol_find (buf),
1235                       0,
1236                       0,
1237                       0,
1238                       NO_RELOC);
1239       fixP->fx_im_disp = 2;     /* 32-bit displacement fix */
1240     }
1241 }
1242
1243 /*****************************************************************************
1244  * cobr_fmt:    generate a COBR-format instruction
1245  *
1246  **************************************************************************** */
1247 static
1248 void
1249 cobr_fmt (arg, opcode, oP)
1250      char *arg[];               /* arg[0]->opcode mnemonic, arg[1-3]->operands (ascii) */
1251      long opcode;               /* Opcode, with branch-prediction bits already set
1252                  *      if necessary.
1253                  */
1254      struct i960_opcode *oP;
1255      /*->description of instruction */
1256 {
1257   long instr;                   /* 32-bit instruction */
1258   struct regop regop;           /* Description of register operand */
1259   int n;                        /* Number of operands */
1260   int var_frag;                 /* 1 if varying length code fragment should
1261                                  *      be emitted;  0 if an address fix
1262                                  *      should be emitted.
1263                                  */
1264
1265   instr = opcode;
1266   n = oP->num_ops;
1267
1268   if (n >= 1)
1269     {
1270       /* First operand (if any) of a COBR is always a register
1271                  * operand.  Parse it.
1272                  */
1273       parse_regop (&regop, arg[1], oP->operand[0]);
1274       instr |= (regop.n << 19) | (regop.mode << 13);
1275     }
1276   if (n >= 2)
1277     {
1278       /* Second operand (if any) of a COBR is always a register
1279                  * operand.  Parse it.
1280                  */
1281       parse_regop (&regop, arg[2], oP->operand[1]);
1282       instr |= (regop.n << 14) | regop.special;
1283     }
1284
1285
1286   if (n < 3)
1287     {
1288       emit (instr);
1289
1290     }
1291   else
1292     {
1293       if (instrument_branches)
1294         {
1295           brcnt_emit ();
1296           colon (brlab_next ());
1297         }
1298
1299       /* A third operand to a COBR is always a displacement.
1300                  * Parse it; if it's relaxable (a cobr "j" directive, or any
1301                  * cobr other than bbs/bbc when the "-norelax" option is not in
1302                  * use) set up a variable code fragment;  otherwise set up an
1303                  * address fix.
1304                  */
1305       var_frag = !norelax || (oP->format == COJ);       /* TRUE or FALSE */
1306       get_cdisp (arg[3], "COBR", instr, 13, var_frag, 0);
1307
1308       if (instrument_branches)
1309         {
1310           brcnt_emit ();
1311         }
1312     }
1313 }                               /* cobr_fmt() */
1314
1315
1316 /*****************************************************************************
1317  * ctrl_fmt:    generate a CTRL-format instruction
1318  *
1319  **************************************************************************** */
1320 static
1321 void
1322 ctrl_fmt (targP, opcode, num_ops)
1323      char *targP;               /* Pointer to text of lone operand (if any) */
1324      long opcode;               /* Template of instruction */
1325      int num_ops;               /* Number of operands */
1326 {
1327   int instrument;               /* TRUE iff we should add instrumentation to track
1328                          * how often the branch is taken
1329                          */
1330
1331
1332   if (num_ops == 0)
1333     {
1334       emit (opcode);            /* Output opcode */
1335     }
1336   else
1337     {
1338
1339       instrument = instrument_branches && (opcode != CALL)
1340         && (opcode != B) && (opcode != RET) && (opcode != BAL);
1341
1342       if (instrument)
1343         {
1344           brcnt_emit ();
1345           colon (brlab_next ());
1346         }
1347
1348       /* The operand MUST be an ip-relative displacment. Parse it
1349                  * and set up address fix for the instruction we just output.
1350                  */
1351       get_cdisp (targP, "CTRL", opcode, 24, 0, 0);
1352
1353       if (instrument)
1354         {
1355           brcnt_emit ();
1356         }
1357     }
1358
1359 }
1360
1361
1362 /*****************************************************************************
1363  * emit:        output instruction binary
1364  *
1365  *      Output instruction binary, in target byte order, 4 bytes at a time.
1366  *      Return pointer to where it was placed.
1367  *
1368  **************************************************************************** */
1369 static
1370 char *
1371 emit (instr)
1372      long instr;                /* Word to be output, host byte order */
1373 {
1374   char *toP;                    /* Where to output it */
1375
1376   toP = frag_more (4);          /* Allocate storage */
1377   md_number_to_chars (toP, instr, 4);   /* Convert to target byte order */
1378   return toP;
1379 }
1380
1381
1382 /*****************************************************************************
1383  * get_args:    break individual arguments out of comma-separated list
1384  *
1385  * Input assumptions:
1386  *      - all comments and labels have been removed
1387  *      - all strings of whitespace have been collapsed to a single blank.
1388  *      - all character constants ('x') have been replaced with decimal
1389  *
1390  * Output:
1391  *      args[0] is untouched. args[1] points to first operand, etc. All args:
1392  *      - are NULL-terminated
1393  *      - contain no whitespace
1394  *
1395  * Return value:
1396  *      Number of operands (0,1,2, or 3) or -1 on error.
1397  *
1398  **************************************************************************** */
1399 static int
1400 get_args (p, args)
1401      register char *p;          /* Pointer to comma-separated operands; MUCKED BY US */
1402      char *args[];              /* Output arg: pointers to operands placed in args[1-3].
1403                  * MUST ACCOMMODATE 4 ENTRIES (args[0-3]).
1404                  */
1405 {
1406   register int n;               /* Number of operands */
1407   register char *to;
1408   /*    char buf[4]; */
1409   /*    int len; */
1410
1411
1412   /* Skip lead white space */
1413   while (*p == ' ')
1414     {
1415       p++;
1416     }
1417
1418   if (*p == '\0')
1419     {
1420       return 0;
1421     }
1422
1423   n = 1;
1424   args[1] = p;
1425
1426   /* Squeze blanks out by moving non-blanks toward start of string.
1427          * Isolate operands, whenever comma is found.
1428          */
1429   to = p;
1430   while (*p != '\0')
1431     {
1432
1433       if (*p == ' ')
1434         {
1435           p++;
1436
1437         }
1438       else if (*p == ',')
1439         {
1440
1441           /* Start of operand */
1442           if (n == 3)
1443             {
1444               as_bad ("too many operands");
1445               return -1;
1446             }
1447           *to++ = '\0';         /* Terminate argument */
1448           args[++n] = to;       /* Start next argument */
1449           p++;
1450
1451         }
1452       else
1453         {
1454           *to++ = *p++;
1455         }
1456     }
1457   *to = '\0';
1458   return n;
1459 }
1460
1461
1462 /*****************************************************************************
1463  * get_cdisp:   handle displacement for a COBR or CTRL instruction.
1464  *
1465  *      Parse displacement for a COBR or CTRL instruction.
1466  *
1467  *      If successful, output the instruction opcode and set up for it,
1468  *      depending on the arg 'var_frag', either:
1469  *          o an address fixup to be done when all symbol values are known, or
1470  *          o a varying length code fragment, with address fixup info.  This
1471  *              will be done for cobr instructions that may have to be relaxed
1472  *              in to compare/branch instructions (8 bytes) if the final address
1473  *              displacement is greater than 13 bits.
1474  *
1475  **************************************************************************** */
1476 static
1477 void
1478 get_cdisp (dispP, ifmtP, instr, numbits, var_frag, callj)
1479      char *dispP;               /*->displacement as specified in source instruction */
1480      char *ifmtP;               /*->"COBR" or "CTRL" (for use in error message) */
1481      long instr;                /* Instruction needing the displacement */
1482      int numbits;               /* # bits of displacement (13 for COBR, 24 for CTRL) */
1483      int var_frag;              /* 1 if varying length code fragment should be emitted;
1484                  *      0 if an address fix should be emitted.
1485                  */
1486      int callj;                 /* 1 if callj relocation should be done; else 0 */
1487 {
1488   expressionS e;                /* Parsed expression */
1489   fixS *fixP;                   /* Structure describing needed address fix */
1490   char *outP;                   /* Where instruction binary is output to */
1491
1492   fixP = NULL;
1493
1494   switch (parse_expr (dispP, &e))
1495     {
1496
1497     case SEG_GOOF:
1498       as_bad ("expression syntax error");
1499       break;
1500
1501     case SEG_TEXT:
1502     case SEG_UNKNOWN:
1503       if (var_frag)
1504         {
1505           outP = frag_more (8); /* Allocate worst-case storage */
1506           md_number_to_chars (outP, instr, 4);
1507           frag_variant (rs_machine_dependent, 4, 4, 1,
1508                         adds (e), offs (e), outP, 0, 0);
1509         }
1510       else
1511         {
1512           /* Set up a new fix structure, so address can be updated
1513                          * when all symbol values are known.
1514                          */
1515           outP = emit (instr);
1516           fixP = fix_new (frag_now,
1517                           outP - frag_now->fr_literal,
1518                           4,
1519                           adds (e),
1520                           0,
1521                           offs (e),
1522                           1,
1523                           NO_RELOC);
1524
1525           fixP->fx_callj = callj;
1526
1527           /* We want to modify a bit field when the address is
1528                          * known.  But we don't need all the garbage in the
1529                          * bit_fix structure.  So we're going to lie and store
1530                          * the number of bits affected instead of a pointer.
1531                          */
1532           fixP->fx_bit_fixP = (bit_fixS *) numbits;
1533         }
1534       break;
1535
1536     case SEG_DATA:
1537     case SEG_BSS:
1538       as_bad ("attempt to branch into different segment");
1539       break;
1540
1541     default:
1542       as_bad ("target of %s instruction must be a label", ifmtP);
1543       break;
1544     }
1545 }
1546
1547
1548 /*****************************************************************************
1549  * get_ispec:   parse a memory operand for an index specification
1550  *
1551  *      Here, an "index specification" is taken to be anything surrounded
1552  *      by square brackets and NOT followed by anything else.
1553  *
1554  *      If it's found, detach it from the input string, remove the surrounding
1555  *      square brackets, and return a pointer to it.  Otherwise, return NULL.
1556  *
1557  **************************************************************************** */
1558 static
1559 char *
1560 get_ispec (textP)
1561      char *textP;               /*->memory operand from source instruction, no white space */
1562 {
1563   char *start;                  /*->start of index specification */
1564   char *end;                    /*->end of index specification */
1565
1566   /* Find opening square bracket, if any
1567          */
1568   start = strchr (textP, '[');
1569
1570   if (start != NULL)
1571     {
1572
1573       /* Eliminate '[', detach from rest of operand */
1574       *start++ = '\0';
1575
1576       end = strchr (start, ']');
1577
1578       if (end == NULL)
1579         {
1580           as_bad ("unmatched '['");
1581
1582         }
1583       else
1584         {
1585           /* Eliminate ']' and make sure it was the last thing
1586                          * in the string.
1587                          */
1588           *end = '\0';
1589           if (*(end + 1) != '\0')
1590             {
1591               as_bad ("garbage after index spec ignored");
1592             }
1593         }
1594     }
1595   return start;
1596 }
1597
1598 /*****************************************************************************
1599  * get_regnum:
1600  *
1601  *      Look up a (suspected) register name in the register table and return the
1602  *      associated register number (or -1 if not found).
1603  *
1604  **************************************************************************** */
1605 static
1606 int
1607 get_regnum (regname)
1608      char *regname;             /* Suspected register name */
1609 {
1610   int *rP;
1611
1612   rP = (int *) hash_find (reg_hash, regname);
1613   return (rP == NULL) ? -1 : *rP;
1614 }
1615
1616
1617 /*****************************************************************************
1618  * i_scan:      perform lexical scan of ascii assembler instruction.
1619  *
1620  * Input assumptions:
1621  *      - input string is an i80960 instruction (not a pseudo-op)
1622  *      - all comments and labels have been removed
1623  *      - all strings of whitespace have been collapsed to a single blank.
1624  *
1625  * Output:
1626  *      args[0] points to opcode, other entries point to operands. All strings:
1627  *      - are NULL-terminated
1628  *      - contain no whitespace
1629  *      - have character constants ('x') replaced with a decimal number
1630  *
1631  * Return value:
1632  *      Number of operands (0,1,2, or 3) or -1 on error.
1633  *
1634  **************************************************************************** */
1635 static int
1636 i_scan (iP, args)
1637      register char *iP;         /* Pointer to ascii instruction;  MUCKED BY US. */
1638      char *args[];              /* Output arg: pointers to opcode and operands placed
1639                  *      here.  MUST ACCOMMODATE 4 ENTRIES.
1640                  */
1641 {
1642
1643   /* Isolate opcode */
1644   if (*(iP) == ' ')
1645     {
1646       iP++;
1647     }                           /* Skip lead space, if any */
1648   args[0] = iP;
1649   for (; *iP != ' '; iP++)
1650     {
1651       if (*iP == '\0')
1652         {
1653           /* There are no operands */
1654           if (args[0] == iP)
1655             {
1656               /* We never moved: there was no opcode either! */
1657               as_bad ("missing opcode");
1658               return -1;
1659             }
1660           return 0;
1661         }
1662     }
1663   *iP++ = '\0';                 /* Terminate opcode */
1664   return (get_args (iP, args));
1665 }                               /* i_scan() */
1666
1667
1668 /*****************************************************************************
1669  * mem_fmt:     generate a MEMA- or MEMB-format instruction
1670  *
1671  **************************************************************************** */
1672 static void
1673 mem_fmt (args, oP, callx)
1674      char *args[];              /* args[0]->opcode mnemonic, args[1-3]->operands */
1675      struct i960_opcode *oP;    /* Pointer to description of instruction */
1676      int callx;                 /* Is this a callx opcode */
1677 {
1678   int i;                        /* Loop counter */
1679   struct regop regop;           /* Description of register operand */
1680   char opdesc;                  /* Operand descriptor byte */
1681   memS instr;                   /* Description of binary to be output */
1682   char *outP;                   /* Where the binary was output to */
1683   expressionS expr;             /* Parsed expression */
1684   fixS *fixP;                   /*->description of deferred address fixup */
1685
1686   memset (&instr, '\0', sizeof (memS));
1687   instr.opcode = oP->opcode;
1688
1689   /* Process operands. */
1690   for (i = 1; i <= oP->num_ops; i++)
1691     {
1692       opdesc = oP->operand[i - 1];
1693
1694       if (MEMOP (opdesc))
1695         {
1696           parse_memop (&instr, args[i], oP->format);
1697         }
1698       else
1699         {
1700           parse_regop (&regop, args[i], opdesc);
1701           instr.opcode |= regop.n << 19;
1702         }
1703     }
1704
1705   /* Output opcode */
1706   outP = emit (instr.opcode);
1707
1708   if (instr.disp == 0)
1709     {
1710       return;
1711     }
1712
1713   /* Parse and process the displacement */
1714   switch (parse_expr (instr.e, &expr))
1715     {
1716
1717     case SEG_GOOF:
1718       as_bad ("expression syntax error");
1719       break;
1720
1721     case SEG_ABSOLUTE:
1722       if (instr.disp == 32)
1723         {
1724           (void) emit (offs (expr));    /* Output displacement */
1725         }
1726       else
1727         {
1728           /* 12-bit displacement */
1729           if (offs (expr) & ~0xfff)
1730             {
1731               /* Won't fit in 12 bits: convert already-output
1732                                  * instruction to MEMB format, output
1733                                  * displacement.
1734                                  */
1735               mema_to_memb (outP);
1736               (void) emit (offs (expr));
1737             }
1738           else
1739             {
1740               /* WILL fit in 12 bits:  OR into opcode and
1741                                  * overwrite the binary we already put out
1742                                  */
1743               instr.opcode |= offs (expr);
1744               md_number_to_chars (outP, instr.opcode, 4);
1745             }
1746         }
1747       break;
1748
1749     case SEG_DIFFERENCE:
1750     case SEG_TEXT:
1751     case SEG_DATA:
1752     case SEG_BSS:
1753     case SEG_UNKNOWN:
1754       if (instr.disp == 12)
1755         {
1756           /* Displacement is dependent on a symbol, whose value
1757                          * may change at link time.  We HAVE to reserve 32 bits.
1758                          * Convert already-output opcode to MEMB format.
1759                          */
1760           mema_to_memb (outP);
1761         }
1762
1763       /* Output 0 displacement and set up address fixup for when
1764                  * this symbol's value becomes known.
1765                  */
1766       outP = emit ((long) 0);
1767       fixP = fix_new (frag_now,
1768                       outP - frag_now->fr_literal,
1769                       4,
1770                       adds (expr),
1771                       subs (expr),
1772                       offs (expr),
1773                       0,
1774                       NO_RELOC);
1775       fixP->fx_im_disp = 2;     /* 32-bit displacement fix */
1776       fixP->fx_bsr = callx;     /*SAC LD RELAX HACK *//* Mark reloc as being in i stream */
1777       break;
1778
1779     default:
1780       BAD_CASE (segs (expr));
1781       break;
1782     }
1783 }                               /* memfmt() */
1784
1785
1786 /*****************************************************************************
1787  * mema_to_memb:        convert a MEMA-format opcode to a MEMB-format opcode.
1788  *
1789  * There are 2 possible MEMA formats:
1790  *      - displacement only
1791  *      - displacement + abase
1792  *
1793  * They are distinguished by the setting of the MEMA_ABASE bit.
1794  *
1795  **************************************************************************** */
1796 static void
1797 mema_to_memb (opcodeP)
1798      char *opcodeP;             /* Where to find the opcode, in target byte order */
1799 {
1800   long opcode;                  /* Opcode in host byte order */
1801   long mode;                    /* Mode bits for MEMB instruction */
1802
1803   opcode = md_chars_to_number (opcodeP, 4);
1804   know (!(opcode & MEMB_BIT));
1805
1806   mode = MEMB_BIT | D_BIT;
1807   if (opcode & MEMA_ABASE)
1808     {
1809       mode |= A_BIT;
1810     }
1811
1812   opcode &= 0xffffc000;         /* Clear MEMA offset and mode bits */
1813   opcode |= mode;               /* Set MEMB mode bits */
1814
1815   md_number_to_chars (opcodeP, opcode, 4);
1816 }                               /* mema_to_memb() */
1817
1818
1819 /*****************************************************************************
1820  * parse_expr:          parse an expression
1821  *
1822  *      Use base assembler's expression parser to parse an expression.
1823  *      It, unfortunately, runs off a global which we have to save/restore
1824  *      in order to make it work for us.
1825  *
1826  *      An empty expression string is treated as an absolute 0.
1827  *
1828  *      Return "segment" to which the expression evaluates.
1829  *      Return SEG_GOOF regardless of expression evaluation if entire input
1830  *      string is not consumed in the evaluation -- tolerate no dangling junk!
1831  *
1832  **************************************************************************** */
1833 static
1834   segT
1835 parse_expr (textP, expP)
1836      char *textP;               /* Text of expression to be parsed */
1837      expressionS *expP;         /* Where to put the results of parsing */
1838 {
1839   char *save_in;                /* Save global here */
1840   segT seg;                     /* Segment to which expression evaluates */
1841   symbolS *symP;
1842
1843   know (textP);
1844
1845   if (*textP == '\0')
1846     {
1847       /* Treat empty string as absolute 0 */
1848       expP->X_add_symbol = expP->X_subtract_symbol = NULL;
1849       expP->X_add_number = 0;
1850       seg = expP->X_seg = SEG_ABSOLUTE;
1851
1852     }
1853   else
1854     {
1855       save_in = input_line_pointer;     /* Save global */
1856       input_line_pointer = textP;       /* Make parser work for us */
1857
1858       seg = expression (expP);
1859       if (input_line_pointer - textP != strlen (textP))
1860         {
1861           /* Did not consume all of the input */
1862           seg = SEG_GOOF;
1863         }
1864       symP = expP->X_add_symbol;
1865       if (symP && (hash_find (reg_hash, S_GET_NAME (symP))))
1866         {
1867           /* Register name in an expression */
1868           seg = SEG_GOOF;
1869         }
1870
1871       input_line_pointer = save_in;     /* Restore global */
1872     }
1873   return seg;
1874 }
1875
1876
1877 /*****************************************************************************
1878  * parse_ldcont:
1879  *      Parse and replace a 'ldconst' pseudo-instruction with an appropriate
1880  *      i80960 instruction.
1881  *
1882  *      Assumes the input consists of:
1883  *              arg[0]  opcode mnemonic ('ldconst')
1884  *              arg[1]  first operand (constant)
1885  *              arg[2]  name of register to be loaded
1886  *
1887  *      Replaces opcode and/or operands as appropriate.
1888  *
1889  *      Returns the new number of arguments, or -1 on failure.
1890  *
1891  **************************************************************************** */
1892 static
1893 int
1894 parse_ldconst (arg)
1895      char *arg[];               /* See above */
1896 {
1897   int n;                        /* Constant to be loaded */
1898   int shift;                    /* Shift count for "shlo" instruction */
1899   static char buf[5];           /* Literal for first operand */
1900   static char buf2[5];          /* Literal for second operand */
1901   expressionS e;                /* Parsed expression */
1902
1903
1904   arg[3] = NULL;                /* So we can tell at the end if it got used or not */
1905
1906   switch (parse_expr (arg[1], &e))
1907     {
1908
1909     case SEG_TEXT:
1910     case SEG_DATA:
1911     case SEG_BSS:
1912     case SEG_UNKNOWN:
1913     case SEG_DIFFERENCE:
1914       /* We're dependent on one or more symbols -- use "lda" */
1915       arg[0] = "lda";
1916       break;
1917
1918     case SEG_ABSOLUTE:
1919       /* Try the following mappings:
1920                  *      ldconst 0,<reg>  ->mov  0,<reg>
1921                  *      ldconst 31,<reg> ->mov  31,<reg>
1922                  *      ldconst 32,<reg> ->addo 1,31,<reg>
1923                  *      ldconst 62,<reg> ->addo 31,31,<reg>
1924                  *      ldconst 64,<reg> ->shlo 8,3,<reg>
1925                  *      ldconst -1,<reg> ->subo 1,0,<reg>
1926                  *      ldconst -31,<reg>->subo 31,0,<reg>
1927                  *
1928                  * anthing else becomes:
1929                  *      lda xxx,<reg>
1930                  */
1931       n = offs (e);
1932       if ((0 <= n) && (n <= 31))
1933         {
1934           arg[0] = "mov";
1935
1936         }
1937       else if ((-31 <= n) && (n <= -1))
1938         {
1939           arg[0] = "subo";
1940           arg[3] = arg[2];
1941           sprintf (buf, "%d", -n);
1942           arg[1] = buf;
1943           arg[2] = "0";
1944
1945         }
1946       else if ((32 <= n) && (n <= 62))
1947         {
1948           arg[0] = "addo";
1949           arg[3] = arg[2];
1950           arg[1] = "31";
1951           sprintf (buf, "%d", n - 31);
1952           arg[2] = buf;
1953
1954         }
1955       else if ((shift = shift_ok (n)) != 0)
1956         {
1957           arg[0] = "shlo";
1958           arg[3] = arg[2];
1959           sprintf (buf, "%d", shift);
1960           arg[1] = buf;
1961           sprintf (buf2, "%d", n >> shift);
1962           arg[2] = buf2;
1963
1964         }
1965       else
1966         {
1967           arg[0] = "lda";
1968         }
1969       break;
1970
1971     default:
1972       as_bad ("invalid constant");
1973       return -1;
1974       break;
1975     }
1976   return (arg[3] == 0) ? 2 : 3;
1977 }
1978
1979 /*****************************************************************************
1980  * parse_memop: parse a memory operand
1981  *
1982  *      This routine is based on the observation that the 4 mode bits of the
1983  *      MEMB format, taken individually, have fairly consistent meaning:
1984  *
1985  *               M3 (bit 13): 1 if displacement is present (D_BIT)
1986  *               M2 (bit 12): 1 for MEMB instructions (MEMB_BIT)
1987  *               M1 (bit 11): 1 if index is present (I_BIT)
1988  *               M0 (bit 10): 1 if abase is present (A_BIT)
1989  *
1990  *      So we parse the memory operand and set bits in the mode as we find
1991  *      things.  Then at the end, if we go to MEMB format, we need only set
1992  *      the MEMB bit (M2) and our mode is built for us.
1993  *
1994  *      Unfortunately, I said "fairly consistent".  The exceptions:
1995  *
1996  *               DBIA
1997  *               0100   Would seem illegal, but means "abase-only".
1998  *
1999  *               0101   Would seem to mean "abase-only" -- it means IP-relative.
2000  *                      Must be converted to 0100.
2001  *
2002  *               0110   Would seem to mean "index-only", but is reserved.
2003  *                      We turn on the D bit and provide a 0 displacement.
2004  *
2005  *      The other thing to observe is that we parse from the right, peeling
2006  *      things * off as we go:  first any index spec, then any abase, then
2007  *      the displacement.
2008  *
2009  **************************************************************************** */
2010 static
2011 void
2012 parse_memop (memP, argP, optype)
2013      memS *memP;                /* Where to put the results */
2014      char *argP;                /* Text of the operand to be parsed */
2015      int optype;                /* MEM1, MEM2, MEM4, MEM8, MEM12, or MEM16 */
2016 {
2017   char *indexP;                 /* Pointer to index specification with "[]" removed */
2018   char *p;                      /* Temp char pointer */
2019   char iprel_flag;              /* True if this is an IP-relative operand */
2020   int regnum;                   /* Register number */
2021   int scale;                    /* Scale factor: 1,2,4,8, or 16.  Later converted
2022                          *      to internal format (0,1,2,3,4 respectively).
2023                          */
2024   int mode;                     /* MEMB mode bits */
2025   int *intP;                    /* Pointer to register number */
2026
2027   /* The following table contains the default scale factors for each
2028          * type of memory instruction.  It is accessed using (optype-MEM1)
2029          * as an index -- thus it assumes the 'optype' constants are assigned
2030          * consecutive values, in the order they appear in this table
2031          */
2032   static int def_scale[] =
2033   {
2034     1,                          /* MEM1 */
2035     2,                          /* MEM2 */
2036     4,                          /* MEM4 */
2037     8,                          /* MEM8 */
2038     -1,                         /* MEM12 -- no valid default */
2039     16                          /* MEM16 */
2040   };
2041
2042
2043   iprel_flag = mode = 0;
2044
2045   /* Any index present? */
2046   indexP = get_ispec (argP);
2047   if (indexP)
2048     {
2049       p = strchr (indexP, '*');
2050       if (p == NULL)
2051         {
2052           /* No explicit scale -- use default for this
2053                          *instruction type.
2054                          */
2055           scale = def_scale[optype - MEM1];
2056         }
2057       else
2058         {
2059           *p++ = '\0';          /* Eliminate '*' */
2060
2061           /* Now indexP->a '\0'-terminated register name,
2062                          * and p->a scale factor.
2063                          */
2064
2065           if (!strcmp (p, "16"))
2066             {
2067               scale = 16;
2068             }
2069           else if (strchr ("1248", *p) && (p[1] == '\0'))
2070             {
2071               scale = *p - '0';
2072             }
2073           else
2074             {
2075               scale = -1;
2076             }
2077         }
2078
2079       regnum = get_regnum (indexP);     /* Get index reg. # */
2080       if (!IS_RG_REG (regnum))
2081         {
2082           as_bad ("invalid index register");
2083           return;
2084         }
2085
2086       /* Convert scale to its binary encoding */
2087       switch (scale)
2088         {
2089         case 1:
2090           scale = 0 << 7;
2091           break;
2092         case 2:
2093           scale = 1 << 7;
2094           break;
2095         case 4:
2096           scale = 2 << 7;
2097           break;
2098         case 8:
2099           scale = 3 << 7;
2100           break;
2101         case 16:
2102           scale = 4 << 7;
2103           break;
2104         default:
2105           as_bad ("invalid scale factor");
2106           return;
2107         };
2108
2109       memP->opcode |= scale | regnum;   /* Set index bits in opcode */
2110       mode |= I_BIT;            /* Found a valid index spec */
2111     }
2112
2113   /* Any abase (Register Indirect) specification present? */
2114   if ((p = strrchr (argP, '(')) != NULL)
2115     {
2116       /* "(" is there -- does it start a legal abase spec?
2117                  * (If not it could be part of a displacement expression.)
2118                  */
2119       intP = (int *) hash_find (areg_hash, p);
2120       if (intP != NULL)
2121         {
2122           /* Got an abase here */
2123           regnum = *intP;
2124           *p = '\0';            /* discard register spec */
2125           if (regnum == IPREL)
2126             {
2127               /* We have to specialcase ip-rel mode */
2128               iprel_flag = 1;
2129             }
2130           else
2131             {
2132               memP->opcode |= regnum << 14;
2133               mode |= A_BIT;
2134             }
2135         }
2136     }
2137
2138   /* Any expression present? */
2139   memP->e = argP;
2140   if (*argP != '\0')
2141     {
2142       mode |= D_BIT;
2143     }
2144
2145   /* Special-case ip-relative addressing */
2146   if (iprel_flag)
2147     {
2148       if (mode & I_BIT)
2149         {
2150           syntax ();
2151         }
2152       else
2153         {
2154           memP->opcode |= 5 << 10;      /* IP-relative mode */
2155           memP->disp = 32;
2156         }
2157       return;
2158     }
2159
2160   /* Handle all other modes */
2161   switch (mode)
2162     {
2163     case D_BIT | A_BIT:
2164       /* Go with MEMA instruction format for now (grow to MEMB later
2165                  *      if 12 bits is not enough for the displacement).
2166                  * MEMA format has a single mode bit: set it to indicate
2167                  *      that abase is present.
2168                  */
2169       memP->opcode |= MEMA_ABASE;
2170       memP->disp = 12;
2171       break;
2172
2173     case D_BIT:
2174       /* Go with MEMA instruction format for now (grow to MEMB later
2175                  *      if 12 bits is not enough for the displacement).
2176                  */
2177       memP->disp = 12;
2178       break;
2179
2180     case A_BIT:
2181       /* For some reason, the bit string for this mode is not
2182                  * consistent:  it should be 0 (exclusive of the MEMB bit),
2183                  * so we set it "by hand" here.
2184                  */
2185       memP->opcode |= MEMB_BIT;
2186       break;
2187
2188     case A_BIT | I_BIT:
2189       /* set MEMB bit in mode, and OR in mode bits */
2190       memP->opcode |= mode | MEMB_BIT;
2191       break;
2192
2193     case I_BIT:
2194       /* Treat missing displacement as displacement of 0 */
2195       mode |= D_BIT;
2196       /***********************
2197                  * Fall into next case *
2198                  ********************** */
2199     case D_BIT | A_BIT | I_BIT:
2200     case D_BIT | I_BIT:
2201       /* set MEMB bit in mode, and OR in mode bits */
2202       memP->opcode |= mode | MEMB_BIT;
2203       memP->disp = 32;
2204       break;
2205
2206     default:
2207       syntax ();
2208       break;
2209     }
2210 }
2211
2212 /*****************************************************************************
2213  * parse_po:    parse machine-dependent pseudo-op
2214  *
2215  *      This is a top-level routine for machine-dependent pseudo-ops.  It slurps
2216  *      up the rest of the input line, breaks out the individual arguments,
2217  *      and dispatches them to the correct handler.
2218  **************************************************************************** */
2219 static
2220 void
2221 parse_po (po_num)
2222      int po_num;                /* Pseudo-op number:  currently S_LEAFPROC or S_SYSPROC */
2223 {
2224   char *args[4];                /* Pointers operands, with no embedded whitespace.
2225                          *      arg[0] unused.
2226                          *      arg[1-3]->operands
2227                          */
2228   int n_ops;                    /* Number of operands */
2229   char *p;                      /* Pointer to beginning of unparsed argument string */
2230   char eol;                     /* Character that indicated end of line */
2231
2232   extern char is_end_of_line[];
2233
2234   /* Advance input pointer to end of line. */
2235   p = input_line_pointer;
2236   while (!is_end_of_line[*input_line_pointer])
2237     {
2238       input_line_pointer++;
2239     }
2240   eol = *input_line_pointer;    /* Save end-of-line char */
2241   *input_line_pointer = '\0';   /* Terminate argument list */
2242
2243   /* Parse out operands */
2244   n_ops = get_args (p, args);
2245   if (n_ops == -1)
2246     {
2247       return;
2248     }
2249
2250   /* Dispatch to correct handler */
2251   switch (po_num)
2252     {
2253     case S_SYSPROC:
2254       s_sysproc (n_ops, args);
2255       break;
2256     case S_LEAFPROC:
2257       s_leafproc (n_ops, args);
2258       break;
2259     default:
2260       BAD_CASE (po_num);
2261       break;
2262     }
2263
2264   /* Restore eol, so line numbers get updated correctly.  Base assembler
2265          * assumes we leave input pointer pointing at char following the eol.
2266          */
2267   *input_line_pointer++ = eol;
2268 }
2269
2270 /*****************************************************************************
2271  * parse_regop: parse a register operand.
2272  *
2273  *      In case of illegal operand, issue a message and return some valid
2274  *      information so instruction processing can continue.
2275  **************************************************************************** */
2276 static
2277 void
2278 parse_regop (regopP, optext, opdesc)
2279      struct regop *regopP;      /* Where to put description of register operand */
2280      char *optext;              /* Text of operand */
2281      char opdesc;               /* Descriptor byte:  what's legal for this operand */
2282 {
2283   int n;                        /* Register number */
2284   expressionS e;                /* Parsed expression */
2285
2286   /* See if operand is a register */
2287   n = get_regnum (optext);
2288   if (n >= 0)
2289     {
2290       if (IS_RG_REG (n))
2291         {
2292           /* global or local register */
2293           if (!REG_ALIGN (opdesc, n))
2294             {
2295               as_bad ("unaligned register");
2296             }
2297           regopP->n = n;
2298           regopP->mode = 0;
2299           regopP->special = 0;
2300           return;
2301         }
2302       else if (IS_FP_REG (n) && FP_OK (opdesc))
2303         {
2304           /* Floating point register, and it's allowed */
2305           regopP->n = n - FP0;
2306           regopP->mode = 1;
2307           regopP->special = 0;
2308           return;
2309         }
2310       else if (IS_SF_REG (n) && SFR_OK (opdesc))
2311         {
2312           /* Special-function register, and it's allowed */
2313           regopP->n = n - SF0;
2314           regopP->mode = 0;
2315           regopP->special = 1;
2316           if (!targ_has_sfr (regopP->n))
2317             {
2318               as_bad ("no such sfr in this architecture");
2319             }
2320           return;
2321         }
2322     }
2323   else if (LIT_OK (opdesc))
2324     {
2325       /*
2326                  * How about a literal?
2327                  */
2328       regopP->mode = 1;
2329       regopP->special = 0;
2330       if (FP_OK (opdesc))
2331         {                       /* floating point literal acceptable */
2332           /* Skip over 0f, 0d, or 0e prefix */
2333           if ((optext[0] == '0')
2334               && (optext[1] >= 'd')
2335               && (optext[1] <= 'f'))
2336             {
2337               optext += 2;
2338             }
2339
2340           if (!strcmp (optext, "0.0") || !strcmp (optext, "0"))
2341             {
2342               regopP->n = 0x10;
2343               return;
2344             }
2345           if (!strcmp (optext, "1.0") || !strcmp (optext, "1"))
2346             {
2347               regopP->n = 0x16;
2348               return;
2349             }
2350
2351         }
2352       else
2353         {                       /* fixed point literal acceptable */
2354           if ((parse_expr (optext, &e) != SEG_ABSOLUTE)
2355               || (offs (e) < 0) || (offs (e) > 31))
2356             {
2357               as_bad ("illegal literal");
2358               offs (e) = 0;
2359             }
2360           regopP->n = offs (e);
2361           return;
2362         }
2363     }
2364
2365   /* Nothing worked */
2366   syntax ();
2367   regopP->mode = 0;             /* Register r0 is always a good one */
2368   regopP->n = 0;
2369   regopP->special = 0;
2370 }                               /* parse_regop() */
2371
2372 /*****************************************************************************
2373  * reg_fmt:     generate a REG-format instruction
2374  *
2375  **************************************************************************** */
2376 static void
2377 reg_fmt (args, oP)
2378      char *args[];              /* args[0]->opcode mnemonic, args[1-3]->operands */
2379      struct i960_opcode *oP;    /* Pointer to description of instruction */
2380 {
2381   long instr;                   /* Binary to be output */
2382   struct regop regop;           /* Description of register operand */
2383   int n_ops;                    /* Number of operands */
2384
2385
2386   instr = oP->opcode;
2387   n_ops = oP->num_ops;
2388
2389   if (n_ops >= 1)
2390     {
2391       parse_regop (&regop, args[1], oP->operand[0]);
2392
2393       if ((n_ops == 1) && !(instr & M3))
2394         {
2395           /* 1-operand instruction in which the dst field should
2396                          * be used (instead of src1).
2397                          */
2398           regop.n <<= 19;
2399           if (regop.special)
2400             {
2401               regop.mode = regop.special;
2402             }
2403           regop.mode <<= 13;
2404           regop.special = 0;
2405         }
2406       else
2407         {
2408           /* regop.n goes in bit 0, needs no shifting */
2409           regop.mode <<= 11;
2410           regop.special <<= 5;
2411         }
2412       instr |= regop.n | regop.mode | regop.special;
2413     }
2414
2415   if (n_ops >= 2)
2416     {
2417       parse_regop (&regop, args[2], oP->operand[1]);
2418
2419       if ((n_ops == 2) && !(instr & M3))
2420         {
2421           /* 2-operand instruction in which the dst field should
2422                          * be used instead of src2).
2423                          */
2424           regop.n <<= 19;
2425           if (regop.special)
2426             {
2427               regop.mode = regop.special;
2428             }
2429           regop.mode <<= 13;
2430           regop.special = 0;
2431         }
2432       else
2433         {
2434           regop.n <<= 14;
2435           regop.mode <<= 12;
2436           regop.special <<= 6;
2437         }
2438       instr |= regop.n | regop.mode | regop.special;
2439     }
2440   if (n_ops == 3)
2441     {
2442       parse_regop (&regop, args[3], oP->operand[2]);
2443       if (regop.special)
2444         {
2445           regop.mode = regop.special;
2446         }
2447       instr |= (regop.n <<= 19) | (regop.mode <<= 13);
2448     }
2449   emit (instr);
2450 }
2451
2452
2453 /*****************************************************************************
2454  * relax_cobr:
2455  *      Replace cobr instruction in a code fragment with equivalent branch and
2456  *      compare instructions, so it can reach beyond a 13-bit displacement.
2457  *      Set up an address fix/relocation for the new branch instruction.
2458  *
2459  **************************************************************************** */
2460
2461 /* This "conditional jump" table maps cobr instructions into equivalent
2462  * compare and branch opcodes.
2463  */
2464 static
2465 struct
2466   {
2467     long compare;
2468     long branch;
2469   }
2470
2471 coj[] =
2472 {                               /* COBR OPCODE: */
2473   CHKBIT, BNO,                  /*      0x30 - bbc */
2474   CMPO, BG,                     /*      0x31 - cmpobg */
2475   CMPO, BE,                     /*      0x32 - cmpobe */
2476   CMPO, BGE,                    /*      0x33 - cmpobge */
2477   CMPO, BL,                     /*      0x34 - cmpobl */
2478   CMPO, BNE,                    /*      0x35 - cmpobne */
2479   CMPO, BLE,                    /*      0x36 - cmpoble */
2480   CHKBIT, BO,                   /*      0x37 - bbs */
2481   CMPI, BNO,                    /*      0x38 - cmpibno */
2482   CMPI, BG,                     /*      0x39 - cmpibg */
2483   CMPI, BE,                     /*      0x3a - cmpibe */
2484   CMPI, BGE,                    /*      0x3b - cmpibge */
2485   CMPI, BL,                     /*      0x3c - cmpibl */
2486   CMPI, BNE,                    /*      0x3d - cmpibne */
2487   CMPI, BLE,                    /*      0x3e - cmpible */
2488   CMPI, BO,                     /*      0x3f - cmpibo */
2489 };
2490
2491 static
2492 void
2493 relax_cobr (fragP)
2494      register fragS *fragP;     /* fragP->fr_opcode is assumed to point to
2495                          * the cobr instruction, which comes at the
2496                          * end of the code fragment.
2497                          */
2498 {
2499   int opcode, src1, src2, m1, s2;
2500   /* Bit fields from cobr instruction */
2501   long bp_bits;                 /* Branch prediction bits from cobr instruction */
2502   long instr;                   /* A single i960 instruction */
2503   char *iP;                     /*->instruction to be replaced */
2504   fixS *fixP;                   /* Relocation that can be done at assembly time */
2505
2506   /* PICK UP & PARSE COBR INSTRUCTION */
2507   iP = fragP->fr_opcode;
2508   instr = md_chars_to_number (iP, 4);
2509   opcode = ((instr >> 24) & 0xff) - 0x30;       /* "-0x30" for table index */
2510   src1 = (instr >> 19) & 0x1f;
2511   m1 = (instr >> 13) & 1;
2512   s2 = instr & 1;
2513   src2 = (instr >> 14) & 0x1f;
2514   bp_bits = instr & BP_MASK;
2515
2516   /* GENERATE AND OUTPUT COMPARE INSTRUCTION */
2517   instr = coj[opcode].compare
2518     | src1 | (m1 << 11) | (s2 << 6) | (src2 << 14);
2519   md_number_to_chars (iP, instr, 4);
2520
2521   /* OUTPUT BRANCH INSTRUCTION */
2522   md_number_to_chars (iP + 4, coj[opcode].branch | bp_bits, 4);
2523
2524   /* SET UP ADDRESS FIXUP/RELOCATION */
2525   fixP = fix_new (fragP,
2526                   iP + 4 - fragP->fr_literal,
2527                   4,
2528                   fragP->fr_symbol,
2529                   0,
2530                   fragP->fr_offset,
2531                   1,
2532                   NO_RELOC);
2533
2534   fixP->fx_bit_fixP = (bit_fixS *) 24;  /* Store size of bit field */
2535
2536   fragP->fr_fix += 4;
2537   frag_wane (fragP);
2538 }
2539
2540
2541 /*****************************************************************************
2542  * reloc_callj: Relocate a 'callj' instruction
2543  *
2544  *      This is a "non-(GNU)-standard" machine-dependent hook.  The base
2545  *      assembler calls it when it decides it can relocate an address at
2546  *      assembly time instead of emitting a relocation directive.
2547  *
2548  *      Check to see if the relocation involves a 'callj' instruction to a:
2549  *          sysproc:    Replace the default 'call' instruction with a 'calls'
2550  *          leafproc:   Replace the default 'call' instruction with a 'bal'.
2551  *          other proc: Do nothing.
2552  *
2553  *      See b.out.h for details on the 'n_other' field in a symbol structure.
2554  *
2555  * IMPORTANT!:
2556  *      Assumes the caller has already figured out, in the case of a leafproc,
2557  *      to use the 'bal' entry point, and has substituted that symbol into the
2558  *      passed fixup structure.
2559  *
2560  **************************************************************************** */
2561 void
2562 reloc_callj (fixP)
2563      fixS *fixP;                /* Relocation that can be done at assembly time */
2564 {
2565   char *where;                  /*->the binary for the instruction being relocated */
2566
2567   if (!fixP->fx_callj)
2568     {
2569       return;
2570     }                           /* This wasn't a callj instruction in the first place */
2571
2572   where = fixP->fx_frag->fr_literal + fixP->fx_where;
2573
2574   if (TC_S_IS_SYSPROC (fixP->fx_addsy))
2575     {
2576       /* Symbol is a .sysproc: replace 'call' with 'calls'.
2577                  * System procedure number is (other-1).
2578                  */
2579       md_number_to_chars (where, CALLS | TC_S_GET_SYSPROC (fixP->fx_addsy), 4);
2580
2581       /* Nothing else needs to be done for this instruction.
2582                  * Make sure 'md_number_to_field()' will perform a no-op.
2583                  */
2584       fixP->fx_bit_fixP = (bit_fixS *) 1;
2585
2586     }
2587   else if (TC_S_IS_CALLNAME (fixP->fx_addsy))
2588     {
2589       /* Should not happen: see block comment above */
2590       as_fatal ("Trying to 'bal' to %s", S_GET_NAME (fixP->fx_addsy));
2591
2592     }
2593   else if (TC_S_IS_BALNAME (fixP->fx_addsy))
2594     {
2595       /* Replace 'call' with 'bal';  both instructions have
2596                  * the same format, so calling code should complete
2597                  * relocation as if nothing happened here.
2598                  */
2599       md_number_to_chars (where, BAL, 4);
2600     }
2601   else if (TC_S_IS_BADPROC (fixP->fx_addsy))
2602     {
2603       as_bad ("Looks like a proc, but can't tell what kind.\n");
2604     }                           /* switch on proc type */
2605
2606   /* else Symbol is neither a sysproc nor a leafproc */
2607
2608   return;
2609 }                               /* reloc_callj() */
2610
2611
2612 /*****************************************************************************
2613  * s_leafproc:  process .leafproc pseudo-op
2614  *
2615  *      .leafproc takes two arguments, the second one is optional:
2616  *              arg[1]: name of 'call' entry point to leaf procedure
2617  *              arg[2]: name of 'bal' entry point to leaf procedure
2618  *
2619  *      If the two arguments are identical, or if the second one is missing,
2620  *      the first argument is taken to be the 'bal' entry point.
2621  *
2622  *      If there are 2 distinct arguments, we must make sure that the 'bal'
2623  *      entry point immediately follows the 'call' entry point in the linked
2624  *      list of symbols.
2625  *
2626  **************************************************************************** */
2627 static void
2628 s_leafproc (n_ops, args)
2629      int n_ops;                 /* Number of operands */
2630      char *args[];              /* args[1]->1st operand, args[2]->2nd operand */
2631 {
2632   symbolS *callP;               /* Pointer to leafproc 'call' entry point symbol */
2633   symbolS *balP;                /* Pointer to leafproc 'bal' entry point symbol */
2634
2635   if ((n_ops != 1) && (n_ops != 2))
2636     {
2637       as_bad ("should have 1 or 2 operands");
2638       return;
2639     }                           /* Check number of arguments */
2640
2641   /* Find or create symbol for 'call' entry point. */
2642   callP = symbol_find_or_make (args[1]);
2643
2644   if (TC_S_IS_CALLNAME (callP))
2645     {
2646       as_warn ("Redefining leafproc %s", S_GET_NAME (callP));
2647     }                           /* is leafproc */
2648
2649   /* If that was the only argument, use it as the 'bal' entry point.
2650          * Otherwise, mark it as the 'call' entry point and find or create
2651          * another symbol for the 'bal' entry point.
2652          */
2653   if ((n_ops == 1) || !strcmp (args[1], args[2]))
2654     {
2655       TC_S_FORCE_TO_BALNAME (callP);
2656
2657     }
2658   else
2659     {
2660       TC_S_FORCE_TO_CALLNAME (callP);
2661
2662       balP = symbol_find_or_make (args[2]);
2663       if (TC_S_IS_CALLNAME (balP))
2664         {
2665           as_warn ("Redefining leafproc %s", S_GET_NAME (balP));
2666         }
2667       TC_S_FORCE_TO_BALNAME (balP);
2668
2669       tc_set_bal_of_call (callP, balP);
2670     }                           /* if only one arg, or the args are the same */
2671
2672   return;
2673 }                               /* s_leafproc() */
2674
2675
2676 /*
2677  * s_sysproc:   process .sysproc pseudo-op
2678  *
2679  *      .sysproc takes two arguments:
2680  *              arg[1]: name of entry point to system procedure
2681  *              arg[2]: 'entry_num' (index) of system procedure in the range
2682  *                      [0,31] inclusive.
2683  *
2684  *      For [ab].out, we store the 'entrynum' in the 'n_other' field of
2685  *      the symbol.  Since that entry is normally 0, we bias 'entrynum'
2686  *      by adding 1 to it.  It must be unbiased before it is used.
2687  */
2688 static void
2689 s_sysproc (n_ops, args)
2690      int n_ops;                 /* Number of operands */
2691      char *args[];              /* args[1]->1st operand, args[2]->2nd operand */
2692 {
2693   expressionS exp;
2694   symbolS *symP;
2695
2696   if (n_ops != 2)
2697     {
2698       as_bad ("should have two operands");
2699       return;
2700     }                           /* bad arg count */
2701
2702   /* Parse "entry_num" argument and check it for validity. */
2703   if ((parse_expr (args[2], &exp) != SEG_ABSOLUTE)
2704       || (offs (exp) < 0)
2705       || (offs (exp) > 31))
2706     {
2707       as_bad ("'entry_num' must be absolute number in [0,31]");
2708       return;
2709     }
2710
2711   /* Find/make symbol and stick entry number (biased by +1) into it */
2712   symP = symbol_find_or_make (args[1]);
2713
2714   if (TC_S_IS_SYSPROC (symP))
2715     {
2716       as_warn ("Redefining entrynum for sysproc %s", S_GET_NAME (symP));
2717     }                           /* redefining */
2718
2719   TC_S_SET_SYSPROC (symP, offs (exp));  /* encode entry number */
2720   TC_S_FORCE_TO_SYSPROC (symP);
2721
2722   return;
2723 }                               /* s_sysproc() */
2724
2725
2726 /*****************************************************************************
2727  * shift_ok:
2728  *      Determine if a "shlo" instruction can be used to implement a "ldconst".
2729  *      This means that some number X < 32 can be shifted left to produce the
2730  *      constant of interest.
2731  *
2732  *      Return the shift count, or 0 if we can't do it.
2733  *      Caller calculates X by shifting original constant right 'shift' places.
2734  *
2735  **************************************************************************** */
2736 static
2737 int
2738 shift_ok (n)
2739      int n;                     /* The constant of interest */
2740 {
2741   int shift;                    /* The shift count */
2742
2743   if (n <= 0)
2744     {
2745       /* Can't do it for negative numbers */
2746       return 0;
2747     }
2748
2749   /* Shift 'n' right until a 1 is about to be lost */
2750   for (shift = 0; (n & 1) == 0; shift++)
2751     {
2752       n >>= 1;
2753     }
2754
2755   if (n >= 32)
2756     {
2757       return 0;
2758     }
2759   return shift;
2760 }
2761
2762
2763 /*****************************************************************************
2764  * syntax:      issue syntax error
2765  *
2766  **************************************************************************** */
2767 static void
2768 syntax ()
2769 {
2770   as_bad ("syntax error");
2771 }                               /* syntax() */
2772
2773
2774 /*****************************************************************************
2775  * targ_has_sfr:
2776  *      Return TRUE iff the target architecture supports the specified
2777  *      special-function register (sfr).
2778  *
2779  **************************************************************************** */
2780 static
2781 int
2782 targ_has_sfr (n)
2783      int n;                     /* Number (0-31) of sfr */
2784 {
2785   switch (architecture)
2786     {
2787     case ARCH_KA:
2788     case ARCH_KB:
2789     case ARCH_MC:
2790       return 0;
2791     case ARCH_CA:
2792     default:
2793       return ((0 <= n) && (n <= 2));
2794     }
2795 }
2796
2797
2798 /*****************************************************************************
2799  * targ_has_iclass:
2800  *      Return TRUE iff the target architecture supports the indicated
2801  *      class of instructions.
2802  *
2803  **************************************************************************** */
2804 static
2805 int
2806 targ_has_iclass (ic)
2807      int ic;                    /* Instruction class;  one of:
2808          *      I_BASE, I_CX, I_DEC, I_KX, I_FP, I_MIL, I_CASIM
2809          */
2810 {
2811   iclasses_seen |= ic;
2812   switch (architecture)
2813     {
2814     case ARCH_KA:
2815       return ic & (I_BASE | I_KX);
2816     case ARCH_KB:
2817       return ic & (I_BASE | I_KX | I_FP | I_DEC);
2818     case ARCH_MC:
2819       return ic & (I_BASE | I_KX | I_FP | I_DEC | I_MIL);
2820     case ARCH_CA:
2821       return ic & (I_BASE | I_CX | I_CASIM);
2822     default:
2823       if ((iclasses_seen & (I_KX | I_FP | I_DEC | I_MIL))
2824           && (iclasses_seen & I_CX))
2825         {
2826           as_warn ("architecture of opcode conflicts with that of earlier instruction(s)");
2827           iclasses_seen &= ~ic;
2828         }
2829       return 1;
2830     }
2831 }
2832
2833
2834 /* Parse an operand that is machine-specific.
2835    We just return without modifying the expression if we have nothing
2836    to do. */
2837
2838 /* ARGSUSED */
2839 void
2840 md_operand (expressionP)
2841      expressionS *expressionP;
2842 {
2843 }
2844
2845 /* We have no need to default values of symbols. */
2846
2847 /* ARGSUSED */
2848 symbolS *
2849 md_undefined_symbol (name)
2850      char *name;
2851 {
2852   return 0;
2853 }                               /* md_undefined_symbol() */
2854
2855 /* Exactly what point is a PC-relative offset relative TO?
2856    On the i960, they're relative to the address of the instruction,
2857    which we have set up as the address of the fixup too. */
2858 long
2859 md_pcrel_from (fixP)
2860      fixS *fixP;
2861 {
2862   return fixP->fx_where + fixP->fx_frag->fr_address;
2863 }
2864
2865 void
2866 md_apply_fix (fixP, val)
2867      fixS *fixP;
2868      long val;
2869 {
2870   char *place = fixP->fx_where + fixP->fx_frag->fr_literal;
2871
2872   if (!fixP->fx_bit_fixP)
2873     {
2874
2875       switch (fixP->fx_im_disp)
2876         {
2877         case 0:
2878           fixP->fx_addnumber = val;
2879           md_number_to_imm (place, val, fixP->fx_size, fixP);
2880           break;
2881         case 1:
2882           md_number_to_disp (place,
2883                          fixP->fx_pcrel ? val + fixP->fx_pcrel_adjust : val,
2884                              fixP->fx_size);
2885           break;
2886         case 2:         /* fix requested for .long .word etc */
2887           md_number_to_chars (place, val, fixP->fx_size);
2888           break;
2889         default:
2890           as_fatal ("Internal error in md_apply_fix() in file \"%s\"", __FILE__);
2891         }                       /* OVE: maybe one ought to put _imm _disp _chars in one md-func */
2892     }
2893   else
2894     {
2895       md_number_to_field (place, val, fixP->fx_bit_fixP);
2896     }
2897
2898   return;
2899 }                               /* md_apply_fix() */
2900
2901 #if defined(OBJ_AOUT) | defined(OBJ_BOUT)
2902 void
2903 tc_bout_fix_to_chars (where, fixP, segment_address_in_file)
2904      char *where;
2905      fixS *fixP;
2906      relax_addressT segment_address_in_file;
2907 {
2908   static unsigned char nbytes_r_length[] =
2909   {42, 0, 1, 42, 2};
2910   struct relocation_info ri;
2911   symbolS *symbolP;
2912
2913   /* JF this is for paranoia */
2914   memset ((char *) &ri, '\0', sizeof (ri));
2915   symbolP = fixP->fx_addsy;
2916   know (symbolP != 0 || fixP->fx_r_type != NO_RELOC);
2917   ri.r_bsr = fixP->fx_bsr;      /*SAC LD RELAX HACK */
2918   /* These two 'cuz of NS32K */
2919   ri.r_callj = fixP->fx_callj;
2920   if (fixP->fx_bit_fixP)
2921     {
2922       ri.r_length = 2;
2923     }
2924   else
2925     {
2926       ri.r_length = nbytes_r_length[fixP->fx_size];
2927     }
2928   ri.r_pcrel = fixP->fx_pcrel;
2929   ri.r_address = fixP->fx_frag->fr_address + fixP->fx_where - segment_address_in_file;
2930
2931   if (fixP->fx_r_type != NO_RELOC)
2932     {
2933       switch (fixP->fx_r_type)
2934         {
2935         case rs_align:
2936           ri.r_index = -2;
2937           ri.r_pcrel = 1;
2938           ri.r_length = fixP->fx_size - 1;
2939           break;
2940         case rs_org:
2941           ri.r_index = -2;
2942           ri.r_pcrel = 0;
2943           break;
2944         case rs_fill:
2945           ri.r_index = -1;
2946           break;
2947         default:
2948           abort ();
2949         }
2950       ri.r_extern = 0;
2951     }
2952   else if (linkrelax || !S_IS_DEFINED (symbolP))
2953     {
2954       ri.r_extern = 1;
2955       ri.r_index = symbolP->sy_number;
2956     }
2957   else
2958     {
2959       ri.r_extern = 0;
2960       ri.r_index = S_GET_TYPE (symbolP);
2961     }
2962
2963   /* Output the relocation information in machine-dependent form. */
2964   md_ri_to_chars (where, &ri);
2965
2966   return;
2967 }                               /* tc_bout_fix_to_chars() */
2968
2969 #endif /* OBJ_AOUT or OBJ_BOUT */
2970
2971 /* Align an address by rounding it up to the specified boundary.
2972  */
2973 long
2974 md_section_align (seg, addr)
2975      segT seg;
2976      long addr;                 /* Address to be rounded up */
2977 {
2978   return ((addr + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg]));
2979 }                               /* md_section_align() */
2980
2981 #ifdef OBJ_COFF
2982 void
2983 tc_headers_hook (headers)
2984      object_headers *headers;
2985 {
2986   /* FIXME: remove this line *//*       unsigned short arch_flag = 0; */
2987
2988   if (iclasses_seen == I_BASE)
2989     {
2990       headers->filehdr.f_flags |= F_I960CORE;
2991     }
2992   else if (iclasses_seen & I_CX)
2993     {
2994       headers->filehdr.f_flags |= F_I960CA;
2995     }
2996   else if (iclasses_seen & I_MIL)
2997     {
2998       headers->filehdr.f_flags |= F_I960MC;
2999     }
3000   else if (iclasses_seen & (I_DEC | I_FP))
3001     {
3002       headers->filehdr.f_flags |= F_I960KB;
3003     }
3004   else
3005     {
3006       headers->filehdr.f_flags |= F_I960KA;
3007     }                           /* set arch flag */
3008
3009   if (flagseen['R'])
3010     {
3011       headers->filehdr.f_magic = I960RWMAGIC;
3012       headers->aouthdr.magic = OMAGIC;
3013     }
3014   else
3015     {
3016       headers->filehdr.f_magic = I960ROMAGIC;
3017       headers->aouthdr.magic = NMAGIC;
3018     }                           /* set magic numbers */
3019
3020   return;
3021 }                               /* tc_headers_hook() */
3022
3023 #endif /* OBJ_COFF */
3024
3025 /*
3026  * Things going on here:
3027  *
3028  * For bout, We need to assure a couple of simplifying
3029  * assumptions about leafprocs for the linker: the leafproc
3030  * entry symbols will be defined in the same assembly in
3031  * which they're declared with the '.leafproc' directive;
3032  * and if a leafproc has both 'call' and 'bal' entry points
3033  * they are both global or both local.
3034  *
3035  * For coff, the call symbol has a second aux entry that
3036  * contains the bal entry point.  The bal symbol becomes a
3037  * label.
3038  *
3039  * For coff representation, the call symbol has a second aux entry that
3040  * contains the bal entry point.  The bal symbol becomes a label.
3041  *
3042  */
3043
3044 void
3045 tc_crawl_symbol_chain (headers)
3046      object_headers *headers;
3047 {
3048   symbolS *symbolP;
3049
3050   for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP))
3051     {
3052 #ifdef OBJ_COFF
3053       if (TC_S_IS_SYSPROC (symbolP))
3054         {
3055           /* second aux entry already contains the sysproc number */
3056           S_SET_NUMBER_AUXILIARY (symbolP, 2);
3057           S_SET_STORAGE_CLASS (symbolP, C_SCALL);
3058           S_SET_DATA_TYPE (symbolP, S_GET_DATA_TYPE (symbolP) | (DT_FCN << N_BTSHFT));
3059           continue;
3060         }                       /* rewrite sysproc */
3061 #endif /* OBJ_COFF */
3062
3063       if (!TC_S_IS_BALNAME (symbolP) && !TC_S_IS_CALLNAME (symbolP))
3064         {
3065           continue;
3066         }                       /* Not a leafproc symbol */
3067
3068       if (!S_IS_DEFINED (symbolP))
3069         {
3070           as_bad ("leafproc symbol '%s' undefined", S_GET_NAME (symbolP));
3071         }                       /* undefined leaf */
3072
3073       if (TC_S_IS_CALLNAME (symbolP))
3074         {
3075           symbolS *balP = tc_get_bal_of_call (symbolP);
3076           if (S_IS_EXTERNAL (symbolP) != S_IS_EXTERNAL (balP))
3077             {
3078               S_SET_EXTERNAL (symbolP);
3079               S_SET_EXTERNAL (balP);
3080               as_warn ("Warning: making leafproc entries %s and %s both global\n",
3081                        S_GET_NAME (symbolP), S_GET_NAME (balP));
3082             }                   /* externality mismatch */
3083         }                       /* if callname */
3084     }                           /* walk the symbol chain */
3085
3086   return;
3087 }                               /* tc_crawl_symbol_chain() */
3088
3089 /*
3090  * For aout or bout, the bal immediately follows the call.
3091  *
3092  * For coff, we cheat and store a pointer to the bal symbol
3093  * in the second aux entry of the call.
3094  */
3095
3096 #undef OBJ_ABOUT
3097 #ifdef OBJ_AOUT
3098 #define OBJ_ABOUT
3099 #endif
3100 #ifdef OBJ_BOUT
3101 #define OBJ_ABOUT
3102 #endif
3103
3104 void
3105 tc_set_bal_of_call (callP, balP)
3106      symbolS *callP;
3107      symbolS *balP;
3108 {
3109   know (TC_S_IS_CALLNAME (callP));
3110   know (TC_S_IS_BALNAME (balP));
3111
3112 #ifdef OBJ_COFF
3113
3114   callP->sy_symbol.ost_auxent[1].x_bal.x_balntry = (int) balP;
3115   S_SET_NUMBER_AUXILIARY (callP, 2);
3116
3117 #else /* ! OBJ_COFF */
3118 #ifdef OBJ_ABOUT
3119
3120   /* If the 'bal' entry doesn't immediately follow the 'call'
3121          * symbol, unlink it from the symbol list and re-insert it.
3122          */
3123   if (symbol_next (callP) != balP)
3124     {
3125       symbol_remove (balP, &symbol_rootP, &symbol_lastP);
3126       symbol_append (balP, callP, &symbol_rootP, &symbol_lastP);
3127     }                           /* if not in order */
3128
3129 #else /* ! OBJ_ABOUT */
3130   (as yet unwritten.);
3131 #endif /* ! OBJ_ABOUT */
3132 #endif /* ! OBJ_COFF */
3133
3134   return;
3135 }                               /* tc_set_bal_of_call() */
3136
3137 char *
3138 _tc_get_bal_of_call (callP)
3139      symbolS *callP;
3140 {
3141   symbolS *retval;
3142
3143   know (TC_S_IS_CALLNAME (callP));
3144
3145 #ifdef OBJ_COFF
3146   retval = (symbolS *) (callP->sy_symbol.ost_auxent[1].x_bal.x_balntry);
3147 #else
3148 #ifdef OBJ_ABOUT
3149   retval = symbol_next (callP);
3150 #else
3151   (as yet unwritten.);
3152 #endif /* ! OBJ_ABOUT */
3153 #endif /* ! OBJ_COFF */
3154
3155   know (TC_S_IS_BALNAME (retval));
3156   return ((char *) retval);
3157 }                               /* _tc_get_bal_of_call() */
3158
3159 void
3160 tc_coff_symbol_emit_hook (symbolP)
3161      symbolS *symbolP;
3162 {
3163   if (TC_S_IS_CALLNAME (symbolP))
3164     {
3165 #ifdef OBJ_COFF
3166       symbolS *balP = tc_get_bal_of_call (symbolP);
3167
3168       /* second aux entry contains the bal entry point */
3169       /*                S_SET_NUMBER_AUXILIARY(symbolP, 2); */
3170       symbolP->sy_symbol.ost_auxent[1].x_bal.x_balntry = S_GET_VALUE (balP);
3171       S_SET_STORAGE_CLASS (symbolP, (!SF_GET_LOCAL (symbolP) ? C_LEAFEXT : C_LEAFSTAT));
3172       S_SET_DATA_TYPE (symbolP, S_GET_DATA_TYPE (symbolP) | (DT_FCN << N_BTSHFT));
3173       /* fix up the bal symbol */
3174       S_SET_STORAGE_CLASS (balP, C_LABEL);
3175 #endif /* OBJ_COFF */
3176     }                           /* only on calls */
3177
3178   return;
3179 }                               /* tc_coff_symbol_emit_hook() */
3180
3181 void
3182 i960_handle_align (fragp)
3183      fragS *fragp;
3184 {
3185   fixS *fixp;
3186   segT old_seg = now_seg, this_seg;
3187   int old_subseg = now_subseg;
3188   int pad_size;
3189   extern struct frag *text_last_frag, *data_last_frag;
3190
3191   if (!linkrelax)
3192     return;
3193
3194   /* The text section "ends" with another alignment reloc, to which we
3195      aren't adding padding.  */
3196   if (fragp->fr_next == text_last_frag
3197       || fragp->fr_next == data_last_frag)
3198     {
3199       return;
3200     }
3201
3202   /* alignment directive */
3203   fixp = fix_new (fragp, fragp->fr_fix, fragp->fr_offset, 0, 0, 0, 0,
3204                   (int) fragp->fr_type);
3205 }
3206
3207 /*
3208  * Local Variables:
3209  * comment-column: 0
3210  * fill-column: 131
3211  * End:
3212  */
3213
3214 /* end of tc-i960.c */