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