* config/tc-avr.c (AVR_ISA_???): moved to include/opcode/avr.h
[deliverable/binutils-gdb.git] / gas / config / tc-avr.c
1 /* tc-avr.c -- Assembler code for the ATMEL AVR
2
3 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Denis Chertykov <denisc@overta.ru>
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 2, 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
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23 #include <stdio.h>
24 #include <ctype.h>
25 #include "as.h"
26 #include "subsegs.h"
27
28 struct avr_opcodes_s
29 {
30 char *name;
31 char *constraints;
32 int insn_size; /* in words */
33 int isa;
34 unsigned int bin_opcode;
35 };
36
37 #define AVR_INSN(NAME, CONSTR, OPCODE, SIZE, ISA, BIN) \
38 {#NAME, CONSTR, SIZE, ISA, BIN},
39
40 struct avr_opcodes_s avr_opcodes[] =
41 {
42 #include "opcode/avr.h"
43 {NULL, NULL, 0, 0, 0}
44 };
45
46
47 const char comment_chars[] = ";";
48 const char line_comment_chars[] = "#";
49 const char line_separator_chars[] = "$";
50
51 const char *md_shortopts = "m:";
52 struct mcu_type_s
53 {
54 char *name;
55 int isa;
56 int mach;
57 };
58
59 static struct mcu_type_s mcu_types[] =
60 {
61 {"avr1", AVR_ISA_TINY1, bfd_mach_avr1},
62 {"avr2", AVR_ISA_85xx, bfd_mach_avr2},
63 {"avr3", AVR_ISA_M103, bfd_mach_avr3},
64 {"avr4", AVR_ISA_ALL, bfd_mach_avr4},
65 {"at90s1200", AVR_ISA_1200, bfd_mach_avr1},
66 {"attiny10", AVR_ISA_TINY1, bfd_mach_avr1},
67 {"attiny11", AVR_ISA_TINY1, bfd_mach_avr1},
68 {"attiny12", AVR_ISA_TINY1, bfd_mach_avr1},
69 {"attiny15", AVR_ISA_TINY1, bfd_mach_avr1},
70 {"attiny28", AVR_ISA_TINY1, bfd_mach_avr1},
71 {"at90s2313", AVR_ISA_2xxx, bfd_mach_avr2},
72 {"at90s2323", AVR_ISA_2xxx, bfd_mach_avr2},
73 {"at90s2333", AVR_ISA_2xxx, bfd_mach_avr2},
74 {"attiny22" , AVR_ISA_2xxx, bfd_mach_avr2},
75 {"at90s2343", AVR_ISA_2xxx, bfd_mach_avr2},
76 {"at90s4433", AVR_ISA_2xxx, bfd_mach_avr2},
77 {"at90s4414", AVR_ISA_2xxx, bfd_mach_avr2},
78 {"at90s4434", AVR_ISA_2xxx, bfd_mach_avr2},
79 {"at90s8515", AVR_ISA_85xx, bfd_mach_avr2},
80 {"at90s8535", AVR_ISA_85xx, bfd_mach_avr2},
81 {"at90c8534", AVR_ISA_85xx, bfd_mach_avr2},
82 {"atmega603", AVR_ISA_M603, bfd_mach_avr3},
83 {"atmega103", AVR_ISA_M103, bfd_mach_avr3},
84 {"atmega161", AVR_ISA_M161, bfd_mach_avr4},
85 {"at94k10", AVR_ISA_94K, bfd_mach_avr4},
86 {"at94k20", AVR_ISA_94K, bfd_mach_avr4},
87 {"at94k40", AVR_ISA_94K, bfd_mach_avr4},
88 {NULL, 0, 0}
89 };
90
91
92 /* Current MCU type. */
93 static struct mcu_type_s default_mcu = {"avr2", AVR_ISA_2xxx,bfd_mach_avr2};
94 static struct mcu_type_s *avr_mcu = &default_mcu;
95
96 const char EXP_CHARS[] = "eE";
97 const char FLT_CHARS[] = "dD";
98 static void avr_set_arch (int dummy);
99
100 /* The target specific pseudo-ops which we support. */
101 const pseudo_typeS md_pseudo_table[] =
102 {
103 {"arch", avr_set_arch, 0},
104 { NULL, NULL, 0}
105 };
106
107 #define LDI_IMMEDIATE(x) (((x) & 0xf) | (((x) << 4) & 0xf00))
108
109 static char * skip_space (char * s);
110 static char * extract_word (char *from, char *to, int limit);
111 static unsigned int avr_operand (struct avr_opcodes_s *opcode,
112 int where, char *op, char **line);
113 static unsigned int avr_operands (struct avr_opcodes_s *opcode, char **line);
114 static unsigned int avr_get_constant (char * str, int max);
115 static char *parse_exp (char *s, expressionS * op);
116 static bfd_reloc_code_real_type avr_ldi_expression (expressionS *exp);
117 long md_pcrel_from_section PARAMS ((fixS *, segT));
118
119
120 #define EXP_MOD_NAME(i) exp_mod[i].name
121 #define EXP_MOD_RELOC(i) exp_mod[i].reloc
122 #define EXP_MOD_NEG_RELOC(i) exp_mod[i].neg_reloc
123 #define HAVE_PM_P(i) exp_mod[i].have_pm
124
125 struct exp_mod_s
126 {
127 char * name;
128 bfd_reloc_code_real_type reloc;
129 bfd_reloc_code_real_type neg_reloc;
130 int have_pm;
131 };
132
133 static struct exp_mod_s exp_mod[] = {
134 {"hh8", BFD_RELOC_AVR_HH8_LDI, BFD_RELOC_AVR_HH8_LDI_NEG, 1},
135 {"pm_hh8", BFD_RELOC_AVR_HH8_LDI_PM, BFD_RELOC_AVR_HH8_LDI_PM_NEG, 0},
136 {"hi8", BFD_RELOC_AVR_HI8_LDI, BFD_RELOC_AVR_HI8_LDI_NEG, 1},
137 {"pm_hi8", BFD_RELOC_AVR_HI8_LDI_PM, BFD_RELOC_AVR_HI8_LDI_PM_NEG, 0},
138 {"lo8", BFD_RELOC_AVR_LO8_LDI, BFD_RELOC_AVR_LO8_LDI_NEG, 1},
139 {"pm_lo8", BFD_RELOC_AVR_LO8_LDI_PM, BFD_RELOC_AVR_LO8_LDI_PM_NEG, 0},
140 {"hlo8", -BFD_RELOC_AVR_LO8_LDI, -BFD_RELOC_AVR_LO8_LDI_NEG, 0},
141 {"hhi8", -BFD_RELOC_AVR_HI8_LDI, -BFD_RELOC_AVR_HI8_LDI_NEG, 0},
142 };
143
144 /* Opcode hash table. */
145 static struct hash_control *avr_hash;
146
147 /* Reloc modifiers hash control (hh8,hi8,lo8,pm_xx). */
148 static struct hash_control *avr_mod_hash;
149
150 #define OPTION_MMCU (OPTION_MD_BASE + 1)
151
152 struct option md_longopts[] = {
153 {"mmcu", required_argument, NULL, 'm'},
154 {NULL, no_argument, NULL, 0}
155 };
156 size_t md_longopts_size = sizeof(md_longopts);
157
158 static inline char *
159 skip_space (s)
160 char * s;
161 {
162 while (*s == ' ' || *s == '\t')
163 ++s;
164 return s;
165 }
166
167 /* Extract one word from FROM and copy it to TO. */
168 static char *
169 extract_word (char *from, char *to, int limit)
170 {
171 char *op_start;
172 char *op_end;
173 int size = 0;
174
175 /* Drop leading whitespace. */
176 from = skip_space (from);
177 *to = 0;
178 /* Find the op code end. */
179 for (op_start = op_end = from; *op_end != 0 && is_part_of_name(*op_end); )
180 {
181 to[size++] = *op_end++;
182 if (size + 1 >= limit)
183 break;
184 }
185 to[size] = 0;
186 return op_end;
187 }
188
189 int
190 md_estimate_size_before_relax (fragp, seg)
191 fragS *fragp ATTRIBUTE_UNUSED;
192 asection *seg ATTRIBUTE_UNUSED;
193 {
194 abort ();
195 return 0;
196 }
197
198 void
199 md_show_usage (stream)
200 FILE *stream;
201 {
202 fprintf
203 (stream,
204 _ ("AVR options:\n"
205 " -mmcu=[avr-name] select microcontroller variant\n"
206 " [avr-name] can be:\n"
207 " avr1 - AT90S1200\n"
208 " avr2 - AT90S2xxx, AT90S4xxx, AT90S85xx, ATtiny22\n"
209 " avr3 - ATmega103 or ATmega603\n"
210 " avr4 - ATmega161\n"
211 " or immediate microcontroller name.\n"));
212 }
213
214 static void
215 avr_set_arch (dummy)
216 int dummy ATTRIBUTE_UNUSED;
217 {
218 char * str;
219 str = (char *)alloca (20);
220 input_line_pointer = extract_word (input_line_pointer, str, 20);
221 md_parse_option ('m', str);
222 bfd_set_arch_mach (stdoutput, TARGET_ARCH, avr_mcu->mach);
223 }
224
225 int
226 md_parse_option (c, arg)
227 int c;
228 char *arg;
229 {
230 char *t = alloca (strlen (arg) + 1);
231 char *s = t;
232 char *arg1 = arg;
233 do
234 *t = tolower (*arg1++);
235 while (*t++);
236
237 if (c == 'm')
238 {
239 int i;
240
241 for (i = 0; mcu_types[i].name; ++i)
242 if (strcmp (mcu_types[i].name, s) == 0)
243 break;
244
245 if (!mcu_types[i].name)
246 as_fatal (_ ("unknown MCU: %s\n"), arg);
247 if (avr_mcu == &default_mcu)
248 avr_mcu = &mcu_types[i];
249 else
250 as_fatal (_ ("redefinition of mcu type `%s'"), mcu_types[i].name);
251 return 1;
252 }
253 return 0;
254 }
255
256 symbolS *
257 md_undefined_symbol(name)
258 char *name ATTRIBUTE_UNUSED;
259 {
260 return 0;
261 }
262
263 /* Convert a string pointed to by input_line_pointer into a floating point
264 constant of type `type', and store the appropriate bytes to `*litP'.
265 The number of LITTLENUMS emitted is stored in `*sizeP'. Returns NULL if
266 OK, or an error message otherwise. */
267 char *
268 md_atof (type, litP, sizeP)
269 int type;
270 char *litP;
271 int *sizeP;
272 {
273 int prec;
274 LITTLENUM_TYPE words[4];
275 LITTLENUM_TYPE *wordP;
276 char *t;
277
278 switch (type)
279 {
280 case 'f':
281 prec = 2;
282 break;
283 case 'd':
284 prec = 4;
285 break;
286 default:
287 *sizeP = 0;
288 return _("bad call to md_atof");
289 }
290
291 t = atof_ieee (input_line_pointer, type, words);
292 if (t)
293 input_line_pointer = t;
294
295 *sizeP = prec * sizeof (LITTLENUM_TYPE);
296 /* This loop outputs the LITTLENUMs in REVERSE order. */
297 for (wordP = words + prec - 1; prec--;)
298 {
299 md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
300 litP += sizeof (LITTLENUM_TYPE);
301 }
302 return NULL;
303 }
304
305 void
306 md_convert_frag (abfd, sec, fragP)
307 bfd *abfd ATTRIBUTE_UNUSED;
308 asection *sec ATTRIBUTE_UNUSED;
309 fragS *fragP ATTRIBUTE_UNUSED;
310 {
311 abort ();
312 }
313
314
315 void
316 md_begin ()
317 {
318 unsigned int i;
319 struct avr_opcodes_s *opcode;
320 avr_hash = hash_new();
321
322 /* Insert unique names into hash table. This hash table then provides a
323 quick index to the first opcode with a particular name in the opcode
324 table. */
325
326 for (opcode = avr_opcodes; opcode->name; opcode++)
327 hash_insert (avr_hash, opcode->name, (char *) opcode);
328
329 avr_mod_hash = hash_new ();
330
331 for (i = 0; i < sizeof (exp_mod) / sizeof (exp_mod[0]); ++i)
332 hash_insert (avr_mod_hash, EXP_MOD_NAME(i), (void*)(i+10));
333
334 bfd_set_arch_mach (stdoutput, TARGET_ARCH, avr_mcu->mach);
335 }
336
337
338 /* Resolve STR as a constant expression and return the result.
339 If result greater than MAX then error. */
340
341 static unsigned int
342 avr_get_constant (str, max)
343 char * str;
344 int max;
345 {
346 expressionS ex;
347 str = skip_space (str);
348 input_line_pointer = str;
349 expression (&ex);
350
351 if (ex.X_op != O_constant)
352 as_bad (_("constant value required"));
353
354 if (ex.X_add_number > max || ex.X_add_number < 0)
355 as_bad (_("number must be less than %d"), max+1);
356 return ex.X_add_number;
357 }
358
359
360 /* Parse instruction operands.
361 Returns binary opcode. */
362
363 static unsigned int
364 avr_operands (opcode, line)
365 struct avr_opcodes_s *opcode;
366 char **line;
367 {
368 char *op = opcode->constraints;
369 unsigned int bin = opcode->bin_opcode;
370 char *frag = frag_more (opcode->insn_size * 2);
371 char *str = *line;
372 int where = frag - frag_now->fr_literal;
373 static unsigned int prev = 0; /* previous opcode */
374
375 /* Opcode have operands. */
376 if (*op)
377 {
378 unsigned int reg1 = 0;
379 unsigned int reg2 = 0;
380 int reg1_present = 0;
381 int reg2_present = 0;
382
383 /* Parse first operand. */
384 if (REGISTER_P (*op))
385 reg1_present = 1;
386 reg1 = avr_operand (opcode, where, op, &str);
387 ++op;
388
389 /* Parse second operand. */
390 if (*op)
391 {
392 if (*op == ',')
393 ++op;
394 if (*op == '=')
395 {
396 reg2 = reg1;
397 reg2_present = 1;
398 }
399 else
400 {
401 if (REGISTER_P (*op))
402 reg2_present = 1;
403
404 str = skip_space (str);
405 if (*str++ != ',')
406 as_bad (_ ("`,' required"));
407 str = skip_space (str);
408
409 reg2 = avr_operand (opcode, where, op, &str);
410
411 }
412 if (reg1_present && reg2_present)
413 reg2 = (reg2 & 0xf) | ((reg2 << 5) & 0x200);
414 else if (reg2_present)
415 reg2 <<= 4;
416 }
417 if (reg1_present)
418 reg1 <<= 4;
419 bin |= reg1 | reg2;
420 }
421
422 /* detect undefined combinations (like lpm r31,Z+) */
423 if (((bin & 0xFDEF) == 0x91AD) || ((bin & 0xFDEF) == 0x91AE) ||
424 ((bin & 0xFDEF) == 0x91C9) || ((bin & 0xFDEF) == 0x91CA) ||
425 ((bin & 0xFDEF) == 0x91E1) || ((bin & 0xFDEF) == 0x91E2) ||
426 ((bin & 0xFFED) == 0x91E5))
427 as_warn( _("undefined combination of operands"));
428
429 if (opcode->insn_size == 2)
430 {
431 /* warn if previous opcode was cpse/sbic/sbis/sbrc/sbrs
432 (AVR core bug) */
433 if ((prev & 0xFC00) == 0x1000
434 || (prev & 0xFD00) == 0x9900
435 || (prev & 0xFC08) == 0xFC00)
436 as_warn (_("skipping two-word instruction"));
437
438 bfd_putl32 ((bfd_vma)bin, frag);
439 }
440 else
441 bfd_putl16 ((bfd_vma)bin, frag);
442
443 prev = bin;
444 *line = str;
445 return bin;
446 }
447
448
449 /* Parse one instruction operand.
450 Returns operand bitmask. Also fixups can be generated. */
451
452 static unsigned int
453 avr_operand (opcode, where, op, line)
454 struct avr_opcodes_s *opcode;
455 int where;
456 char *op;
457 char **line;
458 {
459 expressionS op_expr;
460 unsigned int op_mask = 0;
461 char *str = skip_space (*line);
462
463 switch (*op)
464 {
465 /* Any register operand. */
466 case 'w':
467 case 'd':
468 case 'r':
469 case 'a':
470 case 'v':
471 {
472 op_mask = -1;
473
474 if (*str == 'r' || *str == 'R')
475 {
476 char r_name[20];
477
478 str = extract_word (str, r_name, sizeof (r_name));
479 if (isdigit(r_name[1]))
480 {
481 if (r_name[2] == '\0')
482 op_mask = r_name[1] - '0';
483 else if (r_name[1] != '0'
484 && isdigit(r_name[2])
485 && r_name[3] == '\0')
486 op_mask = (r_name[1] - '0') * 10 + r_name[2] - '0';
487 }
488 }
489 else
490 {
491 op_mask = avr_get_constant (str, 31);
492 str = input_line_pointer;
493 }
494
495 if (op_mask <= 31)
496 {
497 switch (*op)
498 {
499 case 'a':
500 if (op_mask < 16 || op_mask > 23)
501 as_bad (_ ("register r16-r23 required"));
502 op_mask -= 16;
503 break;
504
505 case 'd':
506 if (op_mask < 16)
507 as_bad (_ ("register number above 15 required"));
508 op_mask -= 16;
509 break;
510
511 case 'v':
512 if (op_mask & 1)
513 as_bad (_ ("even register number required"));
514 op_mask >>= 1;
515 break;
516
517 case 'w':
518 op_mask -= 24;
519 if (op_mask & 1 || op_mask > 6)
520 as_bad (_ ("register r24,r26,r28 or r30 required"));
521 op_mask >>= 1;
522 break;
523 }
524 break;
525 }
526 as_bad (_ ("register name or number from 0 to 31 required"));
527 }
528 break;
529
530 case 'e':
531 {
532 char c;
533 if (*str == '-')
534 {
535 str = skip_space (str+1);
536 op_mask = 0x1002;
537 }
538 c = tolower (*str);
539 if (c == 'x')
540 op_mask |= 0x100c;
541 else if (c == 'y')
542 op_mask |= 0x8;
543 else if (c != 'z')
544 as_bad (_ ("pointer register (X,Y or Z) required"));
545
546 str = skip_space (str+1);
547 if (*str == '+')
548 {
549 ++str;
550 if (op_mask & 2)
551 as_bad (_ ("cannot both predecrement and postincrement"));
552 op_mask |= 0x1001;
553 }
554 /* avr1 can do "ld r,Z" and "st Z,r" but no other pointer
555 registers, no predecrement, no postincrement */
556 if ((op_mask & 0x100F) && !(avr_mcu->isa & AVR_ISA_SRAM))
557 as_bad (_ ("addressing mode not supported"));
558 }
559 break;
560
561 case 'z':
562 {
563 if (*str == '-')
564 as_bad (_ ("can't predecrement"));
565
566 if (! (*str == 'z' || *str == 'Z'))
567 as_bad (_ ("pointer register Z required"));
568
569 str = skip_space (str + 1);
570 if (*str == '+')
571 {
572 ++str;
573 op_mask |= 1;
574 }
575 }
576 break;
577
578 case 'b':
579 {
580 char c = tolower (*str++);
581 if (c == 'y')
582 op_mask |= 0x8;
583 else if (c != 'z')
584 as_bad (_ ("pointer register (Y or Z) required"));
585 str = skip_space (str);
586 if (*str++ == '+')
587 {
588 unsigned int x;
589 x = avr_get_constant (str, 63);
590 str = input_line_pointer;
591 op_mask |= (x & 7) | ((x & (3 << 3)) << 7) | ((x & (1 << 5)) << 8);
592 }
593 }
594 break;
595
596 case 'h':
597 {
598 str = parse_exp (str, &op_expr);
599 fix_new_exp (frag_now, where, opcode->insn_size * 2,
600 &op_expr, false, BFD_RELOC_AVR_CALL);
601
602 }
603 break;
604
605 case 'L':
606 {
607 str = parse_exp (str, &op_expr);
608 fix_new_exp (frag_now, where, opcode->insn_size * 2,
609 &op_expr, true, BFD_RELOC_AVR_13_PCREL);
610
611 }
612 break;
613
614 case 'l':
615 {
616 str = parse_exp (str, &op_expr);
617 fix_new_exp (frag_now, where, opcode->insn_size * 2,
618 &op_expr, true, BFD_RELOC_AVR_7_PCREL);
619
620 }
621 break;
622
623 case 'i':
624 {
625 str = parse_exp (str, &op_expr);
626 fix_new_exp (frag_now, where+2, opcode->insn_size * 2,
627 &op_expr, false, BFD_RELOC_16);
628
629 }
630 break;
631
632 case 'M':
633 {
634 bfd_reloc_code_real_type r_type;
635 input_line_pointer = str;
636 r_type = avr_ldi_expression (&op_expr);
637 str = input_line_pointer;
638 fix_new_exp (frag_now, where, 3,
639 &op_expr, false, r_type);
640 }
641 break;
642
643 case 'n':
644 {
645 unsigned int x;
646 x = ~avr_get_constant (str, 255);
647 str = input_line_pointer;
648 op_mask |= (x & 0xf) | ((x << 4) & 0xf00);
649 }
650 break;
651
652 case 'K':
653 {
654 unsigned int x;
655 x = avr_get_constant (str, 63);
656 str = input_line_pointer;
657 op_mask |= (x & 0xf) | ((x & 0x30) << 2);
658 }
659 break;
660
661 case 'S':
662 case 's':
663 {
664 unsigned int x;
665 x = avr_get_constant (str, 7);
666 str = input_line_pointer;
667 if (*op == 'S')
668 x <<= 4;
669 op_mask |= x;
670 }
671 break;
672
673 case 'P':
674 {
675 unsigned int x;
676 x = avr_get_constant (str, 63);
677 str = input_line_pointer;
678 op_mask |= (x & 0xf) | ((x & 0x30) << 5);
679 }
680 break;
681
682 case 'p':
683 {
684 unsigned int x;
685 x = avr_get_constant (str, 31);
686 str = input_line_pointer;
687 op_mask |= x << 3;
688 }
689 break;
690 case '?':
691 break;
692 default:
693 as_bad (_ ("unknown constraint `%c'"), *op);
694 }
695 *line = str;
696 return op_mask;
697 }
698
699 /* GAS will call this function for each section at the end of the assembly,
700 to permit the CPU backend to adjust the alignment of a section. */
701 valueT
702 md_section_align (seg, addr)
703 asection *seg;
704 valueT addr;
705 {
706 int align = bfd_get_section_alignment (stdoutput, seg);
707 return ((addr + (1 << align) - 1) & (-1 << align));
708 }
709
710 /* If you define this macro, it should return the offset between the
711 address of a PC relative fixup and the position from which the PC
712 relative adjustment should be made. On many processors, the base
713 of a PC relative instruction is the next instruction, so this
714 macro would return the length of an instruction. */
715 long
716 md_pcrel_from_section (fixp, sec)
717 fixS *fixp;
718 segT sec;
719 {
720 if (fixp->fx_addsy != (symbolS *)NULL
721 && (!S_IS_DEFINED (fixp->fx_addsy)
722 || (S_GET_SEGMENT (fixp->fx_addsy) != sec)))
723 return 0;
724 return fixp->fx_frag->fr_address + fixp->fx_where;
725 }
726
727 /* GAS will call this for each fixup. It should store the correct
728 value in the object file. */
729 int
730 md_apply_fix3 (fixp, valuep, seg)
731 fixS *fixp;
732 valueT *valuep;
733 segT seg;
734 {
735 unsigned char *where;
736 unsigned long insn;
737 long value;
738
739 if (fixp->fx_addsy == (symbolS *) NULL)
740 {
741 value = *valuep;
742 fixp->fx_done = 1;
743 }
744 else if (fixp->fx_pcrel)
745 {
746 segT s = S_GET_SEGMENT (fixp->fx_addsy);
747 if (fixp->fx_addsy && (s == seg || s == absolute_section))
748 {
749 value = S_GET_VALUE (fixp->fx_addsy) + *valuep;
750 fixp->fx_done = 1;
751 }
752 else
753 value = *valuep;
754 }
755 else
756 {
757 value = fixp->fx_offset;
758 if (fixp->fx_subsy != (symbolS *) NULL)
759 {
760 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
761 {
762 value -= S_GET_VALUE (fixp->fx_subsy);
763 fixp->fx_done = 1;
764 }
765 else
766 {
767 /* We don't actually support subtracting a symbol. */
768 as_bad_where (fixp->fx_file, fixp->fx_line,
769 _ ("expression too complex"));
770 }
771 }
772 }
773 switch (fixp->fx_r_type)
774 {
775 default:
776 fixp->fx_no_overflow = 1;
777 break;
778 case BFD_RELOC_AVR_7_PCREL:
779 case BFD_RELOC_AVR_13_PCREL:
780 case BFD_RELOC_32:
781 case BFD_RELOC_16:
782 case BFD_RELOC_AVR_CALL:
783 break;
784 }
785
786 if (fixp->fx_done)
787 {
788 /* Fetch the instruction, insert the fully resolved operand
789 value, and stuff the instruction back again. */
790 where = fixp->fx_frag->fr_literal + fixp->fx_where;
791 insn = bfd_getl16 (where);
792
793 switch (fixp->fx_r_type)
794 {
795 case BFD_RELOC_AVR_7_PCREL:
796 if (value & 1)
797 as_bad_where (fixp->fx_file, fixp->fx_line,
798 _("odd address operand: %ld"), value);
799 /* Instruction addresses are always right-shifted by 1. */
800 value >>= 1;
801 --value; /* Correct PC. */
802 if (value < -64 || value > 63)
803 as_bad_where (fixp->fx_file, fixp->fx_line,
804 _("operand out of range: %ld"), value);
805 value = (value << 3) & 0x3f8;
806 bfd_putl16 ((bfd_vma) (value | insn), where);
807 break;
808
809 case BFD_RELOC_AVR_13_PCREL:
810 if (value & 1)
811 as_bad_where (fixp->fx_file, fixp->fx_line,
812 _("odd address operand: %ld"), value);
813 /* Instruction addresses are always right-shifted by 1. */
814 value >>= 1;
815 --value; /* Correct PC. */
816
817 if (value < -2048 || value > 2047)
818 {
819 if (avr_mcu->isa & AVR_ISA_WRAP)
820 {
821 if (value > 2047)
822 value -= 4096;
823 else
824 value += 4096;
825 }
826 else
827 as_bad_where (fixp->fx_file, fixp->fx_line,
828 _("operand out of range: %ld"), value);
829 }
830
831 value &= 0xfff;
832 bfd_putl16 ((bfd_vma) (value | insn), where);
833 break;
834
835 case BFD_RELOC_32:
836 bfd_putl16 ((bfd_vma) value, where);
837 break;
838
839 case BFD_RELOC_16:
840 bfd_putl16 ((bfd_vma) value, where);
841 break;
842
843 case BFD_RELOC_AVR_16_PM:
844 bfd_putl16 ((bfd_vma) (value>>1), where);
845 break;
846
847 case BFD_RELOC_AVR_LO8_LDI:
848 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value), where);
849 break;
850
851 case -BFD_RELOC_AVR_LO8_LDI:
852 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 16), where);
853 break;
854
855 case BFD_RELOC_AVR_HI8_LDI:
856 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 8), where);
857 break;
858
859 case -BFD_RELOC_AVR_HI8_LDI:
860 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 24), where);
861 break;
862
863 case BFD_RELOC_AVR_HH8_LDI:
864 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 16), where);
865 break;
866
867 case BFD_RELOC_AVR_LO8_LDI_NEG:
868 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value), where);
869 break;
870
871 case -BFD_RELOC_AVR_LO8_LDI_NEG:
872 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 16), where);
873 break;
874
875 case BFD_RELOC_AVR_HI8_LDI_NEG:
876 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 8), where);
877 break;
878
879 case -BFD_RELOC_AVR_HI8_LDI_NEG:
880 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 24), where);
881 break;
882
883 case BFD_RELOC_AVR_HH8_LDI_NEG:
884 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 16), where);
885 break;
886
887 case BFD_RELOC_AVR_LO8_LDI_PM:
888 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 1), where);
889 break;
890
891 case BFD_RELOC_AVR_HI8_LDI_PM:
892 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 9), where);
893 break;
894
895 case BFD_RELOC_AVR_HH8_LDI_PM:
896 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 17), where);
897 break;
898
899 case BFD_RELOC_AVR_LO8_LDI_PM_NEG:
900 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 1), where);
901 break;
902
903 case BFD_RELOC_AVR_HI8_LDI_PM_NEG:
904 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 9), where);
905 break;
906
907 case BFD_RELOC_AVR_HH8_LDI_PM_NEG:
908 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 17), where);
909 break;
910
911 case BFD_RELOC_AVR_CALL:
912 {
913 unsigned long x;
914 x = bfd_getl16 (where);
915 if (value & 1)
916 as_bad_where (fixp->fx_file, fixp->fx_line,
917 _("odd address operand: %ld"), value);
918 value >>= 1;
919 x |= ((value & 0x10000) | ((value << 3) & 0x1f00000)) >> 16;
920 bfd_putl16 ((bfd_vma) x, where);
921 bfd_putl16 ((bfd_vma) (value & 0xffff), where+2);
922 }
923 break;
924
925 default:
926 as_fatal ( _("line %d: unknown relocation type: 0x%x"),
927 fixp->fx_line, fixp->fx_r_type);
928 break;
929 }
930 }
931 else
932 {
933 switch (fixp->fx_r_type)
934 {
935 case -BFD_RELOC_AVR_HI8_LDI_NEG:
936 case -BFD_RELOC_AVR_HI8_LDI:
937 case -BFD_RELOC_AVR_LO8_LDI_NEG:
938 case -BFD_RELOC_AVR_LO8_LDI:
939 as_bad_where (fixp->fx_file, fixp->fx_line,
940 _("only constant expression allowed"));
941 fixp->fx_done = 1;
942 break;
943 default:
944 break;
945 }
946 fixp->fx_addnumber = value;
947 }
948 return 0;
949 }
950
951
952 /* A `BFD_ASSEMBLER' GAS will call this to generate a reloc. GAS
953 will pass the resulting reloc to `bfd_install_relocation'. This
954 currently works poorly, as `bfd_install_relocation' often does the
955 wrong thing, and instances of `tc_gen_reloc' have been written to
956 work around the problems, which in turns makes it difficult to fix
957 `bfd_install_relocation'. */
958
959 /* If while processing a fixup, a reloc really needs to be created
960 then it is done here. */
961
962 arelent *
963 tc_gen_reloc (seg, fixp)
964 asection *seg ATTRIBUTE_UNUSED;
965 fixS *fixp;
966 {
967 arelent *reloc;
968
969 reloc = (arelent *) xmalloc (sizeof (arelent));
970
971 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
972 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
973
974 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
975 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
976 if (reloc->howto == (reloc_howto_type *) NULL)
977 {
978 as_bad_where (fixp->fx_file, fixp->fx_line,
979 _("reloc %d not supported by object file format"),
980 (int)fixp->fx_r_type);
981 return NULL;
982 }
983
984 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
985 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
986 reloc->address = fixp->fx_offset;
987
988 reloc->addend = fixp->fx_offset;
989
990 return reloc;
991 }
992
993
994 void
995 md_assemble (str)
996 char *str;
997 {
998 struct avr_opcodes_s * opcode;
999 char op[11];
1000
1001 str = skip_space (extract_word (str, op, sizeof(op)));
1002
1003 if (!op[0])
1004 as_bad (_ ("can't find opcode "));
1005
1006 opcode = (struct avr_opcodes_s *) hash_find (avr_hash, op);
1007
1008 if (opcode == NULL)
1009 {
1010 as_bad (_ ("unknown opcode `%s'"), op);
1011 return;
1012 }
1013
1014 /* Special case for opcodes with optional operands (lpm, elpm) -
1015 version with operands exists in avr_opcodes[] in the next entry. */
1016
1017 if (*str && *opcode->constraints == '?')
1018 ++opcode;
1019
1020 if ((opcode->isa & avr_mcu->isa) != opcode->isa)
1021 as_bad (_ ("illegal opcode %s for mcu %s"), opcode->name, avr_mcu->name);
1022
1023 /* We used to set input_line_pointer to the result of get_operands,
1024 but that is wrong. Our caller assumes we don't change it. */
1025 {
1026 char *t = input_line_pointer;
1027 avr_operands (opcode, &str);
1028 if (*skip_space (str))
1029 as_bad (_ ("garbage at end of line"));
1030 input_line_pointer = t;
1031 }
1032 }
1033
1034 /* Parse ordinary expression. */
1035 static char *
1036 parse_exp (s, op)
1037 char *s;
1038 expressionS * op;
1039 {
1040 input_line_pointer = s;
1041 expression (op);
1042 if (op->X_op == O_absent)
1043 as_bad (_("missing operand"));
1044 return input_line_pointer;
1045 }
1046
1047
1048 /* Parse special expressions (needed for LDI command):
1049 xx8 (address)
1050 xx8 (-address)
1051 pm_xx8 (address)
1052 pm_xx8 (-address)
1053 where xx is: hh, hi, lo
1054 */
1055 static bfd_reloc_code_real_type
1056 avr_ldi_expression (exp)
1057 expressionS *exp;
1058 {
1059 char *str = input_line_pointer;
1060 char *tmp;
1061 char op[8];
1062 int mod;
1063 tmp = str;
1064
1065 str = extract_word (str, op, sizeof (op));
1066 if (op[0])
1067 {
1068 mod = (int) hash_find (avr_mod_hash, op);
1069 if (mod)
1070 {
1071 int closes = 0;
1072 mod -= 10;
1073 str = skip_space (str);
1074 if (*str == '(')
1075 {
1076 int neg_p = 0;
1077 ++str;
1078 if (strncmp ("pm(", str, 3) == 0
1079 || strncmp ("-(pm(", str, 5) == 0)
1080 {
1081 if (HAVE_PM_P(mod))
1082 {
1083 ++mod;
1084 ++closes;
1085 }
1086 else
1087 as_bad (_ ("illegal expression"));
1088 if (*str == '-')
1089 {
1090 neg_p = 1;
1091 ++closes;
1092 str += 5;
1093 }
1094 else
1095 str += 3;
1096 }
1097 if (*str == '-' && *(str + 1) == '(')
1098 {
1099 neg_p ^= 1;
1100 ++closes;
1101 str += 2;
1102 }
1103 input_line_pointer = str;
1104 expression (exp);
1105 do
1106 {
1107 if (*input_line_pointer != ')')
1108 {
1109 as_bad (_ ("`)' required"));
1110 break;
1111 }
1112 input_line_pointer++;
1113 }
1114 while (closes--);
1115 return neg_p ? EXP_MOD_NEG_RELOC (mod) : EXP_MOD_RELOC (mod);
1116 }
1117 }
1118 }
1119 input_line_pointer = tmp;
1120 expression (exp);
1121 return BFD_RELOC_AVR_LO8_LDI;
1122 }
1123
1124 /* Flag to pass `pm' mode between `avr_parse_cons_expression' and
1125 `avr_cons_fix_new' */
1126 static int exp_mod_pm = 0;
1127
1128 /* Parse special CONS expression: pm (expression)
1129 which is used for addressing to a program memory.
1130 Relocation: BFD_RELOC_AVR_16_PM */
1131 void
1132 avr_parse_cons_expression (exp, nbytes)
1133 expressionS *exp;
1134 int nbytes;
1135 {
1136 char * tmp;
1137
1138 exp_mod_pm = 0;
1139
1140 tmp = input_line_pointer = skip_space (input_line_pointer);
1141
1142 if (nbytes == 2)
1143 {
1144 char * pm_name = "pm";
1145 int len = strlen (pm_name);
1146 if (strncasecmp (input_line_pointer, pm_name, len) == 0)
1147 {
1148 input_line_pointer = skip_space (input_line_pointer + len);
1149 if (*input_line_pointer == '(')
1150 {
1151 input_line_pointer = skip_space (input_line_pointer + 1);
1152 exp_mod_pm = 1;
1153 expression (exp);
1154 if (*input_line_pointer == ')')
1155 ++input_line_pointer;
1156 else
1157 {
1158 as_bad (_ ("`)' required"));
1159 exp_mod_pm = 0;
1160 }
1161 return;
1162 }
1163 input_line_pointer = tmp;
1164 }
1165 }
1166 expression (exp);
1167 }
1168
1169 void
1170 avr_cons_fix_new(frag, where, nbytes, exp)
1171 fragS *frag;
1172 int where;
1173 int nbytes;
1174 expressionS *exp;
1175 {
1176 if (exp_mod_pm == 0)
1177 {
1178 if (nbytes == 2)
1179 fix_new_exp (frag, where, nbytes, exp, false, BFD_RELOC_16);
1180 else if (nbytes == 4)
1181 fix_new_exp (frag, where, nbytes, exp, false, BFD_RELOC_32);
1182 else
1183 as_bad (_ ("illegal %srelocation size: %d"), "", nbytes);
1184 }
1185 else
1186 {
1187 if (nbytes == 2)
1188 fix_new_exp (frag, where, nbytes, exp, false, BFD_RELOC_AVR_16_PM);
1189 else
1190 as_bad (_ ("illegal %srelocation size: %d"), "`pm' ", nbytes);
1191 exp_mod_pm = 0;
1192 }
1193 }
This page took 0.060191 seconds and 5 git commands to generate.