tc-pru.c revision 1.1 1 /* TI PRU assembler.
2 Copyright (C) 2014-2018 Free Software Foundation, Inc.
3 Contributed by Dimitar Dimitrov <dimitar (at) dinux.eu>
4 Based on tc-nios2.c
5
6 This file is part of GAS, the GNU Assembler.
7
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
22
23 #include "as.h"
24 #include "bfd_stdint.h"
25 #include "opcode/pru.h"
26 #include "elf/pru.h"
27 #include "tc-pru.h"
28 #include "bfd.h"
29 #include "dwarf2dbg.h"
30 #include "subsegs.h"
31 #include "safe-ctype.h"
32 #include "dw2gencfi.h"
33
34 #ifndef OBJ_ELF
35 /* We are not supporting any other target so we throw a compile time error. */
36 #error "OBJ_ELF not defined"
37 #endif
38
39 /* This array holds the chars that always start a comment. If the
40 pre-processor is disabled, these aren't very useful. */
41 const char comment_chars[] = "#;";
42
43 /* This array holds the chars that only start a comment at the beginning of
44 a line. If the line seems to have the form '# 123 filename'
45 .line and .file directives will appear in the pre-processed output. */
46 /* Note that input_file.c hand checks for '#' at the beginning of the
47 first line of the input file. This is because the compiler outputs
48 #NO_APP at the beginning of its output. */
49 /* Also note that C style comments are always supported. */
50 const char line_comment_chars[] = "#;*";
51
52 /* This array holds machine specific line separator characters. */
53 const char line_separator_chars[] = "";
54
55 /* Chars that can be used to separate mant from exp in floating point nums. */
56 const char EXP_CHARS[] = "eE";
57
58 /* Chars that mean this number is a floating point constant.
59 As in 0f12.456
60 or 0d1.2345e12 */
61 const char FLT_CHARS[] = "rRsSfFdDxXpP";
62
63 /* Machine-dependent command-line options. */
64
65 struct pru_opt_s
66 {
67 /* -mno-link-relax / -mlink-relax: generate (or not)
68 relocations for linker relaxation. */
69 bfd_boolean link_relax;
70
71 /* -mno-warn-regname-label: do not output a warning that a label name
72 matches a register name. */
73 bfd_boolean warn_regname_label;
74 };
75
76 static struct pru_opt_s pru_opt = { TRUE, TRUE };
77
78 const char *md_shortopts = "r";
79
80 enum options
81 {
82 OPTION_LINK_RELAX = OPTION_MD_BASE + 1,
83 OPTION_NO_LINK_RELAX,
84 OPTION_NO_WARN_REGNAME_LABEL,
85 };
86
87 struct option md_longopts[] = {
88 { "mlink-relax", no_argument, NULL, OPTION_LINK_RELAX },
89 { "mno-link-relax", no_argument, NULL, OPTION_NO_LINK_RELAX },
90 { "mno-warn-regname-label", no_argument, NULL,
91 OPTION_NO_WARN_REGNAME_LABEL },
92 { NULL, no_argument, NULL, 0 }
93 };
94
95 size_t md_longopts_size = sizeof (md_longopts);
96
97 typedef struct pru_insn_reloc
98 {
99 /* Any expression in the instruction is parsed into this field,
100 which is passed to fix_new_exp () to generate a fixup. */
101 expressionS reloc_expression;
102
103 /* The type of the relocation to be applied. */
104 bfd_reloc_code_real_type reloc_type;
105
106 /* PC-relative. */
107 unsigned int reloc_pcrel;
108
109 /* The next relocation to be applied to the instruction. */
110 struct pru_insn_reloc *reloc_next;
111 } pru_insn_relocS;
112
113 /* This struct is used to hold state when assembling instructions. */
114 typedef struct pru_insn_info
115 {
116 /* Assembled instruction. */
117 unsigned long insn_code;
118 /* Used for assembling LDI32. */
119 unsigned long ldi32_imm32;
120
121 /* Pointer to the relevant bit of the opcode table. */
122 const struct pru_opcode *insn_pru_opcode;
123 /* After parsing ptrs to the tokens in the instruction fill this array
124 it is terminated with a null pointer (hence the first +1).
125 The second +1 is because in some parts of the code the opcode
126 is not counted as a token, but still placed in this array. */
127 const char *insn_tokens[PRU_MAX_INSN_TOKENS + 1 + 1];
128
129 /* This holds information used to generate fixups
130 and eventually relocations if it is not null. */
131 pru_insn_relocS *insn_reloc;
132 } pru_insn_infoS;
133
134 /* Opcode hash table. */
135 static struct hash_control *pru_opcode_hash = NULL;
136 #define pru_opcode_lookup(NAME) \
137 ((struct pru_opcode *) hash_find (pru_opcode_hash, (NAME)))
138
139 /* Register hash table. */
140 static struct hash_control *pru_reg_hash = NULL;
141 #define pru_reg_lookup(NAME) \
142 ((struct pru_reg *) hash_find (pru_reg_hash, (NAME)))
143
144 /* The known current alignment of the current section. */
145 static int pru_current_align;
146 static segT pru_current_align_seg;
147
148 static int pru_auto_align_on = 1;
149
150 /* The last seen label in the current section. This is used to auto-align
151 labels preceding instructions. */
152 static symbolS *pru_last_label;
153
154
155 /** Utility routines. */
157 /* Function md_chars_to_number takes the sequence of
158 bytes in buf and returns the corresponding value
159 in an int. n must be 1, 2, 4 or 8. */
160 static uint64_t
161 md_chars_to_number (char *buf, int n)
162 {
163 int i;
164 uint64_t val;
165
166 gas_assert (n == 1 || n == 2 || n == 4 || n == 8);
167
168 val = 0;
169 for (i = 0; i < n; ++i)
170 val = val | ((buf[i] & 0xff) << 8 * i);
171 return val;
172 }
173
174
175 /* This function turns a C long int, short int or char
176 into the series of bytes that represent the number
177 on the target machine. */
178 void
179 md_number_to_chars (char *buf, valueT val, int n)
180 {
181 gas_assert (n == 1 || n == 2 || n == 4 || n == 8);
182 number_to_chars_littleendian (buf, val, n);
183 }
184
185 /* Turn a string in input_line_pointer into a floating point constant
186 of type TYPE, and store the appropriate bytes in *LITP. The number
187 of LITTLENUMS emitted is stored in *SIZEP. An error message is
188 returned, or NULL on OK. */
189 const char *
190 md_atof (int type, char *litP, int *sizeP)
191 {
192 return ieee_md_atof (type, litP, sizeP, FALSE);
193 }
194
195 /* Return true if STR starts with PREFIX, which should be a string literal. */
196 #define strprefix(STR, PREFIX) \
197 (strncmp ((STR), PREFIX, strlen (PREFIX)) == 0)
198
199 /* nop fill pattern for text section. */
200 static char const nop[4] = { 0xe0, 0xe0, 0xe0, 0x12 };
201
202 /* Handles all machine-dependent alignment needs. */
203 static void
204 pru_align (int log_size, const char *pfill, symbolS *label)
205 {
206 int align;
207 long max_alignment = 15;
208
209 /* The front end is prone to changing segments out from under us
210 temporarily when -g is in effect. */
211 int switched_seg_p = (pru_current_align_seg != now_seg);
212
213 align = log_size;
214 if (align > max_alignment)
215 {
216 align = max_alignment;
217 as_bad (_("Alignment too large: %d assumed"), align);
218 }
219 else if (align < 0)
220 {
221 as_warn (_("Alignment negative: 0 assumed"));
222 align = 0;
223 }
224
225 if (align != 0)
226 {
227 if (subseg_text_p (now_seg) && align >= 2)
228 {
229 /* First, make sure we're on a four-byte boundary, in case
230 someone has been putting .byte values the text section. */
231 if (pru_current_align < 2 || switched_seg_p)
232 frag_align (2, 0, 0);
233
234 /* Now fill in the alignment pattern. */
235 if (pfill != NULL)
236 frag_align_pattern (align, pfill, sizeof nop, 0);
237 else
238 frag_align (align, 0, 0);
239 }
240 else
241 frag_align (align, 0, 0);
242
243 if (!switched_seg_p)
244 pru_current_align = align;
245
246 /* If the last label was in a different section we can't align it. */
247 if (label != NULL && !switched_seg_p)
248 {
249 symbolS *sym;
250 int label_seen = FALSE;
251 struct frag *old_frag;
252 valueT old_value;
253 valueT new_value;
254
255 gas_assert (S_GET_SEGMENT (label) == now_seg);
256
257 old_frag = symbol_get_frag (label);
258 old_value = S_GET_VALUE (label);
259 new_value = (valueT) frag_now_fix ();
260
261 /* It is possible to have more than one label at a particular
262 address, especially if debugging is enabled, so we must
263 take care to adjust all the labels at this address in this
264 fragment. To save time we search from the end of the symbol
265 list, backwards, since the symbols we are interested in are
266 almost certainly the ones that were most recently added.
267 Also to save time we stop searching once we have seen at least
268 one matching label, and we encounter a label that is no longer
269 in the target fragment. Note, this search is guaranteed to
270 find at least one match when sym == label, so no special case
271 code is necessary. */
272 for (sym = symbol_lastP; sym != NULL; sym = symbol_previous (sym))
273 if (symbol_get_frag (sym) == old_frag
274 && S_GET_VALUE (sym) == old_value)
275 {
276 label_seen = TRUE;
277 symbol_set_frag (sym, frag_now);
278 S_SET_VALUE (sym, new_value);
279 }
280 else if (label_seen && symbol_get_frag (sym) != old_frag)
281 break;
282 }
283 record_alignment (now_seg, align);
284 }
285 }
286
287
288 /** Support for self-check mode. */
290
291 /* Mode of the assembler. */
292 typedef enum
293 {
294 PRU_MODE_ASSEMBLE, /* Ordinary operation. */
295 PRU_MODE_TEST /* Hidden mode used for self testing. */
296 } PRU_MODE;
297
298 static PRU_MODE pru_mode = PRU_MODE_ASSEMBLE;
299
300 /* This function is used to in self-checking mode
301 to check the assembled instruction.
302 OPCODE should be the assembled opcode, and exp_opcode
303 the parsed string representing the expected opcode. */
304
305 static void
306 pru_check_assembly (unsigned int opcode, const char *exp_opcode)
307 {
308 if (pru_mode == PRU_MODE_TEST)
309 {
310 if (exp_opcode == NULL)
311 as_bad (_("expecting opcode string in self test mode"));
312 else if (opcode != strtoul (exp_opcode, NULL, 16))
313 as_bad (_("assembly 0x%08x, expected %s"), opcode, exp_opcode);
314 }
315 }
316
317
318 /** Support for machine-dependent assembler directives. */
320 /* Handle the .align pseudo-op. This aligns to a power of two. It
321 also adjusts any current instruction label. We treat this the same
322 way the MIPS port does: .align 0 turns off auto alignment. */
323 static void
324 s_pru_align (int ignore ATTRIBUTE_UNUSED)
325 {
326 int align;
327 char fill;
328 const char *pfill = NULL;
329 long max_alignment = 15;
330
331 align = get_absolute_expression ();
332 if (align > max_alignment)
333 {
334 align = max_alignment;
335 as_bad (_("Alignment too large: %d assumed"), align);
336 }
337 else if (align < 0)
338 {
339 as_warn (_("Alignment negative: 0 assumed"));
340 align = 0;
341 }
342
343 if (*input_line_pointer == ',')
344 {
345 input_line_pointer++;
346 fill = get_absolute_expression ();
347 pfill = (const char *) &fill;
348 }
349 else if (subseg_text_p (now_seg))
350 pfill = (const char *) &nop;
351 else
352 {
353 pfill = NULL;
354 pru_last_label = NULL;
355 }
356
357 if (align != 0)
358 {
359 pru_auto_align_on = 1;
360 pru_align (align, pfill, pru_last_label);
361 pru_last_label = NULL;
362 }
363 else
364 pru_auto_align_on = 0;
365
366 demand_empty_rest_of_line ();
367 }
368
369 /* Handle the .text pseudo-op. This is like the usual one, but it
370 clears the saved last label and resets known alignment. */
371 static void
372 s_pru_text (int i)
373 {
374 s_text (i);
375 pru_last_label = NULL;
376 pru_current_align = 0;
377 pru_current_align_seg = now_seg;
378 }
379
380 /* Handle the .data pseudo-op. This is like the usual one, but it
381 clears the saved last label and resets known alignment. */
382 static void
383 s_pru_data (int i)
384 {
385 s_data (i);
386 pru_last_label = NULL;
387 pru_current_align = 0;
388 pru_current_align_seg = now_seg;
389 }
390
391 /* Handle the .section pseudo-op. This is like the usual one, but it
392 clears the saved last label and resets known alignment. */
393 static void
394 s_pru_section (int ignore)
395 {
396 obj_elf_section (ignore);
397 pru_last_label = NULL;
398 pru_current_align = 0;
399 pru_current_align_seg = now_seg;
400 }
401
402 /* Explicitly unaligned cons. */
403 static void
404 s_pru_ucons (int nbytes)
405 {
406 int hold;
407 hold = pru_auto_align_on;
408 pru_auto_align_on = 0;
409 cons (nbytes);
410 pru_auto_align_on = hold;
411 }
412
413 /* .set sets assembler options. */
414 static void
415 s_pru_set (int equiv)
416 {
417 char *save = input_line_pointer;
418 char *directive;
419 char delim = get_symbol_name (&directive);
420 char *endline = input_line_pointer;
421
422 (void) restore_line_pointer (delim);
423
424 /* We only want to handle ".set XXX" if the
425 user has tried ".set XXX, YYY" they are not
426 trying a directive. This prevents
427 us from polluting the name space. */
428 SKIP_WHITESPACE ();
429 if (is_end_of_line[(unsigned char) *input_line_pointer])
430 {
431 bfd_boolean done = TRUE;
432 *endline = 0;
433
434 if (!strcmp (directive, "no_warn_regname_label"))
435 pru_opt.warn_regname_label = FALSE;
436 else
437 done = FALSE;
438
439 if (done)
440 {
441 *endline = delim;
442 demand_empty_rest_of_line ();
443 return;
444 }
445 }
446
447 /* If we fall through to here, either we have ".set XXX, YYY"
448 or we have ".set XXX" where XXX is unknown or we have
449 a syntax error. */
450 input_line_pointer = save;
451 s_set (equiv);
452 }
453
454 /* Machine-dependent assembler directives.
455 Format of each entry is:
456 { "directive", handler_func, param } */
457 const pseudo_typeS md_pseudo_table[] = {
458 {"align", s_pru_align, 0},
459 {"text", s_pru_text, 0},
460 {"data", s_pru_data, 0},
461 {"section", s_pru_section, 0},
462 {"section.s", s_pru_section, 0},
463 {"sect", s_pru_section, 0},
464 {"sect.s", s_pru_section, 0},
465 /* .dword and .half are included for compatibility with MIPS. */
466 {"dword", cons, 8},
467 {"half", cons, 2},
468 /* PRU native word size is 4 bytes, so we override
469 the GAS default of 2. */
470 {"word", cons, 4},
471 /* Explicitly unaligned directives. */
472 {"2byte", s_pru_ucons, 2},
473 {"4byte", s_pru_ucons, 4},
474 {"8byte", s_pru_ucons, 8},
475 {"16byte", s_pru_ucons, 16},
476 {"set", s_pru_set, 0},
477 {NULL, NULL, 0}
478 };
479
480
481 int
483 md_estimate_size_before_relax (fragS *fragp ATTRIBUTE_UNUSED,
484 asection *seg ATTRIBUTE_UNUSED)
485 {
486 abort ();
487 return 0;
488 }
489
490 void
491 md_convert_frag (bfd *headers ATTRIBUTE_UNUSED, segT segment ATTRIBUTE_UNUSED,
492 fragS *fragp ATTRIBUTE_UNUSED)
493 {
494 abort ();
495 }
496
497
498 static bfd_boolean
500 relaxable_section (asection *sec)
501 {
502 return ((sec->flags & SEC_DEBUGGING) == 0
503 && (sec->flags & SEC_CODE) != 0
504 && (sec->flags & SEC_ALLOC) != 0);
505 }
506
507 /* Does whatever the xtensa port does. */
508 int
509 pru_validate_fix_sub (fixS *fix)
510 {
511 segT add_symbol_segment, sub_symbol_segment;
512
513 /* The difference of two symbols should be resolved by the assembler when
514 linkrelax is not set. If the linker may relax the section containing
515 the symbols, then an Xtensa DIFF relocation must be generated so that
516 the linker knows to adjust the difference value. */
517 if (!linkrelax || fix->fx_addsy == NULL)
518 return 0;
519
520 /* Make sure both symbols are in the same segment, and that segment is
521 "normal" and relaxable. If the segment is not "normal", then the
522 fix is not valid. If the segment is not "relaxable", then the fix
523 should have been handled earlier. */
524 add_symbol_segment = S_GET_SEGMENT (fix->fx_addsy);
525 if (! SEG_NORMAL (add_symbol_segment)
526 || ! relaxable_section (add_symbol_segment))
527 return 0;
528
529 sub_symbol_segment = S_GET_SEGMENT (fix->fx_subsy);
530 return (sub_symbol_segment == add_symbol_segment);
531 }
532
533 /* TC_FORCE_RELOCATION hook. */
534
535 /* If linkrelax is turned on, and the symbol to relocate
536 against is in a relaxable segment, don't compute the value -
537 generate a relocation instead. */
538 int
539 pru_force_relocation (fixS *fix)
540 {
541 if (linkrelax && fix->fx_addsy
542 && relaxable_section (S_GET_SEGMENT (fix->fx_addsy)))
543 return 1;
544
545 return generic_force_reloc (fix);
546 }
547
548
549
550 /** Fixups and overflow checking. */
552
553 /* Check a fixup for overflow. */
554 static bfd_reloc_status_type
555 pru_check_overflow (valueT fixup, reloc_howto_type *howto)
556 {
557 bfd_reloc_status_type ret;
558
559 ret = bfd_check_overflow (howto->complain_on_overflow,
560 howto->bitsize,
561 howto->rightshift,
562 bfd_get_reloc_size (howto) * 8,
563 fixup);
564
565 return ret;
566 }
567
568 /* Emit diagnostic for fixup overflow. */
569 static void
570 pru_diagnose_overflow (valueT fixup, reloc_howto_type *howto,
571 fixS *fixP, valueT value)
572 {
573 if (fixP->fx_r_type == BFD_RELOC_8
574 || fixP->fx_r_type == BFD_RELOC_16
575 || fixP->fx_r_type == BFD_RELOC_32)
576 /* These relocs are against data, not instructions. */
577 as_bad_where (fixP->fx_file, fixP->fx_line,
578 _("immediate value 0x%x truncated to 0x%x"),
579 (unsigned int) fixup,
580 (unsigned int) (~(~(valueT) 0 << howto->bitsize) & fixup));
581 else
582 {
583 /* What opcode is the instruction? This will determine
584 whether we check for overflow in immediate values
585 and what error message we get. */
586 const struct pru_opcode *opcode;
587 enum overflow_type overflow_msg_type;
588 unsigned int range_min;
589 unsigned int range_max;
590 unsigned int address;
591 gas_assert (fixP->fx_size == 4);
592 opcode = pru_find_opcode (value);
593 gas_assert (opcode);
594 overflow_msg_type = opcode->overflow_msg;
595 switch (overflow_msg_type)
596 {
597 case call_target_overflow:
598 range_min
599 = ((fixP->fx_frag->fr_address + fixP->fx_where) & 0xf0000000);
600 range_max = range_min + 0x0fffffff;
601 address = fixup | range_min;
602
603 as_bad_where (fixP->fx_file, fixP->fx_line,
604 _("call target address 0x%08x out of range 0x%08x to 0x%08x"),
605 address, range_min, range_max);
606 break;
607 case qbranch_target_overflow:
608 as_bad_where (fixP->fx_file, fixP->fx_line,
609 _("quick branch offset %d out of range %d to %d"),
610 (int)fixup, -((1<<9) * 4), (1 << 9) * 4);
611 break;
612 case address_offset_overflow:
613 as_bad_where (fixP->fx_file, fixP->fx_line,
614 _("%s offset %d out of range %d to %d"),
615 opcode->name, (int)fixup, -32768, 32767);
616 break;
617 case signed_immed16_overflow:
618 as_bad_where (fixP->fx_file, fixP->fx_line,
619 _("immediate value %d out of range %d to %d"),
620 (int)fixup, -32768, 32767);
621 break;
622 case unsigned_immed32_overflow:
623 as_bad_where (fixP->fx_file, fixP->fx_line,
624 _("immediate value %llu out of range %u to %lu"),
625 (unsigned long long)fixup, 0, 0xfffffffflu);
626 break;
627 case unsigned_immed16_overflow:
628 as_bad_where (fixP->fx_file, fixP->fx_line,
629 _("immediate value %u out of range %u to %u"),
630 (unsigned int)fixup, 0, 65535);
631 break;
632 case unsigned_immed5_overflow:
633 as_bad_where (fixP->fx_file, fixP->fx_line,
634 _("immediate value %u out of range %u to %u"),
635 (unsigned int)fixup, 0, 31);
636 break;
637 default:
638 as_bad_where (fixP->fx_file, fixP->fx_line,
639 _("overflow in immediate argument"));
640 break;
641 }
642 }
643 }
644
645 /* Apply a fixup to the object file. */
646 void
647 md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
648 {
649 unsigned char *where;
650 valueT value = *valP;
651 long n;
652
653 /* Assert that the fixup is one we can handle. */
654 gas_assert (fixP != NULL && valP != NULL
655 && (fixP->fx_r_type == BFD_RELOC_8
656 || fixP->fx_r_type == BFD_RELOC_16
657 || fixP->fx_r_type == BFD_RELOC_32
658 || fixP->fx_r_type == BFD_RELOC_64
659 || fixP->fx_r_type == BFD_RELOC_PRU_LDI32
660 || fixP->fx_r_type == BFD_RELOC_PRU_U16
661 || fixP->fx_r_type == BFD_RELOC_PRU_U16_PMEMIMM
662 || fixP->fx_r_type == BFD_RELOC_PRU_S10_PCREL
663 || fixP->fx_r_type == BFD_RELOC_PRU_U8_PCREL
664 || fixP->fx_r_type == BFD_RELOC_PRU_32_PMEM
665 || fixP->fx_r_type == BFD_RELOC_PRU_16_PMEM
666 /* Add other relocs here as we generate them. */
667 ));
668
669 if (fixP->fx_r_type == BFD_RELOC_64)
670 {
671 /* We may reach here due to .8byte directives, but we never output
672 BFD_RELOC_64; it must be resolved. */
673 if (fixP->fx_addsy != NULL)
674 as_bad_where (fixP->fx_file, fixP->fx_line,
675 _("cannot create 64-bit relocation"));
676 else
677 {
678 md_number_to_chars (fixP->fx_frag->fr_literal + fixP->fx_where,
679 *valP, 8);
680 fixP->fx_done = 1;
681 }
682 return;
683 }
684
685 /* gas_assert (had_errors () || !fixP->fx_subsy); */
686
687 /* In general, fix instructions with immediate
688 constants. But leave LDI32 for the linker,
689 which is prepared to shorten insns. */
690 if (fixP->fx_addsy == (symbolS *) NULL
691 && fixP->fx_r_type != BFD_RELOC_PRU_LDI32)
692 fixP->fx_done = 1;
693
694 else if (fixP->fx_pcrel)
695 {
696 segT s = S_GET_SEGMENT (fixP->fx_addsy);
697
698 if (s == seg || s == absolute_section)
699 {
700 /* Blindly copied from AVR, but I don't understand why
701 this is needed in the first place. Fail hard to catch
702 when this curious code snippet is utilized. */
703 as_bad_where (fixP->fx_file, fixP->fx_line,
704 _("unexpected PC relative expression"));
705 value += S_GET_VALUE (fixP->fx_addsy);
706 fixP->fx_done = 1;
707 }
708 }
709 else if (linkrelax && fixP->fx_subsy)
710 {
711 /* For a subtraction relocation expression, generate one
712 of the DIFF relocs, with the value being the difference.
713 Note that a sym1 - sym2 expression is adjusted into a
714 section_start_sym + sym4_offset_from_section_start - sym1
715 expression. fixP->fx_addsy holds the section start symbol,
716 fixP->fx_offset holds sym2's offset, and fixP->fx_subsy
717 holds sym1. Calculate the current difference and write value,
718 but leave fx_offset as is - during relaxation,
719 fx_offset - value gives sym1's value. */
720
721 offsetT diffval; /* valueT is unsigned, so use offsetT. */
722
723 diffval = S_GET_VALUE (fixP->fx_addsy)
724 + fixP->fx_offset - S_GET_VALUE (fixP->fx_subsy);
725
726 switch (fixP->fx_r_type)
727 {
728 case BFD_RELOC_8:
729 fixP->fx_r_type = BFD_RELOC_PRU_GNU_DIFF8;
730 break;
731 case BFD_RELOC_16:
732 fixP->fx_r_type = BFD_RELOC_PRU_GNU_DIFF16;
733 break;
734 case BFD_RELOC_32:
735 fixP->fx_r_type = BFD_RELOC_PRU_GNU_DIFF32;
736 break;
737 case BFD_RELOC_PRU_16_PMEM:
738 fixP->fx_r_type = BFD_RELOC_PRU_GNU_DIFF16_PMEM;
739 if (diffval % 4)
740 as_bad_where (fixP->fx_file, fixP->fx_line,
741 _("residual low bits in pmem diff relocation"));
742 diffval /= 4;
743 break;
744 case BFD_RELOC_PRU_32_PMEM:
745 fixP->fx_r_type = BFD_RELOC_PRU_GNU_DIFF32_PMEM;
746 if (diffval % 4)
747 as_bad_where (fixP->fx_file, fixP->fx_line,
748 _("residual low bits in pmem diff relocation"));
749 diffval /= 4;
750 break;
751 default:
752 as_bad_where (fixP->fx_file, fixP->fx_line,
753 _("expression too complex"));
754 break;
755 }
756
757 value = *valP = diffval;
758
759 fixP->fx_subsy = NULL;
760 }
761 /* We don't actually support subtracting a symbol. */
762 if (fixP->fx_subsy != (symbolS *) NULL)
763 as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
764
765 /* For the DIFF relocs, write the value into the object file while still
766 keeping fx_done FALSE, as both the difference (recorded in the object file)
767 and the sym offset (part of fixP) are needed at link relax time. */
768 where = (unsigned char *) fixP->fx_frag->fr_literal + fixP->fx_where;
769 switch (fixP->fx_r_type)
770 {
771 case BFD_RELOC_PRU_GNU_DIFF8:
772 *where = value;
773 break;
774 case BFD_RELOC_PRU_GNU_DIFF16:
775 case BFD_RELOC_PRU_GNU_DIFF16_PMEM:
776 bfd_putl16 ((bfd_vma) value, where);
777 break;
778 case BFD_RELOC_PRU_GNU_DIFF32:
779 case BFD_RELOC_PRU_GNU_DIFF32_PMEM:
780 bfd_putl32 ((bfd_vma) value, where);
781 break;
782 default:
783 break;
784 }
785
786 if (fixP->fx_done)
787 /* Fully resolved fixup. */
788 {
789 reloc_howto_type *howto
790 = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
791
792 if (howto == NULL)
793 as_bad_where (fixP->fx_file, fixP->fx_line,
794 _("relocation is not supported"));
795 else
796 {
797 valueT fixup = value;
798 uint64_t insn;
799 char *buf;
800
801 /* Get the instruction or data to be fixed up. */
802 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
803 insn = md_chars_to_number (buf, fixP->fx_size);
804
805 /* Check for overflow, emitting a diagnostic if necessary. */
806 if (pru_check_overflow (fixup, howto) != bfd_reloc_ok)
807 pru_diagnose_overflow (fixup, howto, fixP, insn);
808
809 /* Apply the right shift. */
810 fixup = ((offsetT)fixup) >> howto->rightshift;
811
812 /* Truncate the fixup to right size. */
813 n = sizeof (fixup) * 8 - howto->bitsize;
814 fixup = (fixup << n) >> n;
815
816 /* Fix up the instruction. Non-contiguous bitfields need
817 special handling. */
818 if (fixP->fx_r_type == BFD_RELOC_PRU_LDI32)
819 {
820 /* As the only 64-bit "insn", LDI32 needs special handling. */
821 uint32_t insn1 = insn & 0xffffffff;
822 uint32_t insn2 = insn >> 32;
823 SET_INSN_FIELD (IMM16, insn1, fixup >> 16);
824 SET_INSN_FIELD (IMM16, insn2, fixup & 0xffff);
825
826 SET_INSN_FIELD (RDSEL, insn1, RSEL_31_16);
827 SET_INSN_FIELD (RDSEL, insn2, RSEL_15_0);
828
829 md_number_to_chars (buf, insn1, 4);
830 md_number_to_chars (buf + 4, insn2, 4);
831 }
832 else
833 {
834 if (fixP->fx_r_type == BFD_RELOC_PRU_S10_PCREL)
835 SET_BROFF_URAW (insn, fixup);
836 else
837 insn = (insn & ~howto->dst_mask) | (fixup << howto->bitpos);
838 md_number_to_chars (buf, insn, fixP->fx_size);
839 }
840 }
841
842 fixP->fx_done = 1;
843 }
844
845 if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT)
846 {
847 fixP->fx_done = 0;
848 if (fixP->fx_addsy
849 && !S_IS_DEFINED (fixP->fx_addsy) && !S_IS_WEAK (fixP->fx_addsy))
850 S_SET_WEAK (fixP->fx_addsy);
851 }
852 else if (fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
853 fixP->fx_done = 0;
854 }
855
856
857
858 /** Instruction parsing support. */
860
861 /* Creates a new pru_insn_relocS and returns a pointer to it. */
862 static pru_insn_relocS *
863 pru_insn_reloc_new (bfd_reloc_code_real_type reloc_type, unsigned int pcrel)
864 {
865 pru_insn_relocS *retval;
866 retval = XNEW (pru_insn_relocS);
867 if (retval == NULL)
868 {
869 as_bad (_("can't create relocation"));
870 abort ();
871 }
872
873 /* Fill out the fields with default values. */
874 retval->reloc_next = NULL;
875 retval->reloc_type = reloc_type;
876 retval->reloc_pcrel = pcrel;
877 return retval;
878 }
879
880 /* Frees up memory previously allocated by pru_insn_reloc_new (). */
881 static void
882 pru_insn_reloc_destroy (pru_insn_relocS *reloc)
883 {
884 pru_insn_relocS *next;
885
886 while (reloc)
887 {
888 next = reloc->reloc_next;
889 free (reloc);
890 reloc = next;
891 }
892 }
893
894 /* The various pru_assemble_* functions call this
895 function to generate an expression from a string representing an expression.
896 It then tries to evaluate the expression, and if it can, returns its value.
897 If not, it creates a new pru_insn_relocS and stores the expression and
898 reloc_type for future use. */
899 static unsigned long
900 pru_assemble_expression (const char *exprstr,
901 pru_insn_infoS *insn,
902 pru_insn_relocS *prev_reloc,
903 bfd_reloc_code_real_type reloc_type,
904 unsigned int pcrel)
905 {
906 expressionS *ep;
907 pru_insn_relocS *reloc;
908 char *saved_line_ptr;
909 unsigned short value;
910
911 gas_assert (exprstr != NULL);
912 gas_assert (insn != NULL);
913
914 /* We use this blank keyword to distinguish register from
915 label operands. */
916 if (strstr (exprstr, "%label") != NULL)
917 {
918 exprstr += strlen ("%label") + 1;
919 }
920
921 /* Check for pmem relocation operator.
922 Change the relocation type and advance the ptr to the start of
923 the expression proper. */
924 if (strstr (exprstr, "%pmem") != NULL)
925 {
926 reloc_type = BFD_RELOC_PRU_U16_PMEMIMM;
927 exprstr += strlen ("%pmem") + 1;
928 }
929
930 /* We potentially have a relocation. */
931 reloc = pru_insn_reloc_new (reloc_type, pcrel);
932 if (prev_reloc != NULL)
933 prev_reloc->reloc_next = reloc;
934 else
935 insn->insn_reloc = reloc;
936
937 /* Parse the expression string. */
938 ep = &reloc->reloc_expression;
939 saved_line_ptr = input_line_pointer;
940 input_line_pointer = (char *) exprstr;
941 SKIP_WHITESPACE ();
942 expression (ep);
943 SKIP_WHITESPACE ();
944 if (*input_line_pointer)
945 as_bad (_("trailing garbage after expression: %s"), input_line_pointer);
946 input_line_pointer = saved_line_ptr;
947
948
949 if (ep->X_op == O_illegal || ep->X_op == O_absent)
950 as_bad (_("expected expression, got %s"), exprstr);
951
952 /* This is redundant as the fixup will put this into
953 the instruction, but it is included here so that
954 self-test mode (-r) works. */
955 value = 0;
956 if (pru_mode == PRU_MODE_TEST && ep->X_op == O_constant)
957 value = ep->X_add_number;
958
959 return (unsigned long) value;
960 }
961
962 /* Try to parse a non-relocatable expression. */
963 static unsigned long
964 pru_assemble_noreloc_expression (const char *exprstr)
965 {
966 expressionS exp;
967 char *saved_line_ptr;
968 unsigned long val;
969
970 gas_assert (exprstr != NULL);
971
972 saved_line_ptr = input_line_pointer;
973 input_line_pointer = (char *) exprstr;
974 SKIP_WHITESPACE ();
975 expression (&exp);
976 SKIP_WHITESPACE ();
977 if (*input_line_pointer)
978 as_bad (_("trailing garbage after expression: %s"), input_line_pointer);
979 input_line_pointer = saved_line_ptr;
980
981 val = 0;
982 if (exp.X_op != O_constant)
983 as_bad (_("expected constant expression, got %s"), exprstr);
984 else
985 val = exp.X_add_number;
986
987 return val;
988 }
989
990 /* Argument assemble functions.
991 All take an instruction argument string, and a pointer
992 to an instruction opcode. Upon return the insn_opcode
993 has the relevant fields filled in to represent the arg
994 string. The return value is NULL if successful, or
995 an error message if an error was detected. */
996
997 static void
998 pru_assemble_arg_d (pru_insn_infoS *insn_info, const char *argstr)
999 {
1000 struct pru_reg *dst = pru_reg_lookup (argstr);
1001
1002 if (dst == NULL)
1003 as_bad (_("unknown register %s"), argstr);
1004 else
1005 {
1006 SET_INSN_FIELD (RD, insn_info->insn_code, dst->index);
1007 SET_INSN_FIELD (RDSEL, insn_info->insn_code, dst->regsel);
1008 }
1009 }
1010
1011 static void
1012 pru_assemble_arg_D (pru_insn_infoS *insn_info, const char *argstr)
1013 {
1014 struct pru_reg *dst;
1015
1016 /* The leading & before an address register is optional. */
1017 if (*argstr == '&')
1018 argstr++;
1019
1020 dst = pru_reg_lookup (argstr);
1021
1022 if (dst == NULL)
1023 as_bad (_("unknown register %s"), argstr);
1024 else
1025 {
1026 unsigned long rxb = 0;
1027
1028 switch (dst->regsel)
1029 {
1030 case RSEL_31_0: rxb = 0; break; /* whole register defaults to .b0 */
1031 case RSEL_7_0: rxb = 0; break;
1032 case RSEL_15_8: rxb = 1; break;
1033 case RSEL_23_16: rxb = 2; break;
1034 case RSEL_31_24: rxb = 3; break;
1035 default:
1036 as_bad (_("data transfer register cannot be halfword"));
1037 }
1038
1039 SET_INSN_FIELD (RD, insn_info->insn_code, dst->index);
1040 SET_INSN_FIELD (RDB, insn_info->insn_code, rxb);
1041 }
1042 }
1043
1044 static void
1045 pru_assemble_arg_R (pru_insn_infoS *insn_info, const char *argstr)
1046 {
1047 struct pru_reg *dst = pru_reg_lookup (argstr);
1048
1049 if (dst == NULL)
1050 as_bad (_("unknown register %s"), argstr);
1051 else
1052 {
1053 if (dst->regsel != RSEL_31_0)
1054 {
1055 as_bad (_("destination register must be full-word"));
1056 }
1057
1058 SET_INSN_FIELD (RD, insn_info->insn_code, dst->index);
1059 SET_INSN_FIELD (RDSEL, insn_info->insn_code, dst->regsel);
1060 }
1061 }
1062
1063 static void
1064 pru_assemble_arg_s (pru_insn_infoS *insn_info, const char *argstr)
1065 {
1066 struct pru_reg *src1 = pru_reg_lookup (argstr);
1067
1068 if (src1 == NULL)
1069 as_bad (_("unknown register %s"), argstr);
1070 else
1071 {
1072 SET_INSN_FIELD (RS1, insn_info->insn_code, src1->index);
1073 SET_INSN_FIELD (RS1SEL, insn_info->insn_code, src1->regsel);
1074 }
1075 }
1076
1077 static void
1078 pru_assemble_arg_S (pru_insn_infoS *insn_info, const char *argstr)
1079 {
1080 struct pru_reg *src1 = pru_reg_lookup (argstr);
1081
1082 if (src1 == NULL)
1083 as_bad (_("unknown register %s"), argstr);
1084 else
1085 {
1086 if (src1->regsel != RSEL_31_0)
1087 as_bad (_("cannot use partial register %s for addressing"), argstr);
1088 SET_INSN_FIELD (RS1, insn_info->insn_code, src1->index);
1089 }
1090 }
1091
1092 static void
1093 pru_assemble_arg_b (pru_insn_infoS *insn_info, const char *argstr)
1094 {
1095 struct pru_reg *src2 = pru_reg_lookup (argstr);
1096 if (src2 == NULL)
1097 {
1098 unsigned long imm8 = pru_assemble_noreloc_expression (argstr);
1099 if (imm8 >= 0x100)
1100 as_bad (_("value %lu is too large for a byte operand"), imm8);
1101 SET_INSN_FIELD (IMM8, insn_info->insn_code, imm8);
1102 SET_INSN_FIELD (IO, insn_info->insn_code, 1);
1103 }
1104 else
1105 {
1106 SET_INSN_FIELD (IO, insn_info->insn_code, 0);
1107 SET_INSN_FIELD (RS2, insn_info->insn_code, src2->index);
1108 SET_INSN_FIELD (RS2SEL, insn_info->insn_code, src2->regsel);
1109 }
1110
1111 }
1112
1113 static void
1114 pru_assemble_arg_B (pru_insn_infoS *insn_info, const char *argstr)
1115 {
1116 struct pru_reg *src2 = pru_reg_lookup (argstr);
1117 if (src2 == NULL)
1118 {
1119 unsigned long imm8;
1120 imm8 = pru_assemble_noreloc_expression (argstr);
1121 if (!imm8 || imm8 > 0xff)
1122 as_bad (_("loop count constant %ld is out of range [1..%d]"),
1123 imm8, 0xff);
1124 /* Note: HW expects the immediate loop count field
1125 to be one less than the actual loop count. */
1126 SET_INSN_FIELD (IMM8, insn_info->insn_code, imm8 - 1);
1127 SET_INSN_FIELD (IO, insn_info->insn_code, 1);
1128 }
1129 else
1130 {
1131 SET_INSN_FIELD (IO, insn_info->insn_code, 0);
1132 SET_INSN_FIELD (RS2, insn_info->insn_code, src2->index);
1133 SET_INSN_FIELD (RS2SEL, insn_info->insn_code, src2->regsel);
1134 }
1135 }
1136
1137 static void
1138 pru_assemble_arg_i (pru_insn_infoS *insn_info, const char *argstr)
1139 {
1140 unsigned long imm32;
1141
1142 /* We must not generate PRU_LDI32 relocation if relaxation is disabled in
1143 GAS. Consider the following scenario: GAS relaxation is disabled, so
1144 DIFF* expressions are fixed and not emitted as relocations. Then if LD
1145 has relaxation enabled, it may shorten LDI32 but will not update
1146 accordingly the DIFF expressions. */
1147 if (pru_opt.link_relax)
1148 imm32 = pru_assemble_expression (argstr, insn_info,
1149 insn_info->insn_reloc,
1150 BFD_RELOC_PRU_LDI32, 0);
1151 else
1152 imm32 = pru_assemble_noreloc_expression (argstr);
1153
1154 /* QUIRK: LDI must clear IO bit high, even though it has immediate arg. */
1155 SET_INSN_FIELD (IO, insn_info->insn_code, 0);
1156 SET_INSN_FIELD (RDSEL, insn_info->insn_code, RSEL_31_16);
1157 SET_INSN_FIELD (IMM16, insn_info->insn_code, imm32 >> 16);
1158 insn_info->ldi32_imm32 = imm32;
1159 }
1160
1161 static void
1162 pru_assemble_arg_j (pru_insn_infoS *insn_info, const char *argstr)
1163 {
1164 struct pru_reg *src2 = pru_reg_lookup (argstr);
1165
1166 if (src2 == NULL)
1167 {
1168 unsigned long imm16 = pru_assemble_expression (argstr, insn_info,
1169 insn_info->insn_reloc,
1170 BFD_RELOC_PRU_U16_PMEMIMM,
1171 0);
1172 SET_INSN_FIELD (IMM16, insn_info->insn_code, imm16);
1173 SET_INSN_FIELD (IO, insn_info->insn_code, 1);
1174 }
1175 else
1176 {
1177 SET_INSN_FIELD (IO, insn_info->insn_code, 0);
1178 SET_INSN_FIELD (RS2, insn_info->insn_code, src2->index);
1179 SET_INSN_FIELD (RS2SEL, insn_info->insn_code, src2->regsel);
1180 }
1181 }
1182
1183 static void
1184 pru_assemble_arg_W (pru_insn_infoS *insn_info, const char *argstr)
1185 {
1186 unsigned long imm16 = pru_assemble_expression (argstr, insn_info,
1187 insn_info->insn_reloc,
1188 BFD_RELOC_PRU_U16, 0);
1189 /* QUIRK: LDI must clear IO bit high, even though it has immediate arg. */
1190 SET_INSN_FIELD (IO, insn_info->insn_code, 0);
1191 SET_INSN_FIELD (IMM16, insn_info->insn_code, imm16);
1192 }
1193
1194 static void
1195 pru_assemble_arg_o (pru_insn_infoS *insn_info, const char *argstr)
1196 {
1197 unsigned long imm10 = pru_assemble_expression (argstr, insn_info,
1198 insn_info->insn_reloc,
1199 BFD_RELOC_PRU_S10_PCREL, 1);
1200 SET_BROFF_URAW (insn_info->insn_code, imm10);
1201 }
1202
1203 static void
1204 pru_assemble_arg_O (pru_insn_infoS *insn_info, const char *argstr)
1205 {
1206 unsigned long imm8 = pru_assemble_expression (argstr, insn_info,
1207 insn_info->insn_reloc,
1208 BFD_RELOC_PRU_U8_PCREL, 1);
1209 SET_INSN_FIELD (LOOP_JMPOFFS, insn_info->insn_code, imm8);
1210 }
1211
1212 static void
1213 pru_assemble_arg_l (pru_insn_infoS *insn_info, const char *argstr)
1214 {
1215 unsigned long burstlen = 0;
1216 struct pru_reg *blreg = pru_reg_lookup (argstr);
1217
1218 if (blreg == NULL)
1219 {
1220 burstlen = pru_assemble_noreloc_expression (argstr);
1221 if (!burstlen || burstlen > LSSBBO_BYTECOUNT_R0_BITS7_0)
1222 as_bad (_("byte count constant %ld is out of range [1..%d]"),
1223 burstlen, LSSBBO_BYTECOUNT_R0_BITS7_0);
1224 burstlen--;
1225 }
1226 else
1227 {
1228 if (blreg->index != 0)
1229 as_bad (_("only r0 can be used as byte count register"));
1230 else if (blreg->regsel > RSEL_31_24)
1231 as_bad (_("only r0.bX byte fields of r0 can be used as byte count"));
1232 else
1233 burstlen = LSSBBO_BYTECOUNT_R0_BITS7_0 + blreg->regsel;
1234 }
1235 SET_BURSTLEN (insn_info->insn_code, burstlen);
1236 }
1237
1238 static void
1239 pru_assemble_arg_n (pru_insn_infoS *insn_info, const char *argstr)
1240 {
1241 unsigned long burstlen = 0;
1242 struct pru_reg *blreg = pru_reg_lookup (argstr);
1243
1244 if (blreg == NULL)
1245 {
1246 burstlen = pru_assemble_noreloc_expression (argstr);
1247 if (!burstlen || burstlen > LSSBBO_BYTECOUNT_R0_BITS7_0)
1248 as_bad (_("byte count constant %ld is out of range [1..%d]"),
1249 burstlen, LSSBBO_BYTECOUNT_R0_BITS7_0);
1250 burstlen--;
1251 }
1252 else
1253 {
1254 if (blreg->index != 0)
1255 as_bad (_("only r0 can be used as byte count register"));
1256 else if (blreg->regsel > RSEL_31_24)
1257 as_bad (_("only r0.bX byte fields of r0 can be used as byte count"));
1258 else
1259 burstlen = LSSBBO_BYTECOUNT_R0_BITS7_0 + blreg->regsel;
1260 }
1261 SET_INSN_FIELD (XFR_LENGTH, insn_info->insn_code, burstlen);
1262 }
1263
1264 static void
1265 pru_assemble_arg_c (pru_insn_infoS *insn_info, const char *argstr)
1266 {
1267 unsigned long cb = pru_assemble_noreloc_expression (argstr);
1268
1269 if (cb > 31)
1270 as_bad (_("invalid constant table offset %ld"), cb);
1271 else
1272 SET_INSN_FIELD (CB, insn_info->insn_code, cb);
1273 }
1274
1275 static void
1276 pru_assemble_arg_w (pru_insn_infoS *insn_info, const char *argstr)
1277 {
1278 unsigned long wk = pru_assemble_noreloc_expression (argstr);
1279
1280 if (wk != 0 && wk != 1)
1281 as_bad (_("invalid WakeOnStatus %ld"), wk);
1282 else
1283 SET_INSN_FIELD (WAKEONSTATUS, insn_info->insn_code, wk);
1284 }
1285
1286 static void
1287 pru_assemble_arg_x (pru_insn_infoS *insn_info, const char *argstr)
1288 {
1289 unsigned long wba = pru_assemble_noreloc_expression (argstr);
1290
1291 if (wba > 255)
1292 as_bad (_("invalid XFR WideBus Address %ld"), wba);
1293 else
1294 SET_INSN_FIELD (XFR_WBA, insn_info->insn_code, wba);
1295 }
1296
1297 /* The function consume_arg takes a pointer into a string
1298 of instruction tokens (args) and a pointer into a string
1299 representing the expected sequence of tokens and separators.
1300 It checks whether the first argument in argstr is of the
1301 expected type, throwing an error if it is not, and returns
1302 the pointer argstr. */
1303 static char *
1304 pru_consume_arg (char *argstr, const char *parsestr)
1305 {
1306 char *temp;
1307
1308 switch (*parsestr)
1309 {
1310 case 'W':
1311 if (*argstr == '%')
1312 {
1313 if (strprefix (argstr, "%pmem") || strprefix (argstr, "%label"))
1314 {
1315 /* We zap the parentheses because we don't want them confused
1316 with separators. */
1317 temp = strchr (argstr, '(');
1318 if (temp != NULL)
1319 *temp = ' ';
1320 temp = strchr (argstr, ')');
1321 if (temp != NULL)
1322 *temp = ' ';
1323 }
1324 else
1325 as_bad (_("badly formed expression near %s"), argstr);
1326 }
1327 break;
1328
1329 case 'j':
1330 case 'o':
1331 case 'O':
1332 if (*argstr == '%')
1333 {
1334 /* Only 'j' really requires %label for distinguishing registers
1335 from labels, but we include 'o' and 'O' here to avoid
1336 confusing assembler programmers. Thus for completeness all
1337 jump operands can be prefixed with %label. */
1338 if (strprefix (argstr, "%label"))
1339 {
1340 /* We zap the parentheses because we don't want them confused
1341 with separators. */
1342 temp = strchr (argstr, '(');
1343 if (temp != NULL)
1344 *temp = ' ';
1345 temp = strchr (argstr, ')');
1346 if (temp != NULL)
1347 *temp = ' ';
1348 }
1349 else
1350 as_bad (_("badly formed expression near %s"), argstr);
1351 }
1352 break;
1353
1354 case 'b':
1355 case 'B':
1356 case 'c':
1357 case 'd':
1358 case 'D':
1359 case 'E':
1360 case 'i':
1361 case 's':
1362 case 'S':
1363 case 'l':
1364 case 'n':
1365 case 'R':
1366 case 'w':
1367 case 'x':
1368 /* We can't have %pmem here. */
1369 if (*argstr == '%')
1370 as_bad (_("badly formed expression near %s"), argstr);
1371 break;
1372 default:
1373 BAD_CASE (*parsestr);
1374 break;
1375 }
1376
1377 return argstr;
1378 }
1379
1380 /* The function consume_separator takes a pointer into a string
1381 of instruction tokens (args) and a pointer into a string representing
1382 the expected sequence of tokens and separators. It finds the first
1383 instance of the character pointed to by separator in argstr, and
1384 returns a pointer to the next element of argstr, which is the
1385 following token in the sequence. */
1386 static char *
1387 pru_consume_separator (char *argstr, const char *separator)
1388 {
1389 char *p;
1390
1391 p = strchr (argstr, *separator);
1392
1393 if (p != NULL)
1394 *p++ = 0;
1395 else
1396 as_bad (_("expecting %c near %s"), *separator, argstr);
1397 return p;
1398 }
1399
1400
1401 /* The principal argument parsing function which takes a string argstr
1402 representing the instruction arguments for insn, and extracts the argument
1403 tokens matching parsestr into parsed_args. */
1404 static void
1405 pru_parse_args (pru_insn_infoS *insn ATTRIBUTE_UNUSED, char *argstr,
1406 const char *parsestr, char **parsed_args)
1407 {
1408 char *p;
1409 char *end = NULL;
1410 int i;
1411 p = argstr;
1412 i = 0;
1413 bfd_boolean terminate = FALSE;
1414
1415 /* This rest of this function is it too fragile and it mostly works,
1416 therefore special case this one. */
1417 if (*parsestr == 0 && argstr != 0)
1418 {
1419 as_bad (_("too many arguments"));
1420 parsed_args[0] = NULL;
1421 return;
1422 }
1423
1424 while (p != NULL && !terminate && i < PRU_MAX_INSN_TOKENS)
1425 {
1426 parsed_args[i] = pru_consume_arg (p, parsestr);
1427 ++parsestr;
1428 if (*parsestr != '\0')
1429 {
1430 p = pru_consume_separator (p, parsestr);
1431 ++parsestr;
1432 }
1433 else
1434 {
1435 /* Check that the argument string has no trailing arguments. */
1436 /* If we've got a %pmem relocation, we've zapped the parens with
1437 spaces. */
1438 if (strprefix (p, "%pmem") || strprefix (p, "%label"))
1439 end = strpbrk (p, ",");
1440 else
1441 end = strpbrk (p, " ,");
1442
1443 if (end != NULL)
1444 as_bad (_("too many arguments"));
1445 }
1446
1447 if (*parsestr == '\0' || (p != NULL && *p == '\0'))
1448 terminate = TRUE;
1449 ++i;
1450 }
1451
1452 parsed_args[i] = NULL;
1453
1454 /* There are no instructions with optional arguments; complain. */
1455 if (*parsestr != '\0')
1456 as_bad (_("missing argument"));
1457 }
1458
1459
1460 /** Assembler output support. */
1462
1463 /* Output a normal instruction. */
1464 static void
1465 output_insn (pru_insn_infoS *insn)
1466 {
1467 char *f;
1468 pru_insn_relocS *reloc;
1469
1470 f = frag_more (4);
1471 /* This allocates enough space for the instruction
1472 and puts it in the current frag. */
1473 md_number_to_chars (f, insn->insn_code, 4);
1474 /* Emit debug info. */
1475 dwarf2_emit_insn (4);
1476 /* Create any fixups to be acted on later. */
1477 for (reloc = insn->insn_reloc; reloc != NULL; reloc = reloc->reloc_next)
1478 fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1479 &reloc->reloc_expression, reloc->reloc_pcrel,
1480 reloc->reloc_type);
1481 }
1482
1483 /* Output two LDI instructions from LDI32 macro */
1484 static void
1485 output_insn_ldi32 (pru_insn_infoS *insn)
1486 {
1487 char *f;
1488 pru_insn_relocS *reloc;
1489 unsigned long insn2;
1490
1491 f = frag_more (8);
1492 SET_INSN_FIELD (IMM16, insn->insn_code, insn->ldi32_imm32 >> 16);
1493 SET_INSN_FIELD (RDSEL, insn->insn_code, RSEL_31_16);
1494 md_number_to_chars (f, insn->insn_code, 4);
1495
1496 insn2 = insn->insn_code;
1497 SET_INSN_FIELD (IMM16, insn2, insn->ldi32_imm32 & 0xffff);
1498 SET_INSN_FIELD (RDSEL, insn2, RSEL_15_0);
1499 md_number_to_chars (f + 4, insn2, 4);
1500
1501 /* Emit debug info. */
1502 dwarf2_emit_insn (8);
1503
1504 /* Create any fixups to be acted on later. */
1505 for (reloc = insn->insn_reloc; reloc != NULL; reloc = reloc->reloc_next)
1506 fix_new_exp (frag_now, f - frag_now->fr_literal, 4,
1507 &reloc->reloc_expression, reloc->reloc_pcrel,
1508 reloc->reloc_type);
1509 }
1510
1511
1512 /** External interfaces. */
1514
1515 /* The following functions are called by machine-independent parts of
1516 the assembler. */
1517 int
1518 md_parse_option (int c, const char *arg ATTRIBUTE_UNUSED)
1519 {
1520 switch (c)
1521 {
1522 case 'r':
1523 /* Hidden option for self-test mode. */
1524 pru_mode = PRU_MODE_TEST;
1525 break;
1526 case OPTION_LINK_RELAX:
1527 pru_opt.link_relax = TRUE;
1528 break;
1529 case OPTION_NO_LINK_RELAX:
1530 pru_opt.link_relax = FALSE;
1531 break;
1532 case OPTION_NO_WARN_REGNAME_LABEL:
1533 pru_opt.warn_regname_label = FALSE;
1534 break;
1535 default:
1536 return 0;
1537 break;
1538 }
1539
1540 return 1;
1541 }
1542
1543 const char *
1544 pru_target_format (void)
1545 {
1546 return "elf32-pru";
1547 }
1548
1549 /* Machine-dependent usage message. */
1550 void
1551 md_show_usage (FILE *stream)
1552 {
1553 fprintf (stream,
1554 _("PRU options:\n"
1555 " -mlink-relax generate relocations for linker relaxation (default).\n"
1556 " -mno-link-relax don't generate relocations for linker relaxation.\n"
1557 ));
1558
1559 }
1560
1561 /* This function is called once, at assembler startup time.
1562 It should set up all the tables, etc. that the MD part of the
1563 assembler will need. */
1564 void
1565 md_begin (void)
1566 {
1567 int i;
1568 const char *inserted;
1569
1570 /* Create and fill a hashtable for the PRU opcodes, registers and
1571 arguments. */
1572 pru_opcode_hash = hash_new ();
1573 pru_reg_hash = hash_new ();
1574
1575 for (i = 0; i < NUMOPCODES; ++i)
1576 {
1577 inserted
1578 = hash_insert (pru_opcode_hash, pru_opcodes[i].name,
1579 (PTR) & pru_opcodes[i]);
1580 if (inserted != NULL)
1581 {
1582 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1583 pru_opcodes[i].name, inserted);
1584 /* Probably a memory allocation problem? Give up now. */
1585 as_fatal (_("Broken assembler. No assembly attempted."));
1586 }
1587 }
1588
1589 for (i = 0; i < pru_num_regs; ++i)
1590 {
1591 inserted
1592 = hash_insert (pru_reg_hash, pru_regs[i].name,
1593 (PTR) & pru_regs[i]);
1594 if (inserted != NULL)
1595 {
1596 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
1597 pru_regs[i].name, inserted);
1598 /* Probably a memory allocation problem? Give up now. */
1599 as_fatal (_("Broken assembler. No assembly attempted."));
1600 }
1601
1602 }
1603
1604 linkrelax = pru_opt.link_relax;
1605 /* Initialize the alignment data. */
1606 pru_current_align_seg = now_seg;
1607 pru_last_label = NULL;
1608 pru_current_align = 0;
1609 }
1610
1611
1612 /* Assembles a single line of PRU assembly language. */
1613 void
1614 md_assemble (char *op_str)
1615 {
1616 char *argstr;
1617 char *op_strdup = NULL;
1618 pru_insn_infoS thisinsn;
1619 pru_insn_infoS *insn = &thisinsn;
1620
1621 /* Make sure we are aligned on a 4-byte boundary. */
1622 if (pru_current_align < 2)
1623 pru_align (2, NULL, pru_last_label);
1624 else if (pru_current_align > 2)
1625 pru_current_align = 2;
1626 pru_last_label = NULL;
1627
1628 /* We don't want to clobber to op_str
1629 because we want to be able to use it in messages. */
1630 op_strdup = strdup (op_str);
1631 insn->insn_tokens[0] = strtok (op_strdup, " ");
1632 argstr = strtok (NULL, "");
1633
1634 /* Assemble the opcode. */
1635 insn->insn_pru_opcode = pru_opcode_lookup (insn->insn_tokens[0]);
1636 insn->insn_reloc = NULL;
1637
1638 if (insn->insn_pru_opcode != NULL)
1639 {
1640 const char *argsfmt = insn->insn_pru_opcode->args;
1641 const char **argtk = &insn->insn_tokens[1];
1642 const char *argp;
1643
1644 /* Set the opcode for the instruction. */
1645 insn->insn_code = insn->insn_pru_opcode->match;
1646
1647 if (pru_mode == PRU_MODE_TEST)
1648 {
1649 /* Add the "expected" instruction parameter used for validation. */
1650 argsfmt = malloc (strlen (argsfmt) + 3);
1651 sprintf ((char *)argsfmt, "%s,E", insn->insn_pru_opcode->args);
1652 }
1653 pru_parse_args (insn, argstr, argsfmt,
1654 (char **) &insn->insn_tokens[1]);
1655
1656 for (argp = argsfmt; !had_errors () && *argp && *argtk; ++argp)
1657 {
1658 gas_assert (argtk <= &insn->insn_tokens[PRU_MAX_INSN_TOKENS]);
1659
1660 switch (*argp)
1661 {
1662 case ',':
1663 continue;
1664
1665 case 'd':
1666 pru_assemble_arg_d (insn, *argtk++);
1667 continue;
1668 case 'D':
1669 pru_assemble_arg_D (insn, *argtk++);
1670 continue;
1671 case 'R':
1672 pru_assemble_arg_R (insn, *argtk++);
1673 continue;
1674 case 's':
1675 pru_assemble_arg_s (insn, *argtk++);
1676 continue;
1677 case 'S':
1678 pru_assemble_arg_S (insn, *argtk++);
1679 continue;
1680 case 'b':
1681 pru_assemble_arg_b (insn, *argtk++);
1682 continue;
1683 case 'B':
1684 pru_assemble_arg_B (insn, *argtk++);
1685 continue;
1686 case 'i':
1687 pru_assemble_arg_i (insn, *argtk++);
1688 continue;
1689 case 'j':
1690 pru_assemble_arg_j (insn, *argtk++);
1691 continue;
1692 case 'W':
1693 pru_assemble_arg_W (insn, *argtk++);
1694 continue;
1695 case 'o':
1696 pru_assemble_arg_o (insn, *argtk++);
1697 continue;
1698 case 'O':
1699 pru_assemble_arg_O (insn, *argtk++);
1700 continue;
1701 case 'l':
1702 pru_assemble_arg_l (insn, *argtk++);
1703 continue;
1704 case 'n':
1705 pru_assemble_arg_n (insn, *argtk++);
1706 continue;
1707 case 'c':
1708 pru_assemble_arg_c (insn, *argtk++);
1709 continue;
1710 case 'w':
1711 pru_assemble_arg_w (insn, *argtk++);
1712 continue;
1713 case 'x':
1714 pru_assemble_arg_x (insn, *argtk++);
1715 continue;
1716
1717 case 'E':
1718 pru_check_assembly (insn->insn_code, *argtk++);
1719 continue;
1720
1721 default:
1722 BAD_CASE (*argp);
1723 }
1724 }
1725
1726 if (*argp && !had_errors ())
1727 as_bad (_("missing argument"));
1728
1729 if (!had_errors ())
1730 {
1731 if (insn->insn_pru_opcode->pinfo & PRU_INSN_LDI32)
1732 {
1733 output_insn_ldi32 (insn);
1734 }
1735 else
1736 {
1737 output_insn (insn);
1738 }
1739 }
1740
1741 if (pru_mode == PRU_MODE_TEST)
1742 free ((char *)argsfmt);
1743 }
1744 else
1745 /* Unrecognised instruction - error. */
1746 as_bad (_("unrecognised instruction %s"), insn->insn_tokens[0]);
1747
1748 /* Don't leak memory. */
1749 pru_insn_reloc_destroy (insn->insn_reloc);
1750 free (op_strdup);
1751 }
1752
1753 /* Round up section size. */
1754 valueT
1755 md_section_align (asection *seg, valueT addr)
1756 {
1757 int align = bfd_get_section_alignment (stdoutput, seg);
1758 return ((addr + (1 << align) - 1) & (-((valueT) 1 << align)));
1759 }
1760
1761 /* Implement tc_fix_adjustable. */
1762 int
1763 pru_fix_adjustable (fixS *fixp)
1764 {
1765 if (fixp->fx_addsy == NULL)
1766 return 1;
1767
1768 /* Prevent all adjustments to global symbols. */
1769 if (OUTPUT_FLAVOR == bfd_target_elf_flavour
1770 && (S_IS_EXTERNAL (fixp->fx_addsy) || S_IS_WEAK (fixp->fx_addsy)))
1771 return 0;
1772
1773 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1774 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1775 return 0;
1776
1777 /* Preserve relocations against symbols with function type. */
1778 if (symbol_get_bfdsym (fixp->fx_addsy)->flags & BSF_FUNCTION)
1779 return 0;
1780
1781 return 1;
1782 }
1783
1784 /* The function tc_gen_reloc creates a relocation structure for the
1785 fixup fixp, and returns a pointer to it. This structure is passed
1786 to bfd_install_relocation so that it can be written to the object
1787 file for linking. */
1788 arelent *
1789 tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
1790 {
1791 arelent *reloc = XNEW (arelent);
1792 reloc->sym_ptr_ptr = XNEW (asymbol *);
1793 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1794
1795 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1796 reloc->addend = fixp->fx_offset; /* fixp->fx_addnumber; */
1797
1798 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1799 if (reloc->howto == NULL)
1800 {
1801 as_bad_where (fixp->fx_file, fixp->fx_line,
1802 _("can't represent relocation type %s"),
1803 bfd_get_reloc_code_name (fixp->fx_r_type));
1804
1805 /* Set howto to a garbage value so that we can keep going. */
1806 reloc->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
1807 gas_assert (reloc->howto != NULL);
1808 }
1809 return reloc;
1810 }
1811
1812 long
1813 md_pcrel_from (fixS *fixP ATTRIBUTE_UNUSED)
1814 {
1815 return fixP->fx_where + fixP->fx_frag->fr_address;
1816 }
1817
1818 /* Called just before the assembler exits. */
1819 void
1820 md_end (void)
1821 {
1822 hash_die (pru_opcode_hash);
1823 hash_die (pru_reg_hash);
1824 }
1825
1826 symbolS *
1827 md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
1828 {
1829 return NULL;
1830 }
1831
1832 /* Implement tc_frob_label. */
1833 void
1834 pru_frob_label (symbolS *lab)
1835 {
1836 /* Emit dwarf information. */
1837 dwarf2_emit_label (lab);
1838
1839 /* Update the label's address with the current output pointer. */
1840 symbol_set_frag (lab, frag_now);
1841 S_SET_VALUE (lab, (valueT) frag_now_fix ());
1842
1843 /* Record this label for future adjustment after we find out what
1844 kind of data it references, and the required alignment therewith. */
1845 pru_last_label = lab;
1846
1847 if (pru_opt.warn_regname_label && pru_reg_lookup (S_GET_NAME (lab)))
1848 as_warn (_("Label \"%s\" matches a CPU register name"), S_GET_NAME (lab));
1849 }
1850
1851 static inline char *
1852 skip_space (char *s)
1853 {
1854 while (*s == ' ' || *s == '\t')
1855 ++s;
1856 return s;
1857 }
1858
1859 /* Parse special CONS expression: pmem (expression). Idea from AVR.
1860
1861 Used to catch and mark code (program memory) in constant expression
1862 relocations. Return non-zero for program memory. */
1863
1864 int
1865 pru_parse_cons_expression (expressionS *exp, int nbytes)
1866 {
1867 int is_pmem = FALSE;
1868 char *tmp;
1869
1870 tmp = input_line_pointer = skip_space (input_line_pointer);
1871
1872 if (nbytes == 4 || nbytes == 2)
1873 {
1874 const char *pmem_str = "%pmem";
1875 int len = strlen (pmem_str);
1876
1877 if (strncasecmp (input_line_pointer, pmem_str, len) == 0)
1878 {
1879 input_line_pointer = skip_space (input_line_pointer + len);
1880
1881 if (*input_line_pointer == '(')
1882 {
1883 input_line_pointer = skip_space (input_line_pointer + 1);
1884 is_pmem = TRUE;
1885 expression (exp);
1886
1887 if (*input_line_pointer == ')')
1888 ++input_line_pointer;
1889 else
1890 {
1891 as_bad (_("`)' required"));
1892 is_pmem = FALSE;
1893 }
1894
1895 return is_pmem;
1896 }
1897
1898 input_line_pointer = tmp;
1899 }
1900 }
1901
1902 expression (exp);
1903
1904 return is_pmem;
1905 }
1906
1907 /* Implement TC_CONS_FIX_NEW. */
1908 void
1909 pru_cons_fix_new (fragS *frag, int where, unsigned int nbytes,
1910 expressionS *exp, const int is_pmem)
1911 {
1912 bfd_reloc_code_real_type r;
1913
1914 switch (nbytes | (!!is_pmem << 8))
1915 {
1916 case 1 | (0 << 8): r = BFD_RELOC_8; break;
1917 case 2 | (0 << 8): r = BFD_RELOC_16; break;
1918 case 4 | (0 << 8): r = BFD_RELOC_32; break;
1919 case 8 | (0 << 8): r = BFD_RELOC_64; break;
1920 case 2 | (1 << 8): r = BFD_RELOC_PRU_16_PMEM; break;
1921 case 4 | (1 << 8): r = BFD_RELOC_PRU_32_PMEM; break;
1922 default:
1923 as_bad (_("illegal %s relocation size: %d"),
1924 is_pmem ? "text" : "data", nbytes);
1925 return;
1926 }
1927
1928 fix_new_exp (frag, where, (int) nbytes, exp, 0, r);
1929 }
1930
1931 /* Implement tc_regname_to_dw2regnum, to convert REGNAME to a DWARF-2
1932 register number. */
1933 int
1934 pru_regname_to_dw2regnum (char *regname)
1935 {
1936 struct pru_reg *r = pru_reg_lookup (regname);
1937 if (r == NULL)
1938 return -1;
1939 return r->index;
1940 }
1941
1942 /* Implement tc_cfi_frame_initial_instructions, to initialize the DWARF-2
1943 unwind information for this procedure. */
1944 void
1945 pru_frame_initial_instructions (void)
1946 {
1947 const unsigned fp_regno = 4;
1948 cfi_add_CFA_def_cfa (fp_regno, 0);
1949 }
1950
1951 bfd_boolean
1952 pru_allow_local_subtract (expressionS * left,
1953 expressionS * right,
1954 segT section)
1955 {
1956 /* If we are not in relaxation mode, subtraction is OK. */
1957 if (!linkrelax)
1958 return TRUE;
1959
1960 /* If the symbols are not in a code section then they are OK. */
1961 if ((section->flags & SEC_CODE) == 0)
1962 return TRUE;
1963
1964 if (left->X_add_symbol == right->X_add_symbol)
1965 return TRUE;
1966
1967 /* We have to assume that there may be instructions between the
1968 two symbols and that relaxation may increase the distance between
1969 them. */
1970 return FALSE;
1971 }
1972