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