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 | ||
1222 | subseg_new (SEG_DATA, 0); /* .data */ | |
1223 | frag_align (2, 0); /* .align 2 */ | |
1224 | record_alignment (now_seg, 2); | |
1225 | colon (BR_TAB_NAME); /* BR_TAB_NAME: */ | |
1226 | emit (0); /* .word 0 #link to next table */ | |
1227 | emit (br_cnt); /* .word n #length of table */ | |
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; |
5ac34ac3 | 1842 | exp->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 | ||
1849 | seg = expression (expP); | |
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 | } | |
1865 | return seg; | |
fecd2382 RP |
1866 | } |
1867 | ||
1868 | ||
1869 | /***************************************************************************** | |
1870 | * parse_ldcont: | |
1871 | * Parse and replace a 'ldconst' pseudo-instruction with an appropriate | |
1872 | * i80960 instruction. | |
1873 | * | |
1874 | * Assumes the input consists of: | |
1875 | * arg[0] opcode mnemonic ('ldconst') | |
1876 | * arg[1] first operand (constant) | |
1877 | * arg[2] name of register to be loaded | |
1878 | * | |
1879 | * Replaces opcode and/or operands as appropriate. | |
1880 | * | |
1881 | * Returns the new number of arguments, or -1 on failure. | |
1882 | * | |
1883 | **************************************************************************** */ | |
1884 | static | |
355afbcd KR |
1885 | int |
1886 | parse_ldconst (arg) | |
1887 | char *arg[]; /* See above */ | |
fecd2382 | 1888 | { |
355afbcd KR |
1889 | int n; /* Constant to be loaded */ |
1890 | int shift; /* Shift count for "shlo" instruction */ | |
1891 | static char buf[5]; /* Literal for first operand */ | |
1892 | static char buf2[5]; /* Literal for second operand */ | |
1893 | expressionS e; /* Parsed expression */ | |
1894 | ||
1895 | ||
1896 | arg[3] = NULL; /* So we can tell at the end if it got used or not */ | |
1897 | ||
5ac34ac3 ILT |
1898 | parse_expr (arg[1], &e); |
1899 | switch (e.X_op) | |
355afbcd | 1900 | { |
5ac34ac3 | 1901 | default: |
355afbcd KR |
1902 | /* We're dependent on one or more symbols -- use "lda" */ |
1903 | arg[0] = "lda"; | |
1904 | break; | |
1905 | ||
5ac34ac3 | 1906 | case O_constant: |
355afbcd | 1907 | /* Try the following mappings: |
5ac34ac3 ILT |
1908 | * ldconst 0,<reg> ->mov 0,<reg> |
1909 | * ldconst 31,<reg> ->mov 31,<reg> | |
1910 | * ldconst 32,<reg> ->addo 1,31,<reg> | |
1911 | * ldconst 62,<reg> ->addo 31,31,<reg> | |
1912 | * ldconst 64,<reg> ->shlo 8,3,<reg> | |
1913 | * ldconst -1,<reg> ->subo 1,0,<reg> | |
1914 | * ldconst -31,<reg>->subo 31,0,<reg> | |
1915 | * | |
1916 | * anthing else becomes: | |
1917 | * lda xxx,<reg> | |
1918 | */ | |
355afbcd KR |
1919 | n = offs (e); |
1920 | if ((0 <= n) && (n <= 31)) | |
1921 | { | |
1922 | arg[0] = "mov"; | |
1923 | ||
1924 | } | |
1925 | else if ((-31 <= n) && (n <= -1)) | |
1926 | { | |
1927 | arg[0] = "subo"; | |
1928 | arg[3] = arg[2]; | |
1929 | sprintf (buf, "%d", -n); | |
1930 | arg[1] = buf; | |
1931 | arg[2] = "0"; | |
1932 | ||
fecd2382 | 1933 | } |
355afbcd KR |
1934 | else if ((32 <= n) && (n <= 62)) |
1935 | { | |
1936 | arg[0] = "addo"; | |
1937 | arg[3] = arg[2]; | |
1938 | arg[1] = "31"; | |
1939 | sprintf (buf, "%d", n - 31); | |
1940 | arg[2] = buf; | |
1941 | ||
1942 | } | |
1943 | else if ((shift = shift_ok (n)) != 0) | |
1944 | { | |
1945 | arg[0] = "shlo"; | |
1946 | arg[3] = arg[2]; | |
1947 | sprintf (buf, "%d", shift); | |
1948 | arg[1] = buf; | |
1949 | sprintf (buf2, "%d", n >> shift); | |
1950 | arg[2] = buf2; | |
1951 | ||
1952 | } | |
1953 | else | |
1954 | { | |
1955 | arg[0] = "lda"; | |
1956 | } | |
1957 | break; | |
1958 | ||
5ac34ac3 | 1959 | case O_illegal: |
355afbcd KR |
1960 | as_bad ("invalid constant"); |
1961 | return -1; | |
1962 | break; | |
1963 | } | |
1964 | return (arg[3] == 0) ? 2 : 3; | |
fecd2382 RP |
1965 | } |
1966 | ||
1967 | /***************************************************************************** | |
1968 | * parse_memop: parse a memory operand | |
1969 | * | |
1970 | * This routine is based on the observation that the 4 mode bits of the | |
1971 | * MEMB format, taken individually, have fairly consistent meaning: | |
1972 | * | |
1973 | * M3 (bit 13): 1 if displacement is present (D_BIT) | |
1974 | * M2 (bit 12): 1 for MEMB instructions (MEMB_BIT) | |
1975 | * M1 (bit 11): 1 if index is present (I_BIT) | |
1976 | * M0 (bit 10): 1 if abase is present (A_BIT) | |
1977 | * | |
1978 | * So we parse the memory operand and set bits in the mode as we find | |
1979 | * things. Then at the end, if we go to MEMB format, we need only set | |
1980 | * the MEMB bit (M2) and our mode is built for us. | |
1981 | * | |
1982 | * Unfortunately, I said "fairly consistent". The exceptions: | |
1983 | * | |
1984 | * DBIA | |
1985 | * 0100 Would seem illegal, but means "abase-only". | |
1986 | * | |
1987 | * 0101 Would seem to mean "abase-only" -- it means IP-relative. | |
1988 | * Must be converted to 0100. | |
1989 | * | |
1990 | * 0110 Would seem to mean "index-only", but is reserved. | |
1991 | * We turn on the D bit and provide a 0 displacement. | |
1992 | * | |
1993 | * The other thing to observe is that we parse from the right, peeling | |
1994 | * things * off as we go: first any index spec, then any abase, then | |
1995 | * the displacement. | |
1996 | * | |
1997 | **************************************************************************** */ | |
1998 | static | |
355afbcd KR |
1999 | void |
2000 | parse_memop (memP, argP, optype) | |
2001 | memS *memP; /* Where to put the results */ | |
2002 | char *argP; /* Text of the operand to be parsed */ | |
2003 | int optype; /* MEM1, MEM2, MEM4, MEM8, MEM12, or MEM16 */ | |
fecd2382 | 2004 | { |
355afbcd KR |
2005 | char *indexP; /* Pointer to index specification with "[]" removed */ |
2006 | char *p; /* Temp char pointer */ | |
2007 | char iprel_flag; /* True if this is an IP-relative operand */ | |
2008 | int regnum; /* Register number */ | |
2009 | int scale; /* Scale factor: 1,2,4,8, or 16. Later converted | |
fecd2382 RP |
2010 | * to internal format (0,1,2,3,4 respectively). |
2011 | */ | |
355afbcd KR |
2012 | int mode; /* MEMB mode bits */ |
2013 | int *intP; /* Pointer to register number */ | |
2014 | ||
2015 | /* The following table contains the default scale factors for each | |
c47d388b ME |
2016 | * type of memory instruction. It is accessed using (optype-MEM1) |
2017 | * as an index -- thus it assumes the 'optype' constants are assigned | |
2018 | * consecutive values, in the order they appear in this table | |
2019 | */ | |
355afbcd KR |
2020 | static int def_scale[] = |
2021 | { | |
2022 | 1, /* MEM1 */ | |
2023 | 2, /* MEM2 */ | |
2024 | 4, /* MEM4 */ | |
2025 | 8, /* MEM8 */ | |
2026 | -1, /* MEM12 -- no valid default */ | |
2027 | 16 /* MEM16 */ | |
2028 | }; | |
2029 | ||
2030 | ||
2031 | iprel_flag = mode = 0; | |
2032 | ||
2033 | /* Any index present? */ | |
2034 | indexP = get_ispec (argP); | |
2035 | if (indexP) | |
2036 | { | |
2037 | p = strchr (indexP, '*'); | |
2038 | if (p == NULL) | |
2039 | { | |
2040 | /* No explicit scale -- use default for this | |
fecd2382 RP |
2041 | *instruction type. |
2042 | */ | |
355afbcd KR |
2043 | scale = def_scale[optype - MEM1]; |
2044 | } | |
2045 | else | |
2046 | { | |
2047 | *p++ = '\0'; /* Eliminate '*' */ | |
2048 | ||
2049 | /* Now indexP->a '\0'-terminated register name, | |
fecd2382 RP |
2050 | * and p->a scale factor. |
2051 | */ | |
355afbcd KR |
2052 | |
2053 | if (!strcmp (p, "16")) | |
2054 | { | |
2055 | scale = 16; | |
2056 | } | |
2057 | else if (strchr ("1248", *p) && (p[1] == '\0')) | |
2058 | { | |
2059 | scale = *p - '0'; | |
2060 | } | |
2061 | else | |
2062 | { | |
2063 | scale = -1; | |
2064 | } | |
2065 | } | |
2066 | ||
2067 | regnum = get_regnum (indexP); /* Get index reg. # */ | |
2068 | if (!IS_RG_REG (regnum)) | |
2069 | { | |
2070 | as_bad ("invalid index register"); | |
2071 | return; | |
fecd2382 | 2072 | } |
355afbcd KR |
2073 | |
2074 | /* Convert scale to its binary encoding */ | |
2075 | switch (scale) | |
2076 | { | |
2077 | case 1: | |
2078 | scale = 0 << 7; | |
2079 | break; | |
2080 | case 2: | |
2081 | scale = 1 << 7; | |
2082 | break; | |
2083 | case 4: | |
2084 | scale = 2 << 7; | |
2085 | break; | |
2086 | case 8: | |
2087 | scale = 3 << 7; | |
2088 | break; | |
2089 | case 16: | |
2090 | scale = 4 << 7; | |
2091 | break; | |
2092 | default: | |
2093 | as_bad ("invalid scale factor"); | |
2094 | return; | |
2095 | }; | |
2096 | ||
2097 | memP->opcode |= scale | regnum; /* Set index bits in opcode */ | |
2098 | mode |= I_BIT; /* Found a valid index spec */ | |
2099 | } | |
2100 | ||
2101 | /* Any abase (Register Indirect) specification present? */ | |
2102 | if ((p = strrchr (argP, '(')) != NULL) | |
2103 | { | |
2104 | /* "(" is there -- does it start a legal abase spec? | |
fecd2382 RP |
2105 | * (If not it could be part of a displacement expression.) |
2106 | */ | |
355afbcd KR |
2107 | intP = (int *) hash_find (areg_hash, p); |
2108 | if (intP != NULL) | |
2109 | { | |
2110 | /* Got an abase here */ | |
2111 | regnum = *intP; | |
2112 | *p = '\0'; /* discard register spec */ | |
2113 | if (regnum == IPREL) | |
2114 | { | |
2115 | /* We have to specialcase ip-rel mode */ | |
2116 | iprel_flag = 1; | |
2117 | } | |
2118 | else | |
2119 | { | |
2120 | memP->opcode |= regnum << 14; | |
2121 | mode |= A_BIT; | |
2122 | } | |
fecd2382 | 2123 | } |
355afbcd KR |
2124 | } |
2125 | ||
2126 | /* Any expression present? */ | |
2127 | memP->e = argP; | |
2128 | if (*argP != '\0') | |
2129 | { | |
2130 | mode |= D_BIT; | |
2131 | } | |
2132 | ||
2133 | /* Special-case ip-relative addressing */ | |
2134 | if (iprel_flag) | |
2135 | { | |
2136 | if (mode & I_BIT) | |
2137 | { | |
2138 | syntax (); | |
fecd2382 | 2139 | } |
355afbcd KR |
2140 | else |
2141 | { | |
2142 | memP->opcode |= 5 << 10; /* IP-relative mode */ | |
2143 | memP->disp = 32; | |
fecd2382 | 2144 | } |
355afbcd KR |
2145 | return; |
2146 | } | |
2147 | ||
2148 | /* Handle all other modes */ | |
2149 | switch (mode) | |
2150 | { | |
2151 | case D_BIT | A_BIT: | |
2152 | /* Go with MEMA instruction format for now (grow to MEMB later | |
fecd2382 RP |
2153 | * if 12 bits is not enough for the displacement). |
2154 | * MEMA format has a single mode bit: set it to indicate | |
2155 | * that abase is present. | |
2156 | */ | |
355afbcd KR |
2157 | memP->opcode |= MEMA_ABASE; |
2158 | memP->disp = 12; | |
2159 | break; | |
2160 | ||
2161 | case D_BIT: | |
2162 | /* Go with MEMA instruction format for now (grow to MEMB later | |
fecd2382 RP |
2163 | * if 12 bits is not enough for the displacement). |
2164 | */ | |
355afbcd KR |
2165 | memP->disp = 12; |
2166 | break; | |
2167 | ||
2168 | case A_BIT: | |
2169 | /* For some reason, the bit string for this mode is not | |
fecd2382 RP |
2170 | * consistent: it should be 0 (exclusive of the MEMB bit), |
2171 | * so we set it "by hand" here. | |
2172 | */ | |
355afbcd KR |
2173 | memP->opcode |= MEMB_BIT; |
2174 | break; | |
2175 | ||
2176 | case A_BIT | I_BIT: | |
2177 | /* set MEMB bit in mode, and OR in mode bits */ | |
2178 | memP->opcode |= mode | MEMB_BIT; | |
2179 | break; | |
2180 | ||
2181 | case I_BIT: | |
2182 | /* Treat missing displacement as displacement of 0 */ | |
2183 | mode |= D_BIT; | |
2184 | /*********************** | |
fecd2382 RP |
2185 | * Fall into next case * |
2186 | ********************** */ | |
355afbcd KR |
2187 | case D_BIT | A_BIT | I_BIT: |
2188 | case D_BIT | I_BIT: | |
2189 | /* set MEMB bit in mode, and OR in mode bits */ | |
2190 | memP->opcode |= mode | MEMB_BIT; | |
2191 | memP->disp = 32; | |
2192 | break; | |
2193 | ||
2194 | default: | |
2195 | syntax (); | |
2196 | break; | |
2197 | } | |
fecd2382 RP |
2198 | } |
2199 | ||
2200 | /***************************************************************************** | |
2201 | * parse_po: parse machine-dependent pseudo-op | |
2202 | * | |
2203 | * This is a top-level routine for machine-dependent pseudo-ops. It slurps | |
2204 | * up the rest of the input line, breaks out the individual arguments, | |
2205 | * and dispatches them to the correct handler. | |
2206 | **************************************************************************** */ | |
2207 | static | |
355afbcd KR |
2208 | void |
2209 | parse_po (po_num) | |
2210 | int po_num; /* Pseudo-op number: currently S_LEAFPROC or S_SYSPROC */ | |
fecd2382 | 2211 | { |
355afbcd | 2212 | char *args[4]; /* Pointers operands, with no embedded whitespace. |
fecd2382 RP |
2213 | * arg[0] unused. |
2214 | * arg[1-3]->operands | |
2215 | */ | |
355afbcd KR |
2216 | int n_ops; /* Number of operands */ |
2217 | char *p; /* Pointer to beginning of unparsed argument string */ | |
2218 | char eol; /* Character that indicated end of line */ | |
2219 | ||
2220 | extern char is_end_of_line[]; | |
2221 | ||
2222 | /* Advance input pointer to end of line. */ | |
2223 | p = input_line_pointer; | |
2224 | while (!is_end_of_line[*input_line_pointer]) | |
2225 | { | |
2226 | input_line_pointer++; | |
2227 | } | |
2228 | eol = *input_line_pointer; /* Save end-of-line char */ | |
2229 | *input_line_pointer = '\0'; /* Terminate argument list */ | |
2230 | ||
2231 | /* Parse out operands */ | |
2232 | n_ops = get_args (p, args); | |
2233 | if (n_ops == -1) | |
2234 | { | |
2235 | return; | |
2236 | } | |
2237 | ||
2238 | /* Dispatch to correct handler */ | |
2239 | switch (po_num) | |
2240 | { | |
2241 | case S_SYSPROC: | |
2242 | s_sysproc (n_ops, args); | |
2243 | break; | |
2244 | case S_LEAFPROC: | |
2245 | s_leafproc (n_ops, args); | |
2246 | break; | |
2247 | default: | |
2248 | BAD_CASE (po_num); | |
2249 | break; | |
2250 | } | |
2251 | ||
2252 | /* Restore eol, so line numbers get updated correctly. Base assembler | |
fecd2382 RP |
2253 | * assumes we leave input pointer pointing at char following the eol. |
2254 | */ | |
355afbcd | 2255 | *input_line_pointer++ = eol; |
fecd2382 RP |
2256 | } |
2257 | ||
2258 | /***************************************************************************** | |
2259 | * parse_regop: parse a register operand. | |
2260 | * | |
2261 | * In case of illegal operand, issue a message and return some valid | |
2262 | * information so instruction processing can continue. | |
2263 | **************************************************************************** */ | |
2264 | static | |
355afbcd KR |
2265 | void |
2266 | parse_regop (regopP, optext, opdesc) | |
2267 | struct regop *regopP; /* Where to put description of register operand */ | |
2268 | char *optext; /* Text of operand */ | |
2269 | char opdesc; /* Descriptor byte: what's legal for this operand */ | |
fecd2382 | 2270 | { |
355afbcd KR |
2271 | int n; /* Register number */ |
2272 | expressionS e; /* Parsed expression */ | |
2273 | ||
2274 | /* See if operand is a register */ | |
2275 | n = get_regnum (optext); | |
2276 | if (n >= 0) | |
2277 | { | |
2278 | if (IS_RG_REG (n)) | |
2279 | { | |
2280 | /* global or local register */ | |
2281 | if (!REG_ALIGN (opdesc, n)) | |
2282 | { | |
2283 | as_bad ("unaligned register"); | |
2284 | } | |
2285 | regopP->n = n; | |
2286 | regopP->mode = 0; | |
2287 | regopP->special = 0; | |
2288 | return; | |
2289 | } | |
2290 | else if (IS_FP_REG (n) && FP_OK (opdesc)) | |
2291 | { | |
2292 | /* Floating point register, and it's allowed */ | |
2293 | regopP->n = n - FP0; | |
2294 | regopP->mode = 1; | |
2295 | regopP->special = 0; | |
2296 | return; | |
2297 | } | |
2298 | else if (IS_SF_REG (n) && SFR_OK (opdesc)) | |
2299 | { | |
2300 | /* Special-function register, and it's allowed */ | |
2301 | regopP->n = n - SF0; | |
2302 | regopP->mode = 0; | |
2303 | regopP->special = 1; | |
2304 | if (!targ_has_sfr (regopP->n)) | |
2305 | { | |
2306 | as_bad ("no such sfr in this architecture"); | |
2307 | } | |
2308 | return; | |
2309 | } | |
2310 | } | |
2311 | else if (LIT_OK (opdesc)) | |
2312 | { | |
2313 | /* | |
fecd2382 RP |
2314 | * How about a literal? |
2315 | */ | |
355afbcd KR |
2316 | regopP->mode = 1; |
2317 | regopP->special = 0; | |
2318 | if (FP_OK (opdesc)) | |
2319 | { /* floating point literal acceptable */ | |
2320 | /* Skip over 0f, 0d, or 0e prefix */ | |
2321 | if ((optext[0] == '0') | |
2322 | && (optext[1] >= 'd') | |
2323 | && (optext[1] <= 'f')) | |
2324 | { | |
2325 | optext += 2; | |
2326 | } | |
2327 | ||
2328 | if (!strcmp (optext, "0.0") || !strcmp (optext, "0")) | |
2329 | { | |
2330 | regopP->n = 0x10; | |
2331 | return; | |
2332 | } | |
2333 | if (!strcmp (optext, "1.0") || !strcmp (optext, "1")) | |
2334 | { | |
2335 | regopP->n = 0x16; | |
2336 | return; | |
2337 | } | |
2338 | ||
fecd2382 | 2339 | } |
355afbcd KR |
2340 | else |
2341 | { /* fixed point literal acceptable */ | |
5ac34ac3 ILT |
2342 | parse_expr (optext, &e); |
2343 | if (e.X_op != O_constant | |
355afbcd KR |
2344 | || (offs (e) < 0) || (offs (e) > 31)) |
2345 | { | |
2346 | as_bad ("illegal literal"); | |
2347 | offs (e) = 0; | |
2348 | } | |
2349 | regopP->n = offs (e); | |
2350 | return; | |
2351 | } | |
2352 | } | |
2353 | ||
2354 | /* Nothing worked */ | |
2355 | syntax (); | |
2356 | regopP->mode = 0; /* Register r0 is always a good one */ | |
2357 | regopP->n = 0; | |
2358 | regopP->special = 0; | |
2359 | } /* parse_regop() */ | |
fecd2382 RP |
2360 | |
2361 | /***************************************************************************** | |
2362 | * reg_fmt: generate a REG-format instruction | |
2363 | * | |
2364 | **************************************************************************** */ | |
355afbcd KR |
2365 | static void |
2366 | reg_fmt (args, oP) | |
2367 | char *args[]; /* args[0]->opcode mnemonic, args[1-3]->operands */ | |
2368 | struct i960_opcode *oP; /* Pointer to description of instruction */ | |
fecd2382 | 2369 | { |
355afbcd KR |
2370 | long instr; /* Binary to be output */ |
2371 | struct regop regop; /* Description of register operand */ | |
2372 | int n_ops; /* Number of operands */ | |
2373 | ||
2374 | ||
2375 | instr = oP->opcode; | |
2376 | n_ops = oP->num_ops; | |
2377 | ||
2378 | if (n_ops >= 1) | |
2379 | { | |
2380 | parse_regop (®op, args[1], oP->operand[0]); | |
2381 | ||
2382 | if ((n_ops == 1) && !(instr & M3)) | |
2383 | { | |
2384 | /* 1-operand instruction in which the dst field should | |
fecd2382 RP |
2385 | * be used (instead of src1). |
2386 | */ | |
355afbcd KR |
2387 | regop.n <<= 19; |
2388 | if (regop.special) | |
2389 | { | |
2390 | regop.mode = regop.special; | |
2391 | } | |
2392 | regop.mode <<= 13; | |
2393 | regop.special = 0; | |
2394 | } | |
2395 | else | |
2396 | { | |
2397 | /* regop.n goes in bit 0, needs no shifting */ | |
2398 | regop.mode <<= 11; | |
2399 | regop.special <<= 5; | |
fecd2382 | 2400 | } |
355afbcd KR |
2401 | instr |= regop.n | regop.mode | regop.special; |
2402 | } | |
2403 | ||
2404 | if (n_ops >= 2) | |
2405 | { | |
2406 | parse_regop (®op, args[2], oP->operand[1]); | |
2407 | ||
2408 | if ((n_ops == 2) && !(instr & M3)) | |
2409 | { | |
2410 | /* 2-operand instruction in which the dst field should | |
fecd2382 RP |
2411 | * be used instead of src2). |
2412 | */ | |
355afbcd KR |
2413 | regop.n <<= 19; |
2414 | if (regop.special) | |
2415 | { | |
2416 | regop.mode = regop.special; | |
2417 | } | |
2418 | regop.mode <<= 13; | |
2419 | regop.special = 0; | |
fecd2382 | 2420 | } |
355afbcd KR |
2421 | else |
2422 | { | |
2423 | regop.n <<= 14; | |
2424 | regop.mode <<= 12; | |
2425 | regop.special <<= 6; | |
fecd2382 | 2426 | } |
355afbcd KR |
2427 | instr |= regop.n | regop.mode | regop.special; |
2428 | } | |
2429 | if (n_ops == 3) | |
2430 | { | |
2431 | parse_regop (®op, args[3], oP->operand[2]); | |
2432 | if (regop.special) | |
2433 | { | |
2434 | regop.mode = regop.special; | |
2435 | } | |
2436 | instr |= (regop.n <<= 19) | (regop.mode <<= 13); | |
2437 | } | |
2438 | emit (instr); | |
fecd2382 RP |
2439 | } |
2440 | ||
2441 | ||
2442 | /***************************************************************************** | |
2443 | * relax_cobr: | |
2444 | * Replace cobr instruction in a code fragment with equivalent branch and | |
2445 | * compare instructions, so it can reach beyond a 13-bit displacement. | |
2446 | * Set up an address fix/relocation for the new branch instruction. | |
2447 | * | |
2448 | **************************************************************************** */ | |
2449 | ||
2450 | /* This "conditional jump" table maps cobr instructions into equivalent | |
2451 | * compare and branch opcodes. | |
2452 | */ | |
2453 | static | |
355afbcd KR |
2454 | struct |
2455 | { | |
2456 | long compare; | |
2457 | long branch; | |
2458 | } | |
2459 | ||
2460 | coj[] = | |
2461 | { /* COBR OPCODE: */ | |
2462 | CHKBIT, BNO, /* 0x30 - bbc */ | |
2463 | CMPO, BG, /* 0x31 - cmpobg */ | |
2464 | CMPO, BE, /* 0x32 - cmpobe */ | |
2465 | CMPO, BGE, /* 0x33 - cmpobge */ | |
2466 | CMPO, BL, /* 0x34 - cmpobl */ | |
2467 | CMPO, BNE, /* 0x35 - cmpobne */ | |
2468 | CMPO, BLE, /* 0x36 - cmpoble */ | |
2469 | CHKBIT, BO, /* 0x37 - bbs */ | |
2470 | CMPI, BNO, /* 0x38 - cmpibno */ | |
2471 | CMPI, BG, /* 0x39 - cmpibg */ | |
2472 | CMPI, BE, /* 0x3a - cmpibe */ | |
2473 | CMPI, BGE, /* 0x3b - cmpibge */ | |
2474 | CMPI, BL, /* 0x3c - cmpibl */ | |
2475 | CMPI, BNE, /* 0x3d - cmpibne */ | |
2476 | CMPI, BLE, /* 0x3e - cmpible */ | |
2477 | CMPI, BO, /* 0x3f - cmpibo */ | |
2478 | }; | |
fecd2382 RP |
2479 | |
2480 | static | |
355afbcd KR |
2481 | void |
2482 | relax_cobr (fragP) | |
2483 | register fragS *fragP; /* fragP->fr_opcode is assumed to point to | |
c47d388b ME |
2484 | * the cobr instruction, which comes at the |
2485 | * end of the code fragment. | |
2486 | */ | |
fecd2382 | 2487 | { |
355afbcd KR |
2488 | int opcode, src1, src2, m1, s2; |
2489 | /* Bit fields from cobr instruction */ | |
2490 | long bp_bits; /* Branch prediction bits from cobr instruction */ | |
2491 | long instr; /* A single i960 instruction */ | |
2492 | char *iP; /*->instruction to be replaced */ | |
2493 | fixS *fixP; /* Relocation that can be done at assembly time */ | |
2494 | ||
2495 | /* PICK UP & PARSE COBR INSTRUCTION */ | |
2496 | iP = fragP->fr_opcode; | |
2497 | instr = md_chars_to_number (iP, 4); | |
2498 | opcode = ((instr >> 24) & 0xff) - 0x30; /* "-0x30" for table index */ | |
2499 | src1 = (instr >> 19) & 0x1f; | |
2500 | m1 = (instr >> 13) & 1; | |
2501 | s2 = instr & 1; | |
2502 | src2 = (instr >> 14) & 0x1f; | |
2503 | bp_bits = instr & BP_MASK; | |
2504 | ||
2505 | /* GENERATE AND OUTPUT COMPARE INSTRUCTION */ | |
2506 | instr = coj[opcode].compare | |
2507 | | src1 | (m1 << 11) | (s2 << 6) | (src2 << 14); | |
2508 | md_number_to_chars (iP, instr, 4); | |
2509 | ||
2510 | /* OUTPUT BRANCH INSTRUCTION */ | |
2511 | md_number_to_chars (iP + 4, coj[opcode].branch | bp_bits, 4); | |
2512 | ||
2513 | /* SET UP ADDRESS FIXUP/RELOCATION */ | |
2514 | fixP = fix_new (fragP, | |
2515 | iP + 4 - fragP->fr_literal, | |
2516 | 4, | |
2517 | fragP->fr_symbol, | |
355afbcd KR |
2518 | fragP->fr_offset, |
2519 | 1, | |
2520 | NO_RELOC); | |
2521 | ||
2522 | fixP->fx_bit_fixP = (bit_fixS *) 24; /* Store size of bit field */ | |
2523 | ||
2524 | fragP->fr_fix += 4; | |
2525 | frag_wane (fragP); | |
fecd2382 RP |
2526 | } |
2527 | ||
2528 | ||
2529 | /***************************************************************************** | |
2530 | * reloc_callj: Relocate a 'callj' instruction | |
2531 | * | |
2532 | * This is a "non-(GNU)-standard" machine-dependent hook. The base | |
2533 | * assembler calls it when it decides it can relocate an address at | |
2534 | * assembly time instead of emitting a relocation directive. | |
2535 | * | |
2536 | * Check to see if the relocation involves a 'callj' instruction to a: | |
2537 | * sysproc: Replace the default 'call' instruction with a 'calls' | |
2538 | * leafproc: Replace the default 'call' instruction with a 'bal'. | |
2539 | * other proc: Do nothing. | |
2540 | * | |
2541 | * See b.out.h for details on the 'n_other' field in a symbol structure. | |
2542 | * | |
2543 | * IMPORTANT!: | |
2544 | * Assumes the caller has already figured out, in the case of a leafproc, | |
2545 | * to use the 'bal' entry point, and has substituted that symbol into the | |
2546 | * passed fixup structure. | |
2547 | * | |
2548 | **************************************************************************** */ | |
355afbcd KR |
2549 | void |
2550 | reloc_callj (fixP) | |
2551 | fixS *fixP; /* Relocation that can be done at assembly time */ | |
fecd2382 | 2552 | { |
355afbcd KR |
2553 | char *where; /*->the binary for the instruction being relocated */ |
2554 | ||
2555 | if (!fixP->fx_callj) | |
2556 | { | |
2557 | return; | |
2558 | } /* This wasn't a callj instruction in the first place */ | |
2559 | ||
2560 | where = fixP->fx_frag->fr_literal + fixP->fx_where; | |
2561 | ||
2562 | if (TC_S_IS_SYSPROC (fixP->fx_addsy)) | |
2563 | { | |
2564 | /* Symbol is a .sysproc: replace 'call' with 'calls'. | |
fecd2382 RP |
2565 | * System procedure number is (other-1). |
2566 | */ | |
355afbcd KR |
2567 | md_number_to_chars (where, CALLS | TC_S_GET_SYSPROC (fixP->fx_addsy), 4); |
2568 | ||
2569 | /* Nothing else needs to be done for this instruction. | |
fecd2382 RP |
2570 | * Make sure 'md_number_to_field()' will perform a no-op. |
2571 | */ | |
355afbcd KR |
2572 | fixP->fx_bit_fixP = (bit_fixS *) 1; |
2573 | ||
2574 | } | |
2575 | else if (TC_S_IS_CALLNAME (fixP->fx_addsy)) | |
2576 | { | |
2577 | /* Should not happen: see block comment above */ | |
2578 | as_fatal ("Trying to 'bal' to %s", S_GET_NAME (fixP->fx_addsy)); | |
2579 | ||
2580 | } | |
2581 | else if (TC_S_IS_BALNAME (fixP->fx_addsy)) | |
2582 | { | |
2583 | /* Replace 'call' with 'bal'; both instructions have | |
fecd2382 RP |
2584 | * the same format, so calling code should complete |
2585 | * relocation as if nothing happened here. | |
2586 | */ | |
355afbcd KR |
2587 | md_number_to_chars (where, BAL, 4); |
2588 | } | |
2589 | else if (TC_S_IS_BADPROC (fixP->fx_addsy)) | |
2590 | { | |
2591 | as_bad ("Looks like a proc, but can't tell what kind.\n"); | |
2592 | } /* switch on proc type */ | |
2593 | ||
2594 | /* else Symbol is neither a sysproc nor a leafproc */ | |
2595 | ||
2596 | return; | |
2597 | } /* reloc_callj() */ | |
fecd2382 RP |
2598 | |
2599 | ||
2600 | /***************************************************************************** | |
2601 | * s_leafproc: process .leafproc pseudo-op | |
2602 | * | |
2603 | * .leafproc takes two arguments, the second one is optional: | |
2604 | * arg[1]: name of 'call' entry point to leaf procedure | |
2605 | * arg[2]: name of 'bal' entry point to leaf procedure | |
2606 | * | |
2607 | * If the two arguments are identical, or if the second one is missing, | |
2608 | * the first argument is taken to be the 'bal' entry point. | |
2609 | * | |
2610 | * If there are 2 distinct arguments, we must make sure that the 'bal' | |
2611 | * entry point immediately follows the 'call' entry point in the linked | |
2612 | * list of symbols. | |
2613 | * | |
2614 | **************************************************************************** */ | |
355afbcd KR |
2615 | static void |
2616 | s_leafproc (n_ops, args) | |
2617 | int n_ops; /* Number of operands */ | |
2618 | char *args[]; /* args[1]->1st operand, args[2]->2nd operand */ | |
fecd2382 | 2619 | { |
355afbcd KR |
2620 | symbolS *callP; /* Pointer to leafproc 'call' entry point symbol */ |
2621 | symbolS *balP; /* Pointer to leafproc 'bal' entry point symbol */ | |
2622 | ||
2623 | if ((n_ops != 1) && (n_ops != 2)) | |
2624 | { | |
2625 | as_bad ("should have 1 or 2 operands"); | |
2626 | return; | |
2627 | } /* Check number of arguments */ | |
2628 | ||
2629 | /* Find or create symbol for 'call' entry point. */ | |
2630 | callP = symbol_find_or_make (args[1]); | |
2631 | ||
2632 | if (TC_S_IS_CALLNAME (callP)) | |
2633 | { | |
2634 | as_warn ("Redefining leafproc %s", S_GET_NAME (callP)); | |
2635 | } /* is leafproc */ | |
2636 | ||
2637 | /* If that was the only argument, use it as the 'bal' entry point. | |
fecd2382 RP |
2638 | * Otherwise, mark it as the 'call' entry point and find or create |
2639 | * another symbol for the 'bal' entry point. | |
2640 | */ | |
355afbcd KR |
2641 | if ((n_ops == 1) || !strcmp (args[1], args[2])) |
2642 | { | |
2643 | TC_S_FORCE_TO_BALNAME (callP); | |
2644 | ||
2645 | } | |
2646 | else | |
2647 | { | |
2648 | TC_S_FORCE_TO_CALLNAME (callP); | |
2649 | ||
2650 | balP = symbol_find_or_make (args[2]); | |
2651 | if (TC_S_IS_CALLNAME (balP)) | |
2652 | { | |
2653 | as_warn ("Redefining leafproc %s", S_GET_NAME (balP)); | |
2654 | } | |
2655 | TC_S_FORCE_TO_BALNAME (balP); | |
2656 | ||
2657 | tc_set_bal_of_call (callP, balP); | |
2658 | } /* if only one arg, or the args are the same */ | |
2659 | ||
2660 | return; | |
2661 | } /* s_leafproc() */ | |
fecd2382 RP |
2662 | |
2663 | ||
2664 | /* | |
2665 | * s_sysproc: process .sysproc pseudo-op | |
2666 | * | |
2667 | * .sysproc takes two arguments: | |
2668 | * arg[1]: name of entry point to system procedure | |
2669 | * arg[2]: 'entry_num' (index) of system procedure in the range | |
2670 | * [0,31] inclusive. | |
2671 | * | |
2672 | * For [ab].out, we store the 'entrynum' in the 'n_other' field of | |
2673 | * the symbol. Since that entry is normally 0, we bias 'entrynum' | |
2674 | * by adding 1 to it. It must be unbiased before it is used. | |
2675 | */ | |
355afbcd KR |
2676 | static void |
2677 | s_sysproc (n_ops, args) | |
2678 | int n_ops; /* Number of operands */ | |
2679 | char *args[]; /* args[1]->1st operand, args[2]->2nd operand */ | |
fecd2382 | 2680 | { |
355afbcd KR |
2681 | expressionS exp; |
2682 | symbolS *symP; | |
2683 | ||
2684 | if (n_ops != 2) | |
2685 | { | |
2686 | as_bad ("should have two operands"); | |
2687 | return; | |
2688 | } /* bad arg count */ | |
2689 | ||
2690 | /* Parse "entry_num" argument and check it for validity. */ | |
5ac34ac3 ILT |
2691 | parse_expr (args[2], &exp); |
2692 | if (exp.X_op != O_constant | |
355afbcd KR |
2693 | || (offs (exp) < 0) |
2694 | || (offs (exp) > 31)) | |
2695 | { | |
2696 | as_bad ("'entry_num' must be absolute number in [0,31]"); | |
2697 | return; | |
2698 | } | |
2699 | ||
2700 | /* Find/make symbol and stick entry number (biased by +1) into it */ | |
2701 | symP = symbol_find_or_make (args[1]); | |
2702 | ||
2703 | if (TC_S_IS_SYSPROC (symP)) | |
2704 | { | |
2705 | as_warn ("Redefining entrynum for sysproc %s", S_GET_NAME (symP)); | |
2706 | } /* redefining */ | |
2707 | ||
2708 | TC_S_SET_SYSPROC (symP, offs (exp)); /* encode entry number */ | |
2709 | TC_S_FORCE_TO_SYSPROC (symP); | |
2710 | ||
2711 | return; | |
2712 | } /* s_sysproc() */ | |
fecd2382 RP |
2713 | |
2714 | ||
2715 | /***************************************************************************** | |
2716 | * shift_ok: | |
2717 | * Determine if a "shlo" instruction can be used to implement a "ldconst". | |
2718 | * This means that some number X < 32 can be shifted left to produce the | |
2719 | * constant of interest. | |
2720 | * | |
2721 | * Return the shift count, or 0 if we can't do it. | |
2722 | * Caller calculates X by shifting original constant right 'shift' places. | |
2723 | * | |
2724 | **************************************************************************** */ | |
2725 | static | |
355afbcd KR |
2726 | int |
2727 | shift_ok (n) | |
2728 | int n; /* The constant of interest */ | |
fecd2382 | 2729 | { |
355afbcd KR |
2730 | int shift; /* The shift count */ |
2731 | ||
2732 | if (n <= 0) | |
2733 | { | |
2734 | /* Can't do it for negative numbers */ | |
2735 | return 0; | |
2736 | } | |
2737 | ||
2738 | /* Shift 'n' right until a 1 is about to be lost */ | |
2739 | for (shift = 0; (n & 1) == 0; shift++) | |
2740 | { | |
2741 | n >>= 1; | |
2742 | } | |
2743 | ||
2744 | if (n >= 32) | |
2745 | { | |
2746 | return 0; | |
2747 | } | |
2748 | return shift; | |
fecd2382 RP |
2749 | } |
2750 | ||
2751 | ||
2752 | /***************************************************************************** | |
2753 | * syntax: issue syntax error | |
2754 | * | |
2755 | **************************************************************************** */ | |
355afbcd KR |
2756 | static void |
2757 | syntax () | |
2758 | { | |
2759 | as_bad ("syntax error"); | |
2760 | } /* syntax() */ | |
fecd2382 RP |
2761 | |
2762 | ||
2763 | /***************************************************************************** | |
2764 | * targ_has_sfr: | |
2765 | * Return TRUE iff the target architecture supports the specified | |
2766 | * special-function register (sfr). | |
2767 | * | |
2768 | **************************************************************************** */ | |
2769 | static | |
355afbcd KR |
2770 | int |
2771 | targ_has_sfr (n) | |
2772 | int n; /* Number (0-31) of sfr */ | |
fecd2382 | 2773 | { |
355afbcd KR |
2774 | switch (architecture) |
2775 | { | |
2776 | case ARCH_KA: | |
2777 | case ARCH_KB: | |
2778 | case ARCH_MC: | |
2779 | return 0; | |
2780 | case ARCH_CA: | |
2781 | default: | |
2782 | return ((0 <= n) && (n <= 2)); | |
2783 | } | |
fecd2382 RP |
2784 | } |
2785 | ||
2786 | ||
2787 | /***************************************************************************** | |
2788 | * targ_has_iclass: | |
2789 | * Return TRUE iff the target architecture supports the indicated | |
2790 | * class of instructions. | |
2791 | * | |
2792 | **************************************************************************** */ | |
2793 | static | |
355afbcd KR |
2794 | int |
2795 | targ_has_iclass (ic) | |
2796 | int ic; /* Instruction class; one of: | |
a39116f1 RP |
2797 | * I_BASE, I_CX, I_DEC, I_KX, I_FP, I_MIL, I_CASIM |
2798 | */ | |
fecd2382 | 2799 | { |
355afbcd KR |
2800 | iclasses_seen |= ic; |
2801 | switch (architecture) | |
2802 | { | |
2803 | case ARCH_KA: | |
2804 | return ic & (I_BASE | I_KX); | |
2805 | case ARCH_KB: | |
2806 | return ic & (I_BASE | I_KX | I_FP | I_DEC); | |
2807 | case ARCH_MC: | |
2808 | return ic & (I_BASE | I_KX | I_FP | I_DEC | I_MIL); | |
2809 | case ARCH_CA: | |
2810 | return ic & (I_BASE | I_CX | I_CASIM); | |
2811 | default: | |
2812 | if ((iclasses_seen & (I_KX | I_FP | I_DEC | I_MIL)) | |
2813 | && (iclasses_seen & I_CX)) | |
2814 | { | |
2815 | as_warn ("architecture of opcode conflicts with that of earlier instruction(s)"); | |
2816 | iclasses_seen &= ~ic; | |
fecd2382 | 2817 | } |
355afbcd KR |
2818 | return 1; |
2819 | } | |
fecd2382 RP |
2820 | } |
2821 | ||
2822 | ||
2823 | /* Parse an operand that is machine-specific. | |
2824 | We just return without modifying the expression if we have nothing | |
2825 | to do. */ | |
2826 | ||
2827 | /* ARGSUSED */ | |
2828 | void | |
355afbcd KR |
2829 | md_operand (expressionP) |
2830 | expressionS *expressionP; | |
fecd2382 RP |
2831 | { |
2832 | } | |
2833 | ||
2834 | /* We have no need to default values of symbols. */ | |
2835 | ||
2836 | /* ARGSUSED */ | |
355afbcd KR |
2837 | symbolS * |
2838 | md_undefined_symbol (name) | |
2839 | char *name; | |
fecd2382 | 2840 | { |
355afbcd KR |
2841 | return 0; |
2842 | } /* md_undefined_symbol() */ | |
fecd2382 RP |
2843 | |
2844 | /* Exactly what point is a PC-relative offset relative TO? | |
2845 | On the i960, they're relative to the address of the instruction, | |
2846 | which we have set up as the address of the fixup too. */ | |
2847 | long | |
355afbcd KR |
2848 | md_pcrel_from (fixP) |
2849 | fixS *fixP; | |
fecd2382 | 2850 | { |
355afbcd | 2851 | return fixP->fx_where + fixP->fx_frag->fr_address; |
fecd2382 RP |
2852 | } |
2853 | ||
2854 | void | |
355afbcd KR |
2855 | md_apply_fix (fixP, val) |
2856 | fixS *fixP; | |
2857 | long val; | |
fecd2382 | 2858 | { |
355afbcd KR |
2859 | char *place = fixP->fx_where + fixP->fx_frag->fr_literal; |
2860 | ||
2861 | if (!fixP->fx_bit_fixP) | |
2862 | { | |
2863 | ||
2864 | switch (fixP->fx_im_disp) | |
2865 | { | |
2866 | case 0: | |
2867 | fixP->fx_addnumber = val; | |
2868 | md_number_to_imm (place, val, fixP->fx_size, fixP); | |
2869 | break; | |
2870 | case 1: | |
2871 | md_number_to_disp (place, | |
c47d388b ME |
2872 | (fixP->fx_pcrel |
2873 | ? val + fixP->fx_pcrel_adjust | |
2874 | : val), | |
355afbcd KR |
2875 | fixP->fx_size); |
2876 | break; | |
2877 | case 2: /* fix requested for .long .word etc */ | |
2878 | md_number_to_chars (place, val, fixP->fx_size); | |
2879 | break; | |
2880 | default: | |
c47d388b ME |
2881 | as_fatal ("Internal error in md_apply_fix() in file \"%s\"", |
2882 | __FILE__); | |
2883 | } | |
355afbcd KR |
2884 | } |
2885 | else | |
2886 | { | |
2887 | md_number_to_field (place, val, fixP->fx_bit_fixP); | |
2888 | } | |
2889 | ||
2890 | return; | |
2891 | } /* md_apply_fix() */ | |
fecd2382 RP |
2892 | |
2893 | #if defined(OBJ_AOUT) | defined(OBJ_BOUT) | |
355afbcd KR |
2894 | void |
2895 | tc_bout_fix_to_chars (where, fixP, segment_address_in_file) | |
2896 | char *where; | |
2897 | fixS *fixP; | |
2898 | relax_addressT segment_address_in_file; | |
fecd2382 | 2899 | { |
355afbcd KR |
2900 | static unsigned char nbytes_r_length[] = |
2901 | {42, 0, 1, 42, 2}; | |
2902 | struct relocation_info ri; | |
2903 | symbolS *symbolP; | |
2904 | ||
2905 | /* JF this is for paranoia */ | |
2906 | memset ((char *) &ri, '\0', sizeof (ri)); | |
2907 | symbolP = fixP->fx_addsy; | |
2908 | know (symbolP != 0 || fixP->fx_r_type != NO_RELOC); | |
2909 | ri.r_bsr = fixP->fx_bsr; /*SAC LD RELAX HACK */ | |
2910 | /* These two 'cuz of NS32K */ | |
2911 | ri.r_callj = fixP->fx_callj; | |
2912 | if (fixP->fx_bit_fixP) | |
2913 | { | |
2914 | ri.r_length = 2; | |
2915 | } | |
2916 | else | |
2917 | { | |
2918 | ri.r_length = nbytes_r_length[fixP->fx_size]; | |
2919 | } | |
2920 | ri.r_pcrel = fixP->fx_pcrel; | |
2921 | ri.r_address = fixP->fx_frag->fr_address + fixP->fx_where - segment_address_in_file; | |
2922 | ||
2923 | if (fixP->fx_r_type != NO_RELOC) | |
2924 | { | |
2925 | switch (fixP->fx_r_type) | |
2926 | { | |
2927 | case rs_align: | |
2928 | ri.r_index = -2; | |
2929 | ri.r_pcrel = 1; | |
2930 | ri.r_length = fixP->fx_size - 1; | |
2931 | break; | |
2932 | case rs_org: | |
2933 | ri.r_index = -2; | |
2934 | ri.r_pcrel = 0; | |
2935 | break; | |
2936 | case rs_fill: | |
2937 | ri.r_index = -1; | |
2938 | break; | |
2939 | default: | |
2940 | abort (); | |
fecd2382 | 2941 | } |
355afbcd KR |
2942 | ri.r_extern = 0; |
2943 | } | |
2944 | else if (linkrelax || !S_IS_DEFINED (symbolP)) | |
2945 | { | |
2946 | ri.r_extern = 1; | |
2947 | ri.r_index = symbolP->sy_number; | |
2948 | } | |
2949 | else | |
2950 | { | |
2951 | ri.r_extern = 0; | |
2952 | ri.r_index = S_GET_TYPE (symbolP); | |
2953 | } | |
2954 | ||
2955 | /* Output the relocation information in machine-dependent form. */ | |
2956 | md_ri_to_chars (where, &ri); | |
2957 | ||
2958 | return; | |
2959 | } /* tc_bout_fix_to_chars() */ | |
fecd2382 | 2960 | |
fecd2382 RP |
2961 | #endif /* OBJ_AOUT or OBJ_BOUT */ |
2962 | ||
2963 | /* Align an address by rounding it up to the specified boundary. | |
2964 | */ | |
c47d388b | 2965 | valueT |
355afbcd KR |
2966 | md_section_align (seg, addr) |
2967 | segT seg; | |
c47d388b | 2968 | valueT addr; /* Address to be rounded up */ |
fecd2382 | 2969 | { |
355afbcd KR |
2970 | return ((addr + (1 << section_alignment[(int) seg]) - 1) & (-1 << section_alignment[(int) seg])); |
2971 | } /* md_section_align() */ | |
fecd2382 RP |
2972 | |
2973 | #ifdef OBJ_COFF | |
355afbcd KR |
2974 | void |
2975 | tc_headers_hook (headers) | |
2976 | object_headers *headers; | |
fecd2382 | 2977 | { |
355afbcd KR |
2978 | /* FIXME: remove this line *//* unsigned short arch_flag = 0; */ |
2979 | ||
2980 | if (iclasses_seen == I_BASE) | |
2981 | { | |
2982 | headers->filehdr.f_flags |= F_I960CORE; | |
2983 | } | |
2984 | else if (iclasses_seen & I_CX) | |
2985 | { | |
2986 | headers->filehdr.f_flags |= F_I960CA; | |
2987 | } | |
2988 | else if (iclasses_seen & I_MIL) | |
2989 | { | |
2990 | headers->filehdr.f_flags |= F_I960MC; | |
2991 | } | |
2992 | else if (iclasses_seen & (I_DEC | I_FP)) | |
2993 | { | |
2994 | headers->filehdr.f_flags |= F_I960KB; | |
2995 | } | |
2996 | else | |
2997 | { | |
2998 | headers->filehdr.f_flags |= F_I960KA; | |
2999 | } /* set arch flag */ | |
3000 | ||
3001 | if (flagseen['R']) | |
3002 | { | |
3003 | headers->filehdr.f_magic = I960RWMAGIC; | |
3004 | headers->aouthdr.magic = OMAGIC; | |
3005 | } | |
3006 | else | |
3007 | { | |
3008 | headers->filehdr.f_magic = I960ROMAGIC; | |
3009 | headers->aouthdr.magic = NMAGIC; | |
3010 | } /* set magic numbers */ | |
3011 | ||
3012 | return; | |
3013 | } /* tc_headers_hook() */ | |
3014 | ||
fecd2382 RP |
3015 | #endif /* OBJ_COFF */ |
3016 | ||
3017 | /* | |
3018 | * Things going on here: | |
3019 | * | |
3020 | * For bout, We need to assure a couple of simplifying | |
3021 | * assumptions about leafprocs for the linker: the leafproc | |
3022 | * entry symbols will be defined in the same assembly in | |
3023 | * which they're declared with the '.leafproc' directive; | |
3024 | * and if a leafproc has both 'call' and 'bal' entry points | |
3025 | * they are both global or both local. | |
3026 | * | |
3027 | * For coff, the call symbol has a second aux entry that | |
3028 | * contains the bal entry point. The bal symbol becomes a | |
3029 | * label. | |
3030 | * | |
3031 | * For coff representation, the call symbol has a second aux entry that | |
3032 | * contains the bal entry point. The bal symbol becomes a label. | |
3033 | * | |
3034 | */ | |
3035 | ||
355afbcd KR |
3036 | void |
3037 | tc_crawl_symbol_chain (headers) | |
3038 | object_headers *headers; | |
fecd2382 | 3039 | { |
355afbcd KR |
3040 | symbolS *symbolP; |
3041 | ||
3042 | for (symbolP = symbol_rootP; symbolP; symbolP = symbol_next (symbolP)) | |
3043 | { | |
fecd2382 | 3044 | #ifdef OBJ_COFF |
355afbcd KR |
3045 | if (TC_S_IS_SYSPROC (symbolP)) |
3046 | { | |
3047 | /* second aux entry already contains the sysproc number */ | |
3048 | S_SET_NUMBER_AUXILIARY (symbolP, 2); | |
3049 | S_SET_STORAGE_CLASS (symbolP, C_SCALL); | |
3050 | S_SET_DATA_TYPE (symbolP, S_GET_DATA_TYPE (symbolP) | (DT_FCN << N_BTSHFT)); | |
3051 | continue; | |
3052 | } /* rewrite sysproc */ | |
fecd2382 | 3053 | #endif /* OBJ_COFF */ |
355afbcd KR |
3054 | |
3055 | if (!TC_S_IS_BALNAME (symbolP) && !TC_S_IS_CALLNAME (symbolP)) | |
3056 | { | |
3057 | continue; | |
3058 | } /* Not a leafproc symbol */ | |
3059 | ||
3060 | if (!S_IS_DEFINED (symbolP)) | |
3061 | { | |
3062 | as_bad ("leafproc symbol '%s' undefined", S_GET_NAME (symbolP)); | |
3063 | } /* undefined leaf */ | |
3064 | ||
3065 | if (TC_S_IS_CALLNAME (symbolP)) | |
3066 | { | |
3067 | symbolS *balP = tc_get_bal_of_call (symbolP); | |
3068 | if (S_IS_EXTERNAL (symbolP) != S_IS_EXTERNAL (balP)) | |
3069 | { | |
3070 | S_SET_EXTERNAL (symbolP); | |
3071 | S_SET_EXTERNAL (balP); | |
3072 | as_warn ("Warning: making leafproc entries %s and %s both global\n", | |
3073 | S_GET_NAME (symbolP), S_GET_NAME (balP)); | |
3074 | } /* externality mismatch */ | |
3075 | } /* if callname */ | |
3076 | } /* walk the symbol chain */ | |
3077 | ||
3078 | return; | |
3079 | } /* tc_crawl_symbol_chain() */ | |
fecd2382 RP |
3080 | |
3081 | /* | |
3082 | * For aout or bout, the bal immediately follows the call. | |
3083 | * | |
3084 | * For coff, we cheat and store a pointer to the bal symbol | |
3085 | * in the second aux entry of the call. | |
3086 | */ | |
3087 | ||
355afbcd KR |
3088 | #undef OBJ_ABOUT |
3089 | #ifdef OBJ_AOUT | |
3090 | #define OBJ_ABOUT | |
3091 | #endif | |
3092 | #ifdef OBJ_BOUT | |
3093 | #define OBJ_ABOUT | |
3094 | #endif | |
3095 | ||
3096 | void | |
3097 | tc_set_bal_of_call (callP, balP) | |
3098 | symbolS *callP; | |
3099 | symbolS *balP; | |
fecd2382 | 3100 | { |
355afbcd KR |
3101 | know (TC_S_IS_CALLNAME (callP)); |
3102 | know (TC_S_IS_BALNAME (balP)); | |
3103 | ||
fecd2382 | 3104 | #ifdef OBJ_COFF |
355afbcd KR |
3105 | |
3106 | callP->sy_symbol.ost_auxent[1].x_bal.x_balntry = (int) balP; | |
3107 | S_SET_NUMBER_AUXILIARY (callP, 2); | |
3108 | ||
3109 | #else /* ! OBJ_COFF */ | |
3110 | #ifdef OBJ_ABOUT | |
3111 | ||
3112 | /* If the 'bal' entry doesn't immediately follow the 'call' | |
fecd2382 RP |
3113 | * symbol, unlink it from the symbol list and re-insert it. |
3114 | */ | |
355afbcd KR |
3115 | if (symbol_next (callP) != balP) |
3116 | { | |
3117 | symbol_remove (balP, &symbol_rootP, &symbol_lastP); | |
3118 | symbol_append (balP, callP, &symbol_rootP, &symbol_lastP); | |
3119 | } /* if not in order */ | |
3120 | ||
3121 | #else /* ! OBJ_ABOUT */ | |
3122 | (as yet unwritten.); | |
3123 | #endif /* ! OBJ_ABOUT */ | |
3124 | #endif /* ! OBJ_COFF */ | |
3125 | ||
3126 | return; | |
3127 | } /* tc_set_bal_of_call() */ | |
3128 | ||
3129 | char * | |
3130 | _tc_get_bal_of_call (callP) | |
3131 | symbolS *callP; | |
fecd2382 | 3132 | { |
355afbcd KR |
3133 | symbolS *retval; |
3134 | ||
3135 | know (TC_S_IS_CALLNAME (callP)); | |
3136 | ||
fecd2382 | 3137 | #ifdef OBJ_COFF |
355afbcd | 3138 | retval = (symbolS *) (callP->sy_symbol.ost_auxent[1].x_bal.x_balntry); |
fecd2382 | 3139 | #else |
355afbcd KR |
3140 | #ifdef OBJ_ABOUT |
3141 | retval = symbol_next (callP); | |
3142 | #else | |
3143 | (as yet unwritten.); | |
3144 | #endif /* ! OBJ_ABOUT */ | |
3145 | #endif /* ! OBJ_COFF */ | |
3146 | ||
3147 | know (TC_S_IS_BALNAME (retval)); | |
3148 | return ((char *) retval); | |
3149 | } /* _tc_get_bal_of_call() */ | |
3150 | ||
3151 | void | |
3152 | tc_coff_symbol_emit_hook (symbolP) | |
3153 | symbolS *symbolP; | |
fecd2382 | 3154 | { |
355afbcd KR |
3155 | if (TC_S_IS_CALLNAME (symbolP)) |
3156 | { | |
fecd2382 | 3157 | #ifdef OBJ_COFF |
355afbcd KR |
3158 | symbolS *balP = tc_get_bal_of_call (symbolP); |
3159 | ||
3160 | /* second aux entry contains the bal entry point */ | |
3161 | /* S_SET_NUMBER_AUXILIARY(symbolP, 2); */ | |
3162 | symbolP->sy_symbol.ost_auxent[1].x_bal.x_balntry = S_GET_VALUE (balP); | |
3163 | S_SET_STORAGE_CLASS (symbolP, (!SF_GET_LOCAL (symbolP) ? C_LEAFEXT : C_LEAFSTAT)); | |
3164 | S_SET_DATA_TYPE (symbolP, S_GET_DATA_TYPE (symbolP) | (DT_FCN << N_BTSHFT)); | |
3165 | /* fix up the bal symbol */ | |
3166 | S_SET_STORAGE_CLASS (balP, C_LABEL); | |
fecd2382 | 3167 | #endif /* OBJ_COFF */ |
355afbcd KR |
3168 | } /* only on calls */ |
3169 | ||
3170 | return; | |
3171 | } /* tc_coff_symbol_emit_hook() */ | |
fecd2382 | 3172 | |
09952cd9 KR |
3173 | void |
3174 | i960_handle_align (fragp) | |
3175 | fragS *fragp; | |
3176 | { | |
3177 | fixS *fixp; | |
3178 | segT old_seg = now_seg, this_seg; | |
3179 | int old_subseg = now_subseg; | |
3180 | int pad_size; | |
3181 | extern struct frag *text_last_frag, *data_last_frag; | |
3182 | ||
3183 | if (!linkrelax) | |
3184 | return; | |
3185 | ||
3186 | /* The text section "ends" with another alignment reloc, to which we | |
3187 | aren't adding padding. */ | |
3188 | if (fragp->fr_next == text_last_frag | |
3189 | || fragp->fr_next == data_last_frag) | |
3190 | { | |
3191 | return; | |
3192 | } | |
3193 | ||
3194 | /* alignment directive */ | |
5ac34ac3 | 3195 | fixp = fix_new (fragp, fragp->fr_fix, fragp->fr_offset, 0, 0, 0, |
09952cd9 KR |
3196 | (int) fragp->fr_type); |
3197 | } | |
3198 | ||
8b228fe9 | 3199 | /* end of tc-i960.c */ |