Commit | Line | Data |
---|---|---|
252b5132 RH |
1 | /* tc-mcore.c -- Assemble code for M*Core |
2 | ||
3 | Copyright (C) 1993,1994, 1999 Free Software Foundation. | |
4 | ||
5 | This file is part of GAS, the GNU Assembler. | |
6 | ||
7 | GAS is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GAS is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GAS; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | #include <stdio.h> | |
22 | #include "as.h" | |
23 | #include "bfd.h" | |
24 | #include "subsegs.h" | |
25 | #define DEFINE_TABLE | |
26 | #include "../opcodes/mcore-opc.h" | |
27 | #include <ctype.h> | |
28 | #include <string.h> | |
29 | ||
30 | #ifdef OBJ_ELF | |
31 | #include "elf/mcore.h" | |
32 | #endif | |
33 | ||
34 | #ifndef streq | |
35 | #define streq(a,b) (strcmp (a, b) == 0) | |
36 | #endif | |
37 | ||
38 | /* Forward declarations for dumb compilers. */ | |
39 | static void mcore_s_literals PARAMS ((int)); | |
40 | static void mcore_cons PARAMS ((int)); | |
41 | static void mcore_float_cons PARAMS ((int)); | |
42 | static void mcore_stringer PARAMS ((int)); | |
43 | static int log2 PARAMS ((unsigned int)); | |
44 | static char * parse_reg PARAMS ((char *, unsigned *)); | |
45 | static char * parse_creg PARAMS ((char *, unsigned *)); | |
46 | static char * parse_exp PARAMS ((char *, expressionS *)); | |
47 | static void make_name PARAMS ((char *, char *, int)); | |
48 | static int enter_literal PARAMS ((expressionS *, int)); | |
49 | static char * parse_rt PARAMS ((char *, char **, int, expressionS *)); | |
50 | static char * parse_imm PARAMS ((char *, unsigned *, unsigned, unsigned)); | |
51 | static char * parse_mem PARAMS ((char *, unsigned *, unsigned *, unsigned)); | |
52 | static void dump_literals PARAMS ((int)); | |
53 | static void check_literals PARAMS ((int, int)); | |
54 | static void mcore_s_text PARAMS ((int)); | |
55 | static void mcore_s_data PARAMS ((int)); | |
56 | #ifdef OBJ_ELF | |
57 | static void mcore_s_section PARAMS ((int)); | |
58 | #endif | |
59 | ||
60 | /* Several places in this file insert raw instructions into the | |
61 | object. They should use MCORE_INST_XXX macros to get the opcodes | |
62 | and then use these two macros to crack the MCORE_INST value into | |
63 | the appropriate byte values. */ | |
64 | #define INST_BYTE0(x) (((x) >> 8) & 0xFF) | |
65 | #define INST_BYTE1(x) ((x) & 0xFF) | |
66 | ||
67 | const char comment_chars[] = "#/"; | |
68 | const char line_separator_chars[] = ";"; | |
69 | const char line_comment_chars[] = "#/"; | |
70 | ||
71 | const int md_reloc_size = 8; | |
72 | ||
73 | static int relax; /* set if -relax seen */ | |
74 | static int do_jsri2bsr = 0; /* change here from 1 by Cruess 19 August 97 */ | |
75 | static int sifilter_mode = 0; | |
76 | ||
77 | const char EXP_CHARS[] = "eE"; | |
78 | ||
79 | /* Chars that mean this number is a floating point constant */ | |
80 | /* As in 0f12.456 */ | |
81 | /* or 0d1.2345e12 */ | |
82 | const char FLT_CHARS[] = "rRsSfFdDxXpP"; | |
83 | ||
84 | #define C(what,length) (((what) << 2) + (length)) | |
85 | #define GET_WHAT(x) ((x >> 2)) | |
86 | ||
87 | /* These are the two types of relaxable instruction */ | |
88 | #define COND_JUMP 1 | |
89 | #define UNCD_JUMP 2 | |
90 | ||
91 | #define UNDEF_DISP 0 | |
92 | #define COND12 1 | |
93 | #define COND32 2 | |
94 | #define UNCD12 1 | |
95 | #define UNCD32 2 | |
96 | #define UNDEF_WORD_DISP 4 | |
97 | #define END 5 | |
98 | ||
99 | #define C12_LEN 2 | |
100 | #define C32_LEN 10 /* allow for align */ | |
101 | #define U12_LEN 2 | |
102 | #define U32_LEN 8 /* allow for align */ | |
103 | ||
104 | ||
105 | /* Initialize the relax table */ | |
106 | const relax_typeS md_relax_table[] = | |
107 | { | |
108 | { 1, 1, 0, 0 }, /* 0: unused */ | |
109 | { 1, 1, 0, 0 }, /* 1: unused */ | |
110 | { 1, 1, 0, 0 }, /* 2: unused */ | |
111 | { 1, 1, 0, 0 }, /* 3: unused */ | |
112 | { 1, 1, 0, 0 }, /* 4: unused */ | |
113 | { 2048, -2046, C12_LEN, C(COND_JUMP, COND32) }, /* 5: C(COND_JUMP, COND12) */ | |
114 | { 0, 0, C32_LEN, 0 }, /* 6: C(COND_JUMP, COND32) */ | |
115 | { 1, 1, 0, 0 }, /* 7: unused */ | |
116 | { 1, 1, 0, 0 }, /* 8: unused */ | |
117 | { 2048, -2046, U12_LEN, C(UNCD_JUMP, UNCD32) }, /* 9: C(UNCD_JUMP, UNCD12) */ | |
118 | { 0, 0, U32_LEN, 0 }, /*10: C(UNCD_JUMP, UNCD32) */ | |
119 | { 1, 1, 0, 0 }, /*11: unused */ | |
120 | { 0, 0, 0, 0 } /*12: unused */ | |
121 | }; | |
122 | ||
123 | /* LITERAL POOL DATA STRUCTURES */ | |
124 | struct literal | |
125 | { | |
126 | unsigned short refcnt; | |
127 | unsigned char ispcrel; | |
128 | unsigned char unused; | |
129 | expressionS e; | |
130 | }; | |
131 | ||
132 | #define MAX_POOL_SIZE (1024/4) | |
133 | static struct literal litpool [MAX_POOL_SIZE]; | |
134 | static unsigned poolsize; | |
135 | static unsigned poolnumber; | |
136 | static unsigned long poolspan; | |
137 | ||
138 | /* SPANPANIC: the point at which we get too scared and force a dump | |
139 | of the literal pool, and perhaps put a branch in place. | |
140 | Calculated as: | |
141 | 1024 span of lrw/jmpi/jsri insn (actually span+1) | |
142 | -2 possible alignment at the insn. | |
143 | -2 possible alignment to get the table aligned. | |
144 | -2 an inserted branch around the table. | |
145 | == 1018 | |
146 | at 1018, we might be in trouble. | |
147 | -- so we have to be smaller than 1018 and since we deal with 2-byte | |
148 | instructions, the next good choice is 1016. | |
149 | -- Note we have a test case that fails when we've got 1018 here. */ | |
150 | #define SPANPANIC (1016) /* 1024 - 1 entry - 2 byte rounding */ | |
151 | #define SPANCLOSE (900) | |
152 | #define SPANEXIT (600) | |
153 | static symbolS * poolsym; /* label for current pool */ | |
154 | static char poolname[8]; | |
155 | static struct hash_control * opcode_hash_control; /* Opcode mnemonics */ | |
156 | ||
157 | /* This table describes all the machine specific pseudo-ops the assembler | |
158 | has to support. The fields are: | |
159 | Pseudo-op name without dot | |
160 | Function to call to execute this pseudo-op | |
161 | Integer arg to pass to the function */ | |
162 | const pseudo_typeS md_pseudo_table[] = | |
163 | { | |
164 | { "export", s_globl, 0 }, | |
165 | { "import", s_ignore, 0 }, | |
166 | { "literals", mcore_s_literals, 0 }, | |
167 | { "page", listing_eject, 0 }, | |
168 | { "bss", s_lcomm_bytes, 1 }, | |
169 | ||
170 | /* The following are to intercept the placement of data into the text | |
171 | section (eg addresses for a switch table), so that the space they | |
172 | occupy can be taken into account when deciding whether or not to | |
173 | dump the current literal pool. | |
174 | XXX - currently we do not cope with the .space and .dcb.d directives. */ | |
175 | { "ascii", mcore_stringer, 0 }, | |
176 | { "asciz", mcore_stringer, 1 }, | |
177 | { "byte", mcore_cons, 1 }, | |
178 | { "dc", mcore_cons, 2 }, | |
179 | { "dc.b", mcore_cons, 1 }, | |
180 | { "dc.d", mcore_float_cons, 'd' }, | |
181 | { "dc.l", mcore_cons, 4 }, | |
182 | { "dc.s", mcore_float_cons, 'f' }, | |
183 | { "dc.w", mcore_cons, 2 }, | |
184 | { "dc.x", mcore_float_cons, 'x' }, | |
185 | { "double", mcore_float_cons, 'd'}, | |
186 | { "float", mcore_float_cons, 'f'}, | |
187 | { "hword", mcore_cons, 2 }, | |
188 | { "int", mcore_cons, 4 }, | |
189 | { "long", mcore_cons, 4 }, | |
190 | { "octa", mcore_cons, 16 }, | |
191 | { "quad", mcore_cons, 8 }, | |
192 | { "short", mcore_cons, 2 }, | |
193 | { "single", mcore_float_cons, 'f'}, | |
194 | { "string", mcore_stringer, 1 }, | |
195 | { "word", mcore_cons, 2 }, | |
196 | ||
197 | /* Allow for the effect of section changes. */ | |
198 | { "text", mcore_s_text, 0 }, | |
199 | { "data", mcore_s_data, 0 }, | |
200 | ||
201 | #ifdef OBJ_ELF | |
202 | { "section", mcore_s_section, 0 }, | |
203 | { "section.s", mcore_s_section, 0 }, | |
204 | { "sect", mcore_s_section, 0 }, | |
205 | { "sect.s", mcore_s_section, 0 }, | |
206 | #endif | |
207 | { 0, 0, 0 } | |
208 | }; | |
209 | ||
210 | static void | |
211 | mcore_s_literals (ignore) | |
212 | int ignore; | |
213 | { | |
214 | dump_literals (0); | |
215 | demand_empty_rest_of_line (); | |
216 | } | |
217 | ||
218 | ||
219 | static void | |
220 | mcore_cons (nbytes) | |
221 | int nbytes; | |
222 | { | |
223 | if (now_seg == text_section) | |
224 | { | |
225 | char * ptr = input_line_pointer; | |
226 | int commas = 1; | |
227 | ||
228 | /* Count the number of commas on the line. */ | |
229 | while (! is_end_of_line [* ptr]) | |
230 | commas += * ptr ++ == ','; | |
231 | ||
232 | poolspan += nbytes * commas; | |
233 | } | |
234 | ||
235 | cons (nbytes); | |
236 | ||
237 | /* In theory we ought to call check_literals (2,0) here in case | |
238 | we need to dump the literal table. We cannot do this however, | |
239 | as the directives that we are intercepting may be being used | |
240 | to build a switch table, and we must not interfere with its | |
241 | contents. Instead we cross our fingers and pray... */ | |
242 | } | |
243 | ||
244 | static void | |
245 | mcore_float_cons (float_type) | |
246 | int float_type; | |
247 | { | |
248 | if (now_seg == text_section) | |
249 | { | |
250 | char * ptr = input_line_pointer; | |
251 | int commas = 1; | |
252 | ||
253 | #ifdef REPEAT_CONS_EXPRESSIONS | |
254 | #error REPEAT_CONS_EXPRESSIONS not handled | |
255 | #endif | |
256 | ||
257 | /* Count the number of commas on the line. */ | |
258 | while (! is_end_of_line [* ptr]) | |
259 | commas += * ptr ++ == ','; | |
260 | ||
261 | /* We would like to compute "hex_float (float_type) * commas" | |
262 | but hex_float is not exported from read.c */ | |
263 | float_type == 'f' ? 4 : (float_type == 'd' ? 8 : 12); | |
264 | poolspan += float_type * commas; | |
265 | } | |
266 | ||
267 | float_cons (float_type); | |
268 | ||
269 | /* See the comment in mcore_cons () about calling check_literals. | |
270 | It is unlikely that a switch table will be constructed using | |
271 | floating point values, but it is still likely that an indexed | |
272 | table of floating point constants is being created by these | |
273 | directives, so again we must not interfere with their placement. */ | |
274 | } | |
275 | ||
276 | static void | |
277 | mcore_stringer (append_zero) | |
278 | int append_zero; | |
279 | { | |
280 | if (now_seg == text_section) | |
281 | { | |
282 | char * ptr = input_line_pointer; | |
283 | ||
284 | /* In theory we should compute how many bytes are going to | |
285 | be occupied by the string(s) and add this to the poolspan. | |
286 | To keep things simple however, we just add the number of | |
287 | bytes left on the current line. This will be an over- | |
288 | estimate, which is OK, and automatically allows for the | |
289 | appending a zero byte, since the real string(s) is/are | |
290 | required to be enclosed in double quotes. */ | |
291 | while (! is_end_of_line [* ptr]) | |
292 | ptr ++; | |
293 | ||
294 | poolspan += ptr - input_line_pointer; | |
295 | } | |
296 | ||
297 | stringer (append_zero); | |
298 | ||
299 | /* We call check_literals here in case a large number of strings are | |
300 | being placed into the text section with a sequence of stringer | |
301 | directives. In theory we could be upsetting something if these | |
302 | strings are actually in an indexed table instead of referenced by | |
303 | individual labels. Let us hope that that never happens. */ | |
304 | check_literals (2, 0); | |
305 | } | |
306 | ||
307 | static void | |
308 | mcore_s_text (ignore) | |
309 | int ignore; | |
310 | { | |
311 | dump_literals (0); | |
312 | ||
313 | s_text (ignore); | |
314 | } | |
315 | ||
316 | static void | |
317 | mcore_s_data (ignore) | |
318 | int ignore; | |
319 | { | |
320 | dump_literals (0); | |
321 | ||
322 | s_data (ignore); | |
323 | } | |
324 | ||
325 | /* This function is called once, at assembler startup time. This should | |
326 | set up all the tables, etc that the MD part of the assembler needs. */ | |
327 | void | |
328 | md_begin () | |
329 | { | |
330 | mcore_opcode_info * opcode; | |
331 | char * prev_name = ""; | |
332 | ||
333 | opcode_hash_control = hash_new (); | |
334 | ||
335 | /* Insert unique names into hash table */ | |
336 | for (opcode = mcore_table; opcode->name; opcode ++) | |
337 | { | |
338 | if (streq (prev_name, opcode->name)) | |
339 | { | |
340 | /* Make all the opcodes with the same name point to the same | |
341 | string. */ | |
342 | opcode->name = prev_name; | |
343 | } | |
344 | else | |
345 | { | |
346 | prev_name = opcode->name; | |
347 | hash_insert (opcode_hash_control, opcode->name, (char *) opcode); | |
348 | } | |
349 | } | |
350 | } | |
351 | ||
352 | static int reg_m; | |
353 | static int reg_n; | |
354 | static expressionS immediate; /* absolute expression */ | |
355 | ||
356 | /* Get a log2(val). */ | |
357 | static int | |
358 | log2 (val) | |
359 | unsigned int val; | |
360 | { | |
361 | int log = -1; | |
362 | while (val != 0) | |
363 | { | |
364 | log ++; | |
365 | val >>= 1; | |
366 | } | |
367 | ||
368 | return log; | |
369 | } | |
370 | ||
371 | /* Try to parse a reg name. */ | |
372 | static char * | |
373 | parse_reg (s, reg) | |
374 | char * s; | |
375 | unsigned * reg; | |
376 | { | |
377 | /* Strip leading whitespace. */ | |
378 | while (isspace (* s)) | |
379 | ++ s; | |
380 | ||
381 | if (tolower (s[0]) == 'r') | |
382 | { | |
383 | if (s[1] == '1' && s[2] >= '0' && s[2] <= '5') | |
384 | { | |
385 | *reg = 10 + s[2] - '0'; | |
386 | return s + 3; | |
387 | } | |
388 | ||
389 | if (s[1] >= '0' && s[1] <= '9') | |
390 | { | |
391 | *reg = s[1] - '0'; | |
392 | return s + 2; | |
393 | } | |
394 | } | |
395 | else if ( tolower (s[0]) == 's' | |
396 | && tolower (s[1]) == 'p' | |
397 | && (isspace (s[2]) || s[2] == ',')) | |
398 | { | |
399 | * reg = 0; | |
400 | return s + 2; | |
401 | } | |
402 | ||
403 | as_bad (_("register expected, but saw '%.6s'"), s); | |
404 | return s; | |
405 | } | |
406 | ||
407 | static struct Cregs | |
408 | { | |
409 | char * name; | |
410 | unsigned int crnum; | |
411 | } | |
412 | cregs[] = | |
413 | { | |
414 | { "psr", 0}, | |
415 | { "vbr", 1}, | |
416 | { "epsr", 2}, | |
417 | { "fpsr", 3}, | |
418 | { "epc", 4}, | |
419 | { "fpc", 5}, | |
420 | { "ss0", 6}, | |
421 | { "ss1", 7}, | |
422 | { "ss2", 8}, | |
423 | { "ss3", 9}, | |
424 | { "ss4", 10}, | |
425 | { "gcr", 11}, | |
426 | { "gsr", 12}, | |
427 | { "", 0} | |
428 | }; | |
429 | ||
430 | static char * | |
431 | parse_creg (s, reg) | |
432 | char * s; | |
433 | unsigned * reg; | |
434 | { | |
435 | int i; | |
436 | ||
437 | /* Strip leading whitespace. */ | |
438 | while (isspace (* s)) | |
439 | ++s; | |
440 | ||
441 | if ((tolower (s[0]) == 'c' && tolower (s[1]) == 'r')) | |
442 | { | |
443 | if (s[2] == '3' && s[3] >= '0' && s[3] <= '1') | |
444 | { | |
445 | *reg = 30 + s[3] - '0'; | |
446 | return s + 4; | |
447 | } | |
448 | ||
449 | if (s[2] == '2' && s[3] >= '0' && s[3] <= '9') | |
450 | { | |
451 | *reg = 20 + s[3] - '0'; | |
452 | return s + 4; | |
453 | } | |
454 | ||
455 | if (s[2] == '1' && s[3] >= '0' && s[3] <= '9') | |
456 | { | |
457 | *reg = 10 + s[3] - '0'; | |
458 | return s + 4; | |
459 | } | |
460 | ||
461 | if (s[2] >= '0' && s[2] <= '9') | |
462 | { | |
463 | *reg = s[2] - '0'; | |
464 | return s + 3; | |
465 | } | |
466 | } | |
467 | ||
468 | /* Look at alternate creg names before giving error. */ | |
469 | for (i = 0; cregs[i].name[0] != '\0'; i++) | |
470 | { | |
471 | char buf [10]; | |
472 | int length; | |
473 | int j; | |
474 | ||
475 | length = strlen (cregs[i].name); | |
476 | ||
477 | for (j = 0; j < length; j++) | |
478 | buf[j] = tolower (s[j]); | |
479 | ||
480 | if (strncmp (cregs[i].name, buf, length) == 0) | |
481 | { | |
482 | *reg = cregs[i].crnum; | |
483 | return s + length; | |
484 | } | |
485 | } | |
486 | ||
487 | as_bad (_("control register expected, but saw '%.6s'"), s); | |
488 | ||
489 | return s; | |
490 | } | |
491 | ||
492 | static char * | |
493 | parse_exp (s, e) | |
494 | char * s; | |
495 | expressionS * e; | |
496 | { | |
497 | char * save; | |
498 | char * new; | |
499 | ||
500 | /* Skip whitespace. */ | |
501 | while (isspace (* s)) | |
502 | ++ s; | |
503 | ||
504 | save = input_line_pointer; | |
505 | input_line_pointer = s; | |
506 | ||
507 | expression (e); | |
508 | ||
509 | if (e->X_op == O_absent) | |
510 | as_bad (_("missing operand")); | |
511 | ||
512 | new = input_line_pointer; | |
513 | input_line_pointer = save; | |
514 | ||
515 | return new; | |
516 | } | |
517 | ||
518 | static void | |
519 | make_name (s, p, n) | |
520 | char * s; | |
521 | char * p; | |
522 | int n; | |
523 | { | |
524 | static const char hex[] = "0123456789ABCDEF"; | |
525 | ||
526 | s[0] = p[0]; | |
527 | s[1] = p[1]; | |
528 | s[2] = p[2]; | |
529 | s[3] = hex[(n >> 12) & 0xF]; | |
530 | s[4] = hex[(n >> 8) & 0xF]; | |
531 | s[5] = hex[(n >> 4) & 0xF]; | |
532 | s[6] = hex[(n) & 0xF]; | |
533 | s[7] = 0; | |
534 | } | |
535 | ||
536 | static void | |
537 | dump_literals (isforce) | |
538 | int isforce; | |
539 | { | |
540 | int i; | |
541 | struct literal * p; | |
542 | struct symbol * brarsym; | |
543 | ||
544 | if (poolsize == 0) | |
545 | return; | |
546 | ||
547 | /* Must we branch around the literal table? */ | |
548 | if (isforce) | |
549 | { | |
550 | char * output; | |
551 | char brarname[8]; | |
552 | ||
553 | make_name (brarname, ".YP.", poolnumber); | |
554 | ||
555 | brarsym = symbol_make (brarname); | |
556 | ||
557 | symbol_table_insert (brarsym); | |
558 | ||
559 | output = frag_var (rs_machine_dependent, | |
560 | md_relax_table[C (UNCD_JUMP, UNCD32)].rlx_length, | |
561 | md_relax_table[C (UNCD_JUMP, UNCD12)].rlx_length, | |
562 | C (UNCD_JUMP, 0), brarsym, 0, 0); | |
563 | output[0] = INST_BYTE0 (MCORE_INST_BR); /* br .+xxx */ | |
564 | output[1] = INST_BYTE1 (MCORE_INST_BR); | |
565 | } | |
566 | ||
567 | /* Make sure that the section is sufficiently aligned and that | |
568 | the literal table is aligned within it. */ | |
569 | record_alignment (now_seg, 2); | |
570 | frag_align (2, 0, 0); | |
571 | ||
572 | colon (S_GET_NAME (poolsym)); | |
573 | ||
574 | for (i = 0, p = litpool; i < poolsize; i++, p++) | |
575 | emit_expr (& p->e, 4); | |
576 | ||
577 | if (isforce) | |
578 | colon (S_GET_NAME (brarsym)); | |
579 | ||
580 | poolsize = 0; | |
581 | } | |
582 | ||
583 | static void | |
584 | check_literals (kind, offset) | |
585 | int kind; | |
586 | int offset; | |
587 | { | |
588 | poolspan += offset; | |
589 | ||
590 | /* SPANCLOSE and SPANEXIT are smaller numbers than SPANPANIC. | |
591 | SPANPANIC means that we must dump now. | |
592 | kind == 0 is any old instruction. | |
593 | kind > 0 means we just had a control transfer instruction. | |
594 | kind == 1 means within a function | |
595 | kind == 2 means we just left a function | |
596 | ||
597 | The dump_literals (1) call inserts a branch around the table, so | |
598 | we first look to see if its a situation where we won't have to | |
599 | insert a branch (e.g., the previous instruction was an unconditional | |
600 | branch). | |
601 | ||
602 | SPANPANIC is the point where we must dump a single-entry pool. | |
603 | it accounts for alignments and an inserted branch. | |
604 | the 'poolsize*2' accounts for the scenario where we do: | |
605 | lrw r1,lit1; lrw r2,lit2; lrw r3,lit3 | |
606 | Note that the 'lit2' reference is 2 bytes further along | |
607 | but the literal it references will be 4 bytes further along, | |
608 | so we must consider the poolsize into this equation. | |
609 | This is slightly over-cautious, but guarantees that we won't | |
610 | panic because a relocation is too distant. */ | |
611 | ||
612 | if (poolspan > SPANCLOSE && kind > 0) | |
613 | dump_literals (0); | |
614 | else if (poolspan > SPANEXIT && kind > 1) | |
615 | dump_literals (0); | |
616 | else if (poolspan >= (SPANPANIC - poolsize * 2)) | |
617 | dump_literals (1); | |
618 | } | |
619 | ||
620 | static int | |
621 | enter_literal (e, ispcrel) | |
622 | expressionS * e; | |
623 | int ispcrel; | |
624 | { | |
625 | int i; | |
626 | struct literal * p; | |
627 | ||
628 | if (poolsize >= MAX_POOL_SIZE - 2) | |
629 | { | |
630 | /* The literal pool is as full as we can handle. We have | |
631 | to be 2 entries shy of the 1024/4=256 entries because we | |
632 | have to allow for the branch (2 bytes) and the alignment | |
633 | (2 bytes before the first insn referencing the pool and | |
634 | 2 bytes before the pool itself) == 6 bytes, rounds up | |
635 | to 2 entries. */ | |
636 | dump_literals (1); | |
637 | } | |
638 | ||
639 | if (poolsize == 0) | |
640 | { | |
641 | /* Create new literal pool. */ | |
642 | if (++ poolnumber > 0xFFFF) | |
643 | as_fatal (_("more than 65K literal pools")); | |
644 | ||
645 | make_name (poolname, ".XP.", poolnumber); | |
646 | poolsym = symbol_make (poolname); | |
647 | symbol_table_insert (poolsym); | |
648 | poolspan = 0; | |
649 | } | |
650 | ||
651 | /* Search pool for value so we don't have duplicates. */ | |
652 | for (p = litpool, i = 0; i < poolsize; i++, p++) | |
653 | { | |
654 | if (e->X_op == p->e.X_op | |
655 | && e->X_add_symbol == p->e.X_add_symbol | |
656 | && e->X_add_number == p->e.X_add_number | |
657 | && ispcrel == p->ispcrel) | |
658 | { | |
659 | p->refcnt ++; | |
660 | return i; | |
661 | } | |
662 | } | |
663 | ||
664 | p->refcnt = 1; | |
665 | p->ispcrel = ispcrel; | |
666 | p->e = * e; | |
667 | ||
668 | poolsize ++; | |
669 | ||
670 | return i; | |
671 | } | |
672 | ||
673 | /* Parse a literal specification. -- either new or old syntax. | |
674 | old syntax: the user supplies the label and places the literal. | |
675 | new syntax: we put it into the literal pool. */ | |
676 | static char * | |
677 | parse_rt (s, outputp, ispcrel, ep) | |
678 | char * s; | |
679 | char ** outputp; | |
680 | int ispcrel; | |
681 | expressionS * ep; | |
682 | { | |
683 | expressionS e; | |
684 | int n; | |
685 | ||
686 | if (ep) | |
687 | /* Indicate nothing there. */ | |
688 | ep->X_op = O_absent; | |
689 | ||
690 | if (*s == '[') | |
691 | { | |
692 | s = parse_exp (s + 1, & e); | |
693 | ||
694 | if (*s == ']') | |
695 | s++; | |
696 | else | |
697 | as_bad (_("missing ']'")); | |
698 | } | |
699 | else | |
700 | { | |
701 | s = parse_exp (s, & e); | |
702 | ||
703 | n = enter_literal (& e, ispcrel); | |
704 | ||
705 | if (ep) | |
706 | *ep = e; | |
707 | ||
708 | /* Create a reference to pool entry. */ | |
709 | e.X_op = O_symbol; | |
710 | e.X_add_symbol = poolsym; | |
711 | e.X_add_number = n << 2; | |
712 | } | |
713 | ||
714 | * outputp = frag_more (2); | |
715 | ||
716 | fix_new_exp (frag_now, (*outputp) - frag_now->fr_literal, 2, & e, 1, | |
717 | BFD_RELOC_MCORE_PCREL_IMM8BY4); | |
718 | ||
719 | return s; | |
720 | } | |
721 | ||
722 | static char * | |
723 | parse_imm (s, val, min, max) | |
724 | char * s; | |
725 | unsigned * val; | |
726 | unsigned min; | |
727 | unsigned max; | |
728 | { | |
729 | char * new; | |
730 | expressionS e; | |
731 | ||
732 | new = parse_exp (s, & e); | |
733 | ||
734 | if (e.X_op == O_absent) | |
735 | ; /* An error message has already been emitted. */ | |
736 | else if (e.X_op != O_constant) | |
737 | as_bad (_("operand must be a constant")); | |
738 | else if (e.X_add_number < min || e.X_add_number > max) | |
739 | as_bad (_("operand must be absolute in range %d..%d, not %d"), | |
740 | min, max, e.X_add_number); | |
741 | ||
742 | * val = e.X_add_number; | |
743 | ||
744 | return new; | |
745 | } | |
746 | ||
747 | static char * | |
748 | parse_mem (s, reg, off, siz) | |
749 | char * s; | |
750 | unsigned * reg; | |
751 | unsigned * off; | |
752 | unsigned siz; | |
753 | { | |
754 | char * new; | |
755 | ||
756 | * off = 0; | |
757 | ||
758 | while (isspace (* s)) | |
759 | ++ s; | |
760 | ||
761 | if (* s == '(') | |
762 | { | |
763 | s = parse_reg (s + 1, reg); | |
764 | ||
765 | while (isspace (* s)) | |
766 | ++ s; | |
767 | ||
768 | if (* s == ',') | |
769 | { | |
770 | s = parse_imm (s + 1, off, 0, 63); | |
771 | ||
772 | if (siz > 1) | |
773 | { | |
774 | if (siz > 2) | |
775 | { | |
776 | if (* off & 0x3) | |
777 | as_bad (_("operand must be a multiple of 4")); | |
778 | ||
779 | * off >>= 2; | |
780 | } | |
781 | else | |
782 | { | |
783 | if (* off & 0x1) | |
784 | as_bad (_("operand must be a multiple of 2")); | |
785 | ||
786 | * off >>= 1; | |
787 | } | |
788 | } | |
789 | } | |
790 | ||
791 | while (isspace (* s)) | |
792 | ++ s; | |
793 | ||
794 | if (* s == ')') | |
795 | s ++; | |
796 | } | |
797 | else | |
798 | as_bad (_("base register expected")); | |
799 | ||
800 | return s; | |
801 | } | |
802 | ||
803 | /* This is the guts of the machine-dependent assembler. STR points to a | |
804 | machine dependent instruction. This function is supposed to emit | |
805 | the frags/bytes it assembles to. */ | |
806 | ||
807 | void | |
808 | md_assemble (str) | |
809 | char * str; | |
810 | { | |
811 | char * op_start; | |
812 | char * op_end; | |
813 | mcore_opcode_info * opcode; | |
814 | char * output; | |
815 | int nlen = 0; | |
816 | unsigned short inst; | |
817 | unsigned reg; | |
818 | unsigned off; | |
819 | unsigned isize; | |
820 | expressionS e; | |
821 | char name[20]; | |
822 | ||
823 | /* Drop leading whitespace. */ | |
824 | while (isspace (* str)) | |
825 | str ++; | |
826 | ||
827 | /* Find the op code end. */ | |
828 | for (op_start = op_end = str; | |
829 | * op_end && nlen < 20 && !is_end_of_line [*op_end] && *op_end != ' '; | |
830 | op_end++) | |
831 | { | |
832 | name[nlen] = op_start[nlen]; | |
833 | nlen++; | |
834 | } | |
835 | ||
836 | name [nlen] = 0; | |
837 | ||
838 | if (nlen == 0) | |
839 | { | |
840 | as_bad (_("can't find opcode ")); | |
841 | return; | |
842 | } | |
843 | ||
844 | opcode = (mcore_opcode_info *) hash_find (opcode_hash_control, name); | |
845 | if (opcode == NULL) | |
846 | { | |
847 | as_bad (_("unknown opcode \"%s\""), name); | |
848 | return; | |
849 | } | |
850 | ||
851 | inst = opcode->inst; | |
852 | isize = 2; | |
853 | ||
854 | switch (opcode->opclass) | |
855 | { | |
856 | case O0: | |
857 | output = frag_more (2); | |
858 | break; | |
859 | ||
860 | case OT: | |
861 | op_end = parse_imm (op_end + 1, & reg, 0, 3); | |
862 | inst |= reg; | |
863 | output = frag_more (2); | |
864 | break; | |
865 | ||
866 | case O1: | |
867 | op_end = parse_reg (op_end + 1, & reg); | |
868 | inst |= reg; | |
869 | output = frag_more (2); | |
870 | break; | |
871 | ||
872 | case JMP: | |
873 | op_end = parse_reg (op_end + 1, & reg); | |
874 | inst |= reg; | |
875 | output = frag_more (2); | |
876 | /* In a sifilter mode, we emit this insn 2 times, | |
877 | fixes problem of an interrupt during a jmp.. */ | |
878 | if (sifilter_mode) | |
879 | { | |
880 | output[0] = (inst >> 8); | |
881 | output[1] = (inst); | |
882 | output = frag_more (2); | |
883 | } | |
884 | break; | |
885 | ||
886 | case JSR: | |
887 | op_end = parse_reg (op_end + 1, & reg); | |
888 | ||
889 | if (reg == 15) | |
890 | as_bad (_("invalid register: r15 illegal")); | |
891 | ||
892 | inst |= reg; | |
893 | output = frag_more (2); | |
894 | ||
895 | if (sifilter_mode) | |
896 | { | |
897 | /* Replace with: bsr .+2 ; addi r15,6; jmp rx ; jmp rx */ | |
898 | inst = MCORE_INST_BSR; /* with 0 displacement */ | |
899 | output[0] = (inst >> 8); | |
900 | output[1] = (inst); | |
901 | ||
902 | output = frag_more (2); | |
903 | inst = MCORE_INST_ADDI; | |
904 | inst |= 15; /* addi r15,6 */ | |
905 | inst |= (6 - 1) << 4; /* over the jmp's */ | |
906 | output[0] = (inst >> 8); | |
907 | output[1] = (inst); | |
908 | ||
909 | output = frag_more (2); | |
910 | inst = MCORE_INST_JMP | reg; | |
911 | output[0] = (inst >> 8); | |
912 | output[1] = (inst); | |
913 | ||
914 | output = frag_more (2); /* 2nd emitted in fallthru */ | |
915 | } | |
916 | break; | |
917 | ||
918 | case OC: | |
919 | op_end = parse_reg (op_end + 1, & reg); | |
920 | inst |= reg; | |
921 | ||
922 | /* Skip whitespace. */ | |
923 | while (isspace (* op_end)) | |
924 | ++ op_end; | |
925 | ||
926 | if (*op_end == ',') | |
927 | { | |
928 | op_end = parse_creg (op_end + 1, & reg); | |
929 | inst |= reg << 4; | |
930 | } | |
931 | ||
932 | output = frag_more (2); | |
933 | break; | |
934 | ||
935 | case O2: | |
936 | op_end = parse_reg (op_end + 1, & reg); | |
937 | inst |= reg; | |
938 | ||
939 | /* Skip whitespace. */ | |
940 | while (isspace (* op_end)) | |
941 | ++ op_end; | |
942 | ||
943 | if (* op_end == ',') | |
944 | { | |
945 | op_end = parse_reg (op_end + 1, & reg); | |
946 | inst |= reg << 4; | |
947 | } | |
948 | else | |
949 | as_bad (_("second operand missing")); | |
950 | ||
951 | output = frag_more (2); | |
952 | break; | |
953 | ||
954 | case X1: /* Handle both syntax-> xtrb- r1,rx OR xtrb- rx */ | |
955 | op_end = parse_reg (op_end + 1, & reg); | |
956 | ||
957 | /* Skip whitespace. */ | |
958 | while (isspace (* op_end)) | |
959 | ++ op_end; | |
960 | ||
961 | if (* op_end == ',') /* xtrb- r1,rx */ | |
962 | { | |
963 | if (reg != 1) | |
964 | as_bad (_("destination register must be r1")); | |
965 | ||
966 | op_end = parse_reg (op_end + 1, & reg); | |
967 | } | |
968 | ||
969 | inst |= reg; | |
970 | output = frag_more (2); | |
971 | break; | |
972 | ||
973 | case O1R1: /* div- rx,r1 */ | |
974 | op_end = parse_reg (op_end + 1, & reg); | |
975 | inst |= reg; | |
976 | ||
977 | /* Skip whitespace. */ | |
978 | while (isspace (* op_end)) | |
979 | ++ op_end; | |
980 | ||
981 | if (* op_end == ',') | |
982 | { | |
983 | op_end = parse_reg (op_end + 1, & reg); | |
984 | if (reg != 1) | |
985 | as_bad (_("source register must be r1")); | |
986 | } | |
987 | else | |
988 | as_bad (_("second operand missing")); | |
989 | ||
990 | output = frag_more (2); | |
991 | break; | |
992 | ||
993 | case OI: | |
994 | op_end = parse_reg (op_end + 1, & reg); | |
995 | inst |= reg; | |
996 | ||
997 | /* Skip whitespace. */ | |
998 | while (isspace (* op_end)) | |
999 | ++ op_end; | |
1000 | ||
1001 | if (* op_end == ',') | |
1002 | { | |
1003 | op_end = parse_imm (op_end + 1, & reg, 1, 32); | |
1004 | inst |= (reg - 1) << 4; | |
1005 | } | |
1006 | else | |
1007 | as_bad (_("second operand missing")); | |
1008 | ||
1009 | output = frag_more (2); | |
1010 | break; | |
1011 | ||
1012 | case OB: | |
1013 | op_end = parse_reg (op_end + 1, & reg); | |
1014 | inst |= reg; | |
1015 | ||
1016 | /* Skip whitespace. */ | |
1017 | while (isspace (* op_end)) | |
1018 | ++ op_end; | |
1019 | ||
1020 | if (* op_end == ',') | |
1021 | { | |
1022 | op_end = parse_imm (op_end + 1, & reg, 0, 31); | |
1023 | inst |= reg << 4; | |
1024 | } | |
1025 | else | |
1026 | as_bad (_("second operand missing")); | |
1027 | ||
1028 | output = frag_more (2); | |
1029 | break; | |
1030 | ||
1031 | case OB2: /* like OB, but arg is 2^n instead of n */ | |
1032 | op_end = parse_reg (op_end + 1, & reg); | |
1033 | inst |= reg; | |
1034 | ||
1035 | /* Skip whitespace. */ | |
1036 | while (isspace (* op_end)) | |
1037 | ++ op_end; | |
1038 | ||
1039 | if (* op_end == ',') | |
1040 | { | |
1041 | op_end = parse_imm (op_end + 1, & reg, 1, 1 << 31); | |
1042 | /* Further restrict the immediate to a power of two. */ | |
1043 | if ((reg & (reg - 1)) == 0) | |
1044 | reg = log2 (reg); | |
1045 | else | |
1046 | { | |
1047 | reg = 0; | |
1048 | as_bad (_("immediate is not a power of two")); | |
1049 | } | |
1050 | inst |= (reg) << 4; | |
1051 | } | |
1052 | else | |
1053 | as_bad (_("second operand missing")); | |
1054 | ||
1055 | output = frag_more (2); | |
1056 | break; | |
1057 | ||
1058 | case OBRa: /* Specific for bgeni: imm of 0->6 translate to movi. */ | |
1059 | case OBRb: | |
1060 | case OBRc: | |
1061 | op_end = parse_reg (op_end + 1, & reg); | |
1062 | inst |= reg; | |
1063 | ||
1064 | /* Skip whitespace. */ | |
1065 | while (isspace (* op_end)) | |
1066 | ++ op_end; | |
1067 | ||
1068 | if (* op_end == ',') | |
1069 | { | |
1070 | op_end = parse_imm (op_end + 1, & reg, 0, 31); | |
1071 | /* immediate values of 0 -> 6 translate to movi */ | |
1072 | if (reg <= 6) | |
1073 | { | |
1074 | inst = (inst & 0xF) | MCORE_INST_BGENI_ALT; | |
1075 | reg = 0x1 << reg; | |
1076 | as_warn (_("translating bgeni to movi")); | |
1077 | } | |
1078 | inst &= ~ 0x01f0; | |
1079 | inst |= reg << 4; | |
1080 | } | |
1081 | else | |
1082 | as_bad (_("second operand missing")); | |
1083 | ||
1084 | output = frag_more (2); | |
1085 | break; | |
1086 | ||
1087 | case OBR2: /* like OBR, but arg is 2^n instead of n */ | |
1088 | op_end = parse_reg (op_end + 1, & reg); | |
1089 | inst |= reg; | |
1090 | ||
1091 | /* Skip whitespace. */ | |
1092 | while (isspace (* op_end)) | |
1093 | ++ op_end; | |
1094 | ||
1095 | if (* op_end == ',') | |
1096 | { | |
1097 | op_end = parse_imm (op_end + 1, & reg, 1, 1 << 31); | |
1098 | ||
1099 | /* Further restrict the immediate to a power of two. */ | |
1100 | if ((reg & (reg - 1)) == 0) | |
1101 | reg = log2 (reg); | |
1102 | else | |
1103 | { | |
1104 | reg = 0; | |
1105 | as_bad (_("immediate is not a power of two")); | |
1106 | } | |
1107 | ||
1108 | /* Immediate values of 0 -> 6 translate to movi. */ | |
1109 | if (reg <= 6) | |
1110 | { | |
1111 | inst = (inst & 0xF) | MCORE_INST_BGENI_ALT; | |
1112 | reg = 0x1 << reg; | |
1113 | as_warn (_("translating mgeni to movi")); | |
1114 | } | |
1115 | ||
1116 | inst |= reg << 4; | |
1117 | } | |
1118 | else | |
1119 | as_bad (_("second operand missing")); | |
1120 | ||
1121 | output = frag_more (2); | |
1122 | break; | |
1123 | ||
1124 | case OMa: /* Specific for bmaski: imm 1->7 translate to movi. */ | |
1125 | case OMb: | |
1126 | case OMc: | |
1127 | op_end = parse_reg (op_end + 1, & reg); | |
1128 | inst |= reg; | |
1129 | ||
1130 | /* Skip whitespace. */ | |
1131 | while (isspace (* op_end)) | |
1132 | ++ op_end; | |
1133 | ||
1134 | if (* op_end == ',') | |
1135 | { | |
1136 | op_end = parse_imm (op_end + 1, & reg, 1, 32); | |
1137 | ||
1138 | /* Immediate values of 1 -> 7 translate to movi. */ | |
1139 | if (reg <= 7) | |
1140 | { | |
1141 | inst = (inst & 0xF) | MCORE_INST_BMASKI_ALT; | |
1142 | reg = (0x1 << reg) - 1; | |
1143 | inst |= reg << 4; | |
1144 | ||
1145 | as_warn (_("translating bmaski to movi")); | |
1146 | } | |
1147 | else | |
1148 | { | |
1149 | inst &= ~ 0x01F0; | |
1150 | inst |= (reg & 0x1F) << 4; | |
1151 | } | |
1152 | } | |
1153 | else | |
1154 | as_bad (_("second operand missing")); | |
1155 | ||
1156 | output = frag_more (2); | |
1157 | break; | |
1158 | ||
1159 | case SI: | |
1160 | op_end = parse_reg (op_end + 1, & reg); | |
1161 | inst |= reg; | |
1162 | ||
1163 | /* Skip whitespace. */ | |
1164 | while (isspace (* op_end)) | |
1165 | ++ op_end; | |
1166 | ||
1167 | if (* op_end == ',') | |
1168 | { | |
1169 | op_end = parse_imm (op_end + 1, & reg, 1, 31); | |
1170 | inst |= reg << 4; | |
1171 | } | |
1172 | else | |
1173 | as_bad (_("second operand missing")); | |
1174 | ||
1175 | output = frag_more (2); | |
1176 | break; | |
1177 | ||
1178 | case I7: | |
1179 | op_end = parse_reg (op_end + 1, & reg); | |
1180 | inst |= reg; | |
1181 | ||
1182 | /* Skip whitespace. */ | |
1183 | while (isspace (* op_end)) | |
1184 | ++ op_end; | |
1185 | ||
1186 | if (* op_end == ',') | |
1187 | { | |
1188 | op_end = parse_imm (op_end + 1, & reg, 0, 0x7F); | |
1189 | inst |= reg << 4; | |
1190 | } | |
1191 | else | |
1192 | as_bad (_("second operand missing")); | |
1193 | ||
1194 | output = frag_more (2); | |
1195 | break; | |
1196 | ||
1197 | case LS: | |
1198 | op_end = parse_reg (op_end + 1, & reg); | |
1199 | inst |= reg << 8; | |
1200 | ||
1201 | /* Skip whitespace. */ | |
1202 | while (isspace (* op_end)) | |
1203 | ++ op_end; | |
1204 | ||
1205 | if (* op_end == ',') | |
1206 | { | |
1207 | int size; | |
1208 | ||
1209 | if ((inst & 0x6000) == 0) | |
1210 | size = 4; | |
1211 | else if ((inst & 0x6000) == 0x4000) | |
1212 | size = 2; | |
1213 | else if ((inst & 0x6000) == 0x2000) | |
1214 | size = 1; | |
1215 | ||
1216 | op_end = parse_mem (op_end + 1, & reg, & off, size); | |
1217 | ||
1218 | if (off > 16) | |
1219 | as_bad (_("displacement too large (%d)"), off); | |
1220 | else | |
1221 | inst |= (reg) | (off << 4); | |
1222 | } | |
1223 | else | |
1224 | as_bad (_("second operand missing")); | |
1225 | ||
1226 | output = frag_more (2); | |
1227 | break; | |
1228 | ||
1229 | case LR: | |
1230 | op_end = parse_reg (op_end + 1, & reg); | |
1231 | ||
1232 | if (reg == 0 || reg == 15) | |
1233 | as_bad (_("Invalid register: r0 and r15 illegal")); | |
1234 | ||
1235 | inst |= (reg << 8); | |
1236 | ||
1237 | /* Skip whitespace. */ | |
1238 | while (isspace (* op_end)) | |
1239 | ++ op_end; | |
1240 | ||
1241 | if (* op_end == ',') | |
1242 | /* parse_rt calls frag_more() for us. */ | |
1243 | input_line_pointer = parse_rt (op_end + 1, & output, 0, 0); | |
1244 | else | |
1245 | { | |
1246 | as_bad (_("second operand missing")); | |
1247 | output = frag_more (2); /* save its space */ | |
1248 | } | |
1249 | break; | |
1250 | ||
1251 | case LJ: | |
1252 | input_line_pointer = parse_rt (op_end + 1, & output, 1, 0); | |
1253 | /* parse_rt() calls frag_more() for us. */ | |
1254 | break; | |
1255 | ||
1256 | case RM: | |
1257 | op_end = parse_reg (op_end + 1, & reg); | |
1258 | ||
1259 | if (reg == 0 || reg == 15) | |
1260 | as_bad (_("bad starting register: r0 and r15 invalid")); | |
1261 | ||
1262 | inst |= reg; | |
1263 | ||
1264 | /* Skip whitespace. */ | |
1265 | while (isspace (* op_end)) | |
1266 | ++ op_end; | |
1267 | ||
1268 | if (* op_end == '-') | |
1269 | { | |
1270 | op_end = parse_reg (op_end + 1, & reg); | |
1271 | ||
1272 | if (reg != 15) | |
1273 | as_bad (_("ending register must be r15")); | |
1274 | ||
1275 | /* Skip whitespace. */ | |
1276 | while (isspace (* op_end)) | |
1277 | ++ op_end; | |
1278 | } | |
1279 | ||
1280 | if (* op_end == ',') | |
1281 | { | |
1282 | op_end ++; | |
1283 | ||
1284 | /* Skip whitespace. */ | |
1285 | while (isspace (* op_end)) | |
1286 | ++ op_end; | |
1287 | ||
1288 | if (* op_end == '(') | |
1289 | { | |
1290 | op_end = parse_reg (op_end + 1, & reg); | |
1291 | ||
1292 | if (reg != 0) | |
1293 | as_bad (_("bad base register: must be r0")); | |
1294 | ||
1295 | if (* op_end == ')') | |
1296 | op_end ++; | |
1297 | } | |
1298 | else | |
1299 | as_bad (_("base register expected")); | |
1300 | } | |
1301 | else | |
1302 | as_bad (_("second operand missing")); | |
1303 | ||
1304 | output = frag_more (2); | |
1305 | break; | |
1306 | ||
1307 | case RQ: | |
1308 | op_end = parse_reg (op_end + 1, & reg); | |
1309 | ||
1310 | if (reg != 4) | |
1311 | as_fatal (_("first register must be r4")); | |
1312 | ||
1313 | /* Skip whitespace. */ | |
1314 | while (isspace (* op_end)) | |
1315 | ++ op_end; | |
1316 | ||
1317 | if (* op_end == '-') | |
1318 | { | |
1319 | op_end = parse_reg (op_end + 1, & reg); | |
1320 | ||
1321 | if (reg != 7) | |
1322 | as_fatal (_("last register must be r7")); | |
1323 | ||
1324 | /* Skip whitespace. */ | |
1325 | while (isspace (* op_end)) | |
1326 | ++ op_end; | |
1327 | ||
1328 | if (* op_end == ',') | |
1329 | { | |
1330 | op_end ++; | |
1331 | ||
1332 | /* Skip whitespace. */ | |
1333 | while (isspace (* op_end)) | |
1334 | ++ op_end; | |
1335 | ||
1336 | if (* op_end == '(') | |
1337 | { | |
1338 | op_end = parse_reg (op_end + 1, & reg); | |
1339 | ||
1340 | if (reg >= 4 && reg <= 7) | |
1341 | as_fatal ("base register cannot be r4, r5, r6, or r7"); | |
1342 | ||
1343 | inst |= reg; | |
1344 | ||
1345 | /* Skip whitespace. */ | |
1346 | while (isspace (* op_end)) | |
1347 | ++ op_end; | |
1348 | ||
1349 | if (* op_end == ')') | |
1350 | op_end ++; | |
1351 | } | |
1352 | else | |
1353 | as_bad (_("base register expected")); | |
1354 | } | |
1355 | else | |
1356 | as_bad (_("second operand missing")); | |
1357 | } | |
1358 | else | |
1359 | as_bad (_("reg-reg expected")); | |
1360 | ||
1361 | output = frag_more (2); | |
1362 | break; | |
1363 | ||
1364 | case BR: | |
1365 | input_line_pointer = parse_exp (op_end + 1, & e); | |
1366 | ||
1367 | output = frag_more (2); | |
1368 | ||
1369 | fix_new_exp (frag_now, output-frag_now->fr_literal, | |
1370 | 2, & e, 1, BFD_RELOC_MCORE_PCREL_IMM11BY2); | |
1371 | break; | |
1372 | ||
1373 | case BL: | |
1374 | op_end = parse_reg (op_end + 1, & reg); | |
1375 | inst |= reg << 4; | |
1376 | ||
1377 | /* Skip whitespace. */ | |
1378 | while (isspace (* op_end)) | |
1379 | ++ op_end; | |
1380 | ||
1381 | if (* op_end == ',') | |
1382 | { | |
1383 | op_end = parse_exp (op_end + 1, & e); | |
1384 | output = frag_more (2); | |
1385 | ||
1386 | fix_new_exp (frag_now, output-frag_now->fr_literal, | |
1387 | 2, & e, 1, BFD_RELOC_MCORE_PCREL_IMM4BY2); | |
1388 | } | |
1389 | else | |
1390 | { | |
1391 | as_bad (_("second operand missing")); | |
1392 | output = frag_more (2); | |
1393 | } | |
1394 | break; | |
1395 | ||
1396 | case JC: | |
1397 | input_line_pointer = parse_exp (op_end + 1, & e); | |
1398 | ||
1399 | output = frag_var (rs_machine_dependent, | |
1400 | md_relax_table[C (COND_JUMP, COND32)].rlx_length, | |
1401 | md_relax_table[C (COND_JUMP, COND12)].rlx_length, | |
1402 | C (COND_JUMP, 0), e.X_add_symbol, e.X_add_number, 0); | |
1403 | isize = C32_LEN; | |
1404 | break; | |
1405 | ||
1406 | case JU: | |
1407 | input_line_pointer = parse_exp (op_end + 1, & e); | |
1408 | output = frag_var (rs_machine_dependent, | |
1409 | md_relax_table[C (UNCD_JUMP, UNCD32)].rlx_length, | |
1410 | md_relax_table[C (UNCD_JUMP, UNCD12)].rlx_length, | |
1411 | C (UNCD_JUMP, 0), e.X_add_symbol, e.X_add_number, 0); | |
1412 | isize = U32_LEN; | |
1413 | break; | |
1414 | ||
1415 | case JL: | |
1416 | inst = MCORE_INST_JSRI; /* jsri */ | |
1417 | input_line_pointer = parse_rt (op_end + 1, & output, 1, & e); | |
1418 | /* parse_rt() calls frag_more for us */ | |
1419 | ||
1420 | /* Only do this if we know how to do it ... */ | |
1421 | if (e.X_op != O_absent && do_jsri2bsr) | |
1422 | { | |
1423 | /* Look at adding the R_PCREL_JSRIMM11BY2. */ | |
1424 | fix_new_exp (frag_now, output-frag_now->fr_literal, | |
1425 | 2, & e, 1, BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2); | |
1426 | } | |
1427 | break; | |
1428 | ||
1429 | case RSI: /* SI, but imm becomes 32-imm */ | |
1430 | op_end = parse_reg (op_end + 1, & reg); | |
1431 | inst |= reg; | |
1432 | ||
1433 | /* Skip whitespace. */ | |
1434 | while (isspace (* op_end)) | |
1435 | ++ op_end; | |
1436 | ||
1437 | if (* op_end == ',') | |
1438 | { | |
1439 | op_end = parse_imm (op_end + 1, & reg, 1, 31); | |
1440 | ||
1441 | reg = 32 - reg; | |
1442 | inst |= reg << 4; | |
1443 | } | |
1444 | else | |
1445 | as_bad (_("second operand missing")); | |
1446 | ||
1447 | output = frag_more (2); | |
1448 | break; | |
1449 | ||
1450 | case DO21: /* O2, dup rd, lit must be 1 */ | |
1451 | op_end = parse_reg (op_end + 1, & reg); | |
1452 | inst |= reg; | |
1453 | inst |= reg << 4; | |
1454 | ||
1455 | /* Skip whitespace. */ | |
1456 | while (isspace (* op_end)) | |
1457 | ++ op_end; | |
1458 | ||
1459 | if (* op_end == ',') | |
1460 | { | |
1461 | op_end = parse_imm (op_end + 1, & reg, 1, 31); | |
1462 | ||
1463 | if (reg != 1) | |
1464 | as_bad (_("second operand must be 1")); | |
1465 | } | |
1466 | else | |
1467 | as_bad (_("second operand missing")); | |
1468 | ||
1469 | output = frag_more (2); | |
1470 | break; | |
1471 | ||
1472 | case SIa: | |
1473 | op_end = parse_reg (op_end + 1, & reg); | |
1474 | inst |= reg; | |
1475 | ||
1476 | /* Skip whitespace. */ | |
1477 | while (isspace (* op_end)) | |
1478 | ++ op_end; | |
1479 | ||
1480 | if (* op_end == ',') | |
1481 | { | |
1482 | op_end = parse_imm (op_end + 1, & reg, 1, 31); | |
1483 | ||
1484 | if (reg == 0) | |
1485 | as_bad (_("zero used as immediate value")); | |
1486 | ||
1487 | inst |= reg << 4; | |
1488 | } | |
1489 | else | |
1490 | as_bad (_("second operand missing")); | |
1491 | ||
1492 | output = frag_more (2); | |
1493 | break; | |
1494 | ||
1495 | default: | |
1496 | as_bad (_("unimplemented opcode \"%s\""), name); | |
1497 | } | |
1498 | ||
1499 | output[0] = inst >> 8; | |
1500 | output[1] = inst; | |
1501 | ||
1502 | check_literals (opcode->transfer, isize); | |
1503 | } | |
1504 | ||
1505 | symbolS * | |
1506 | md_undefined_symbol (name) | |
1507 | char * name; | |
1508 | { | |
1509 | return 0; | |
1510 | } | |
1511 | ||
1512 | void | |
1513 | md_mcore_end () | |
1514 | { | |
1515 | dump_literals (0); | |
1516 | subseg_set (text_section, 0); | |
1517 | } | |
1518 | ||
1519 | /* Various routines to kill one day. */ | |
1520 | /* Equal to MAX_PRECISION in atof-ieee.c */ | |
1521 | #define MAX_LITTLENUMS 6 | |
1522 | ||
1523 | /* Turn a string in input_line_pointer into a floating point constant of type | |
1524 | type, and store the appropriate bytes in *litP. The number of LITTLENUMS | |
1525 | emitted is stored in *sizeP. An error message is returned, or NULL on OK.*/ | |
1526 | char * | |
1527 | md_atof (type, litP, sizeP) | |
1528 | int type; | |
1529 | char * litP; | |
1530 | int * sizeP; | |
1531 | { | |
1532 | int prec; | |
1533 | LITTLENUM_TYPE words[MAX_LITTLENUMS]; | |
1534 | LITTLENUM_TYPE * wordP; | |
1535 | char * t; | |
1536 | char * atof_ieee (); | |
1537 | ||
1538 | switch (type) | |
1539 | { | |
1540 | case 'f': | |
1541 | case 'F': | |
1542 | case 's': | |
1543 | case 'S': | |
1544 | prec = 2; | |
1545 | break; | |
1546 | ||
1547 | case 'd': | |
1548 | case 'D': | |
1549 | case 'r': | |
1550 | case 'R': | |
1551 | prec = 4; | |
1552 | break; | |
1553 | ||
1554 | case 'x': | |
1555 | case 'X': | |
1556 | prec = 6; | |
1557 | break; | |
1558 | ||
1559 | case 'p': | |
1560 | case 'P': | |
1561 | prec = 6; | |
1562 | break; | |
1563 | ||
1564 | default: | |
1565 | *sizeP = 0; | |
1566 | return _("Bad call to MD_NTOF()"); | |
1567 | } | |
1568 | ||
1569 | t = atof_ieee (input_line_pointer, type, words); | |
1570 | ||
1571 | if (t) | |
1572 | input_line_pointer = t; | |
1573 | ||
1574 | *sizeP = prec * sizeof (LITTLENUM_TYPE); | |
1575 | ||
1576 | for (wordP = words; prec--;) | |
1577 | { | |
1578 | md_number_to_chars (litP, (long) (*wordP++), sizeof (LITTLENUM_TYPE)); | |
1579 | litP += sizeof (LITTLENUM_TYPE); | |
1580 | } | |
1581 | ||
1582 | return 0; | |
1583 | } | |
1584 | \f | |
1585 | CONST char * md_shortopts = ""; | |
1586 | ||
1587 | #define OPTION_RELAX (OPTION_MD_BASE) | |
1588 | #define OPTION_JSRI2BSR_ON (OPTION_MD_BASE + 1) | |
1589 | #define OPTION_JSRI2BSR_OFF (OPTION_MD_BASE + 2) | |
1590 | #define OPTION_SIFILTER_ON (OPTION_MD_BASE + 3) | |
1591 | #define OPTION_SIFILTER_OFF (OPTION_MD_BASE + 4) | |
1592 | ||
1593 | struct option md_longopts[] = | |
1594 | { | |
1595 | { "relax", no_argument, NULL, OPTION_RELAX}, | |
1596 | { "no-jsri2bsr", no_argument, NULL, OPTION_JSRI2BSR_OFF}, | |
1597 | { "jsri2bsr", no_argument, NULL, OPTION_JSRI2BSR_ON}, | |
1598 | { "sifilter", no_argument, NULL, OPTION_SIFILTER_ON}, | |
1599 | { "no-sifilter", no_argument, NULL, OPTION_SIFILTER_OFF}, | |
1600 | { NULL, no_argument, NULL, 0} | |
1601 | }; | |
1602 | ||
1603 | size_t md_longopts_size = sizeof (md_longopts); | |
1604 | ||
1605 | int | |
1606 | md_parse_option (c, arg) | |
1607 | int c; | |
1608 | char * arg; | |
1609 | { | |
1610 | int i; | |
1611 | char * p; | |
1612 | ||
1613 | switch (c) | |
1614 | { | |
1615 | ||
1616 | case OPTION_RELAX: relax = 1; break; | |
1617 | case OPTION_JSRI2BSR_ON: do_jsri2bsr = 1; break; | |
1618 | case OPTION_JSRI2BSR_OFF: do_jsri2bsr = 0; break; | |
1619 | case OPTION_SIFILTER_ON: sifilter_mode = 1; break; | |
1620 | case OPTION_SIFILTER_OFF: sifilter_mode = 0; break; | |
1621 | default: return 0; | |
1622 | } | |
1623 | ||
1624 | return 1; | |
1625 | } | |
1626 | ||
1627 | void | |
1628 | md_show_usage (stream) | |
1629 | FILE * stream; | |
1630 | { | |
1631 | fprintf (stream, _("\ | |
1632 | MCORE specific options:\n\ | |
1633 | -{no-}jsri2bsr {dis}able jsri to bsr transformation (def: off)\n\ | |
1634 | -{no-}sifilter {dis}able silicon filter behavior (def: off)\n\ | |
1635 | -relax alter jump instructions for long displacements\n")); | |
1636 | } | |
1637 | \f | |
1638 | int md_short_jump_size; | |
1639 | ||
1640 | void | |
1641 | md_create_short_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol) | |
1642 | char * ptr; | |
1643 | addressT from_Nddr; | |
1644 | addressT to_Nddr; | |
1645 | fragS * frag; | |
1646 | symbolS * to_symbol; | |
1647 | { | |
1648 | as_fatal (_("failed sanity check: short_jump")); | |
1649 | } | |
1650 | ||
1651 | void | |
1652 | md_create_long_jump (ptr, from_Nddr, to_Nddr, frag, to_symbol) | |
1653 | char * ptr; | |
1654 | addressT from_Nddr; | |
1655 | addressT to_Nddr; | |
1656 | fragS * frag; | |
1657 | symbolS * to_symbol; | |
1658 | { | |
1659 | as_fatal (_("failed sanity check: long_jump")); | |
1660 | } | |
1661 | ||
1662 | /* Called after relaxing, change the frags so they know how big they are. */ | |
1663 | void | |
1664 | md_convert_frag (abfd, sec, fragP) | |
1665 | bfd * abfd; | |
1666 | segT sec; | |
1667 | register fragS * fragP; | |
1668 | { | |
1669 | unsigned char * buffer; | |
1670 | int targ_addr = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset; | |
1671 | ||
1672 | buffer = (unsigned char *) (fragP->fr_fix + fragP->fr_literal); | |
1673 | targ_addr += fragP->fr_symbol->sy_frag->fr_address; | |
1674 | ||
1675 | switch (fragP->fr_subtype) | |
1676 | { | |
1677 | case C (COND_JUMP, COND12): | |
1678 | case C (UNCD_JUMP, UNCD12): | |
1679 | { | |
1680 | /* Get the address of the end of the instruction */ | |
1681 | int next_inst = fragP->fr_fix + fragP->fr_address + 2; | |
1682 | unsigned char t0; | |
1683 | int disp = targ_addr - next_inst; | |
1684 | ||
1685 | if (disp & 1) | |
1686 | as_bad (_("odd displacement at %x"), next_inst - 2); | |
1687 | ||
1688 | disp >>= 1; | |
1689 | t0 = buffer[0] & 0xF8; | |
1690 | ||
1691 | md_number_to_chars (buffer, disp, 2); | |
1692 | ||
1693 | buffer[0] = (buffer[0] & 0x07) | t0; | |
1694 | fragP->fr_fix += 2; | |
1695 | fragP->fr_var = 0; | |
1696 | } | |
1697 | break; | |
1698 | ||
1699 | case C (COND_JUMP, COND32): | |
1700 | case C (COND_JUMP, UNDEF_WORD_DISP): | |
1701 | { | |
1702 | /* A conditional branch wont fit into 12 bits so: | |
1703 | * b!cond 1f | |
1704 | * jmpi 0f | |
1705 | * .align 2 | |
1706 | * 0: .long disp | |
1707 | * 1: | |
1708 | * | |
1709 | * if the b!cond is 4 byte aligned, the literal which would | |
1710 | * go at x+4 will also be aligned. | |
1711 | */ | |
1712 | int first_inst = fragP->fr_fix + fragP->fr_address; | |
1713 | int needpad = (first_inst & 3); | |
1714 | ||
1715 | buffer[0] ^= 0x08; /* Toggle T/F bit */ | |
1716 | ||
1717 | buffer[2] = INST_BYTE0 (MCORE_INST_JMPI); /* Build jmpi */ | |
1718 | buffer[3] = INST_BYTE1 (MCORE_INST_JMPI); | |
1719 | ||
1720 | if (needpad) | |
1721 | { | |
1722 | buffer[1] = 4; /* branch over jmpi, pad, and ptr */ | |
1723 | buffer[3] = 1; /* jmpi offset of 1 gets the pointer */ | |
1724 | buffer[4] = 0; /* alignment/pad */ | |
1725 | buffer[5] = 0; | |
1726 | buffer[6] = 0; /* space for 32 bit address */ | |
1727 | buffer[7] = 0; | |
1728 | buffer[8] = 0; | |
1729 | buffer[9] = 0; | |
1730 | ||
1731 | /* Make reloc for the long disp */ | |
1732 | fix_new (fragP, fragP->fr_fix + 6, 4, | |
1733 | fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32); | |
1734 | ||
1735 | fragP->fr_fix += C32_LEN; | |
1736 | } | |
1737 | else | |
1738 | { | |
1739 | /* See comment below about this given gas' limitations for | |
1740 | shrinking the fragment. '3' is the amount of code that | |
1741 | we inserted here, but '4' is right for the space we reserved | |
1742 | for this fragment. */ | |
1743 | buffer[1] = 3; /* branch over jmpi, and ptr */ | |
1744 | buffer[3] = 0; /* jmpi offset of 0 gets the pointer */ | |
1745 | buffer[4] = 0; /* space for 32 bit address */ | |
1746 | buffer[5] = 0; | |
1747 | buffer[6] = 0; | |
1748 | buffer[7] = 0; | |
1749 | ||
1750 | /* Make reloc for the long disp. */ | |
1751 | fix_new (fragP, fragP->fr_fix + 4, 4, | |
1752 | fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32); | |
1753 | fragP->fr_fix += C32_LEN; | |
1754 | ||
1755 | /* frag is actually shorter (see the other side of this ifdef) | |
1756 | but gas isn't prepared for that. We have to re-adjust | |
1757 | the branch displacement so that it goes beyond the | |
1758 | full length of the fragment, not just what we actually | |
1759 | filled in. */ | |
1760 | buffer[1] = 4; /* jmpi, ptr, and the 'tail pad' */ | |
1761 | } | |
1762 | ||
1763 | fragP->fr_var = 0; | |
1764 | } | |
1765 | break; | |
1766 | ||
1767 | case C (UNCD_JUMP, UNCD32): | |
1768 | case C (UNCD_JUMP, UNDEF_WORD_DISP): | |
1769 | { | |
1770 | /* An unconditional branch will not fit in 12 bits, make code which | |
1771 | looks like: | |
1772 | jmpi 0f | |
1773 | .align 2 | |
1774 | 0: .long disp | |
1775 | we need a pad if "first_inst" is 4 byte aligned. | |
1776 | [because the natural literal place is x + 2] */ | |
1777 | int first_inst = fragP->fr_fix + fragP->fr_address; | |
1778 | int needpad = !(first_inst & 3); | |
1779 | ||
1780 | buffer[0] = INST_BYTE0 (MCORE_INST_JMPI); /* Build jmpi */ | |
1781 | buffer[1] = INST_BYTE1 (MCORE_INST_JMPI); | |
1782 | ||
1783 | if (needpad) | |
1784 | { | |
1785 | buffer[1] = 1; /* jmpi offset of 1 since padded */ | |
1786 | buffer[2] = 0; /* alignment */ | |
1787 | buffer[3] = 0; | |
1788 | buffer[4] = 0; /* space for 32 bit address */ | |
1789 | buffer[5] = 0; | |
1790 | buffer[6] = 0; | |
1791 | buffer[7] = 0; | |
1792 | ||
1793 | /* Make reloc for the long disp */ | |
1794 | fix_new (fragP, fragP->fr_fix + 4, 4, | |
1795 | fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32); | |
1796 | ||
1797 | fragP->fr_fix += U32_LEN; | |
1798 | } | |
1799 | else | |
1800 | { | |
1801 | buffer[1] = 0; /* jmpi offset of 0 if no pad */ | |
1802 | buffer[2] = 0; /* space for 32 bit address */ | |
1803 | buffer[3] = 0; | |
1804 | buffer[4] = 0; | |
1805 | buffer[5] = 0; | |
1806 | ||
1807 | /* Make reloc for the long disp */ | |
1808 | fix_new (fragP, fragP->fr_fix + 2, 4, | |
1809 | fragP->fr_symbol, fragP->fr_offset, 0, BFD_RELOC_32); | |
1810 | fragP->fr_fix += U32_LEN; | |
1811 | } | |
1812 | ||
1813 | fragP->fr_var = 0; | |
1814 | } | |
1815 | break; | |
1816 | ||
1817 | default: | |
1818 | abort (); | |
1819 | } | |
1820 | } | |
1821 | ||
1822 | /* Applies the desired value to the specified location. | |
1823 | Also sets up addends for 'rela' type relocations. */ | |
1824 | int | |
1825 | md_apply_fix3 (fixP, valp, segment) | |
1826 | fixS * fixP; | |
1827 | valueT * valp; | |
1828 | segT segment; | |
1829 | { | |
1830 | char * buf = fixP->fx_where + fixP->fx_frag->fr_literal; | |
1831 | char * file = fixP->fx_file ? fixP->fx_file : _("unknown"); | |
1832 | const char * symname; | |
1833 | /* Note: use offsetT because it is signed, valueT is unsigned. */ | |
1834 | offsetT val = (offsetT) * valp; | |
1835 | ||
1836 | symname = fixP->fx_addsy ? S_GET_NAME (fixP->fx_addsy) : _("<unknown>"); | |
1837 | /* Save this for the addend in the relocation record. */ | |
1838 | fixP->fx_addnumber = val; | |
1839 | ||
1840 | /* If the fix is relative to a symbol which is not defined, or not | |
1841 | in the same segment as the fix, we cannot resolve it here. */ | |
1842 | if (fixP->fx_addsy != NULL | |
1843 | && ( ! S_IS_DEFINED (fixP->fx_addsy) | |
1844 | || (S_GET_SEGMENT (fixP->fx_addsy) != segment))) | |
1845 | { | |
1846 | fixP->fx_done = 0; | |
1847 | #ifdef OBJ_ELF | |
1848 | /* For ELF we can just return and let the reloc that will be generated | |
1849 | take care of everything. For COFF we still have to insert 'val' | |
1850 | into the insn since the addend field will be ignored. */ | |
1851 | return 0; | |
1852 | #endif | |
1853 | } | |
1854 | else | |
1855 | fixP->fx_done = 1; | |
1856 | ||
1857 | switch (fixP->fx_r_type) | |
1858 | { | |
1859 | case BFD_RELOC_MCORE_PCREL_IMM11BY2: /* second byte of 2 byte opcode */ | |
1860 | if ((val & 1) != 0) | |
1861 | as_bad_where (file, fixP->fx_line, | |
1862 | _("odd distance branch (0x%x bytes)"), val); | |
1863 | val /= 2; | |
1864 | if (((val & ~0x3ff) != 0) && ((val | 0x3ff) != -1)) | |
1865 | as_bad_where (file, fixP->fx_line, | |
1866 | _("pcrel for branch to %s too far (0x%x)"), | |
1867 | symname, val); | |
1868 | buf[0] |= ((val >> 8) & 0x7); | |
1869 | buf[1] |= (val & 0xff); | |
1870 | break; | |
1871 | ||
1872 | case BFD_RELOC_MCORE_PCREL_IMM8BY4: /* lower 8 bits of 2 byte opcode */ | |
1873 | val += 3; | |
1874 | val /= 4; | |
1875 | if (val & ~0xff) | |
1876 | as_bad_where (file, fixP->fx_line, | |
1877 | _("pcrel for lrw/jmpi/jsri to %s too far (0x%x)"), | |
1878 | symname, val); | |
1879 | else | |
1880 | buf[1] |= (val & 0xff); | |
1881 | break; | |
1882 | ||
1883 | case BFD_RELOC_MCORE_PCREL_IMM4BY2: /* loopt instruction */ | |
1884 | if ((val < -32) || (val > -2)) | |
1885 | as_bad_where (file, fixP->fx_line, | |
1886 | _("pcrel for loopt too far (0x%x)"), val); | |
1887 | val /= 2; | |
1888 | buf[1] |= (val & 0xf); | |
1889 | break; | |
1890 | ||
1891 | case BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2: | |
1892 | /* Conditional linker map jsri to bsr. */ | |
1893 | /* If its a local target and close enough, fix it. | |
1894 | NB: >= -2k for backwards bsr; < 2k for forwards... */ | |
1895 | if (fixP->fx_addsy == 0 && val >= -2048 && val < 2048) | |
1896 | { | |
1897 | long nval = (val / 2) & 0x7ff; | |
1898 | nval |= MCORE_INST_BSR; | |
1899 | ||
1900 | /* REPLACE the instruction, don't just modify it. */ | |
1901 | buf[0] = ((nval >> 8) & 0xff); | |
1902 | buf[1] = (nval & 0xff); | |
1903 | } | |
1904 | else | |
1905 | fixP->fx_done = 0; | |
1906 | break; | |
1907 | ||
1908 | case BFD_RELOC_MCORE_PCREL_32: | |
1909 | case BFD_RELOC_VTABLE_INHERIT: | |
1910 | case BFD_RELOC_VTABLE_ENTRY: | |
1911 | fixP->fx_done = 0; | |
1912 | break; | |
1913 | ||
1914 | default: | |
1915 | if (fixP->fx_addsy != NULL) | |
1916 | { | |
1917 | /* If the fix is an absolute reloc based on a symbol's | |
1918 | address, then it cannot be resolved until the final link. */ | |
1919 | fixP->fx_done = 0; | |
1920 | } | |
1921 | #ifdef OBJ_ELF | |
1922 | else | |
1923 | #endif | |
1924 | { | |
1925 | if (fixP->fx_size == 4) | |
1926 | { | |
1927 | *buf++ = val >> 24; | |
1928 | *buf++ = val >> 16; | |
1929 | *buf++ = val >> 8; | |
1930 | *buf = val; | |
1931 | } | |
1932 | else if (fixP->fx_size == 2 && val >= -32768 && val <= 32767) | |
1933 | { | |
1934 | *buf++ = val >> 8; | |
1935 | *buf = val; | |
1936 | } | |
1937 | else if (fixP->fx_size == 1 && val >= -256 && val <= 255) | |
1938 | *buf = val; | |
1939 | else | |
1940 | abort (); | |
1941 | } | |
1942 | break; | |
1943 | } | |
1944 | ||
1945 | return 0; /* Return value is ignored. */ | |
1946 | } | |
1947 | ||
1948 | void | |
1949 | md_operand (expressionP) | |
1950 | expressionS * expressionP; | |
1951 | { | |
1952 | /* Ignore leading hash symbol, if poresent. */ | |
1953 | if (* input_line_pointer == '#') | |
1954 | { | |
1955 | input_line_pointer ++; | |
1956 | expression (expressionP); | |
1957 | } | |
1958 | } | |
1959 | ||
1960 | int md_long_jump_size; | |
1961 | ||
1962 | /* Called just before address relaxation, return the length | |
1963 | by which a fragment must grow to reach it's destination. */ | |
1964 | int | |
1965 | md_estimate_size_before_relax (fragP, segment_type) | |
1966 | register fragS * fragP; | |
1967 | register segT segment_type; | |
1968 | { | |
1969 | switch (fragP->fr_subtype) | |
1970 | { | |
1971 | case C (UNCD_JUMP, UNDEF_DISP): | |
1972 | /* Used to be a branch to somewhere which was unknown. */ | |
1973 | if (!fragP->fr_symbol) | |
1974 | { | |
1975 | fragP->fr_subtype = C (UNCD_JUMP, UNCD12); | |
1976 | fragP->fr_var = md_relax_table[C (UNCD_JUMP, UNCD12)].rlx_length; | |
1977 | } | |
1978 | else if (S_GET_SEGMENT (fragP->fr_symbol) == segment_type) | |
1979 | { | |
1980 | fragP->fr_subtype = C (UNCD_JUMP, UNCD12); | |
1981 | fragP->fr_var = md_relax_table[C (UNCD_JUMP, UNCD12)].rlx_length; | |
1982 | } | |
1983 | else | |
1984 | { | |
1985 | fragP->fr_subtype = C (UNCD_JUMP, UNDEF_WORD_DISP); | |
1986 | fragP->fr_var = md_relax_table[C (UNCD_JUMP, UNCD32)].rlx_length; | |
1987 | return md_relax_table[C (UNCD_JUMP, UNCD32)].rlx_length; | |
1988 | } | |
1989 | break; | |
1990 | ||
1991 | default: | |
1992 | abort (); | |
1993 | ||
1994 | case C (COND_JUMP, UNDEF_DISP): | |
1995 | /* Used to be a branch to somewhere which was unknown. */ | |
1996 | if (fragP->fr_symbol | |
1997 | && S_GET_SEGMENT (fragP->fr_symbol) == segment_type) | |
1998 | { | |
1999 | /* Got a symbol and it's defined in this segment, become byte | |
2000 | sized - maybe it will fix up */ | |
2001 | fragP->fr_subtype = C (COND_JUMP, COND12); | |
2002 | fragP->fr_var = md_relax_table[C (COND_JUMP, COND12)].rlx_length; | |
2003 | } | |
2004 | else if (fragP->fr_symbol) | |
2005 | { | |
2006 | /* Its got a segment, but its not ours, so it will always be long. */ | |
2007 | fragP->fr_subtype = C (COND_JUMP, UNDEF_WORD_DISP); | |
2008 | fragP->fr_var = md_relax_table[C (COND_JUMP, COND32)].rlx_length; | |
2009 | return md_relax_table[C (COND_JUMP, COND32)].rlx_length; | |
2010 | } | |
2011 | else | |
2012 | { | |
2013 | /* We know the abs value. */ | |
2014 | fragP->fr_subtype = C (COND_JUMP, COND12); | |
2015 | fragP->fr_var = md_relax_table[C (COND_JUMP, COND12)].rlx_length; | |
2016 | } | |
2017 | ||
2018 | break; | |
2019 | } | |
2020 | ||
2021 | return fragP->fr_var; | |
2022 | } | |
2023 | ||
2024 | /* Put number into target byte order */ | |
2025 | ||
2026 | void | |
2027 | md_number_to_chars (ptr, use, nbytes) | |
2028 | char * ptr; | |
2029 | valueT use; | |
2030 | int nbytes; | |
2031 | { | |
2032 | switch (nbytes) | |
2033 | { | |
2034 | case 4: *ptr++ = (use >> 24) & 0xff; /* fall through */ | |
2035 | case 3: *ptr++ = (use >> 16) & 0xff; /* fall through */ | |
2036 | case 2: *ptr++ = (use >> 8) & 0xff; /* fall through */ | |
2037 | case 1: *ptr++ = (use >> 0) & 0xff; break; | |
2038 | default: abort (); | |
2039 | } | |
2040 | } | |
2041 | ||
2042 | /* Round up a section size to the appropriate boundary. */ | |
2043 | valueT | |
2044 | md_section_align (segment, size) | |
2045 | segT segment; | |
2046 | valueT size; | |
2047 | { | |
2048 | return size; /* Byte alignment is fine */ | |
2049 | } | |
2050 | ||
2051 | ||
2052 | /* The location from which a PC relative jump should be calculated, | |
2053 | given a PC relative reloc. */ | |
2054 | long | |
2055 | md_pcrel_from_section (fixp, sec) | |
2056 | fixS * fixp; | |
2057 | segT sec; | |
2058 | { | |
2059 | #ifdef OBJ_ELF | |
2060 | /* If the symbol is undefined or defined in another section | |
2061 | we leave the add number alone for the linker to fix it later. | |
2062 | Only account for the PC pre-bump (which is 2 bytes on the MCore). */ | |
2063 | if (fixp->fx_addsy != (symbolS *) NULL | |
2064 | && (! S_IS_DEFINED (fixp->fx_addsy) | |
2065 | || (S_GET_SEGMENT (fixp->fx_addsy) != sec))) | |
2066 | ||
2067 | { | |
2068 | assert (fixp->fx_size == 2); /* must be an insn */ | |
2069 | return fixp->fx_size; | |
2070 | } | |
2071 | #endif | |
2072 | ||
2073 | /* The case where we are going to resolve things... */ | |
2074 | return fixp->fx_size + fixp->fx_where + fixp->fx_frag->fr_address; | |
2075 | } | |
2076 | ||
2077 | #define F(SZ,PCREL) (((SZ) << 1) + (PCREL)) | |
2078 | #define MAP(SZ,PCREL,TYPE) case F (SZ, PCREL): code = (TYPE); break | |
2079 | ||
2080 | arelent * | |
2081 | tc_gen_reloc (section, fixp) | |
2082 | asection * section; | |
2083 | fixS * fixp; | |
2084 | { | |
2085 | arelent * rel; | |
2086 | bfd_reloc_code_real_type code; | |
2087 | int handled = 0; | |
2088 | ||
2089 | switch (fixp->fx_r_type) | |
2090 | { | |
2091 | /* These confuse the size/pcrel macro approach. */ | |
2092 | case BFD_RELOC_VTABLE_INHERIT: | |
2093 | case BFD_RELOC_VTABLE_ENTRY: | |
2094 | case BFD_RELOC_MCORE_PCREL_IMM4BY2: | |
2095 | case BFD_RELOC_MCORE_PCREL_IMM8BY4: | |
2096 | case BFD_RELOC_MCORE_PCREL_IMM11BY2: | |
2097 | case BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2: | |
2098 | code = fixp->fx_r_type; | |
2099 | break; | |
2100 | ||
2101 | default: | |
2102 | switch (F (fixp->fx_size, fixp->fx_pcrel)) | |
2103 | { | |
2104 | MAP (1, 0, BFD_RELOC_8); | |
2105 | MAP (2, 0, BFD_RELOC_16); | |
2106 | MAP (4, 0, BFD_RELOC_32); | |
2107 | MAP (1, 1, BFD_RELOC_8_PCREL); | |
2108 | MAP (2, 1, BFD_RELOC_16_PCREL); | |
2109 | MAP (4, 1, BFD_RELOC_32_PCREL); | |
2110 | default: | |
2111 | code = fixp->fx_r_type; | |
2112 | as_bad (_("Can not do %d byte %srelocation"), | |
2113 | fixp->fx_size, | |
2114 | fixp->fx_pcrel ? _("pc-relative") : ""); | |
2115 | } | |
2116 | break; | |
2117 | } | |
2118 | ||
2119 | rel = (arelent *) xmalloc (sizeof (arelent)); | |
2120 | rel->sym_ptr_ptr = & fixp->fx_addsy->bsym; | |
2121 | rel->address = fixp->fx_frag->fr_address + fixp->fx_where; | |
2122 | /* Always pass the addend along! */ | |
2123 | rel->addend = fixp->fx_addnumber; | |
2124 | ||
2125 | rel->howto = bfd_reloc_type_lookup (stdoutput, code); | |
2126 | ||
2127 | if (rel->howto == NULL) | |
2128 | { | |
2129 | as_bad_where (fixp->fx_file, fixp->fx_line, | |
2130 | _("Cannot represent relocation type %s"), | |
2131 | bfd_get_reloc_code_name (code)); | |
2132 | ||
2133 | /* Set howto to a garbage value so that we can keep going. */ | |
2134 | rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32); | |
2135 | assert (rel->howto != NULL); | |
2136 | } | |
2137 | ||
2138 | return rel; | |
2139 | } | |
2140 | ||
2141 | #ifdef OBJ_ELF | |
2142 | /* See whether we need to force a relocation into the output file. | |
2143 | This is used to force out switch and PC relative relocations when | |
2144 | relaxing. */ | |
2145 | int | |
2146 | mcore_force_relocation (fix) | |
2147 | fixS * fix; | |
2148 | { | |
2149 | if ( fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT | |
2150 | || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY) | |
2151 | return 1; | |
2152 | ||
2153 | return 0; | |
2154 | } | |
2155 | ||
2156 | /* Return true if the fix can be handled by GAS, false if it must | |
2157 | be passed through to the linker. */ | |
2158 | boolean | |
2159 | mcore_fix_adjustable (fixP) | |
2160 | fixS * fixP; | |
2161 | { | |
2162 | if (fixP->fx_addsy == NULL) | |
2163 | return 1; | |
2164 | ||
2165 | /* We need the symbol name for the VTABLE entries. */ | |
2166 | if ( fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT | |
2167 | || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY) | |
2168 | return 0; | |
2169 | ||
2170 | return 1; | |
2171 | } | |
2172 | ||
2173 | /* Handle the .section pseudo-op. This is like the usual one, but it | |
2174 | dumps the literal pool before changing the section. */ | |
2175 | static void | |
2176 | mcore_s_section (ignore) | |
2177 | int ignore; | |
2178 | { | |
2179 | dump_literals (0); | |
2180 | ||
2181 | obj_elf_section (ignore); | |
2182 | } | |
2183 | #endif /* OBJ_ELF */ |