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