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