2001-04-27 David Mosberger <davidm@hpl.hp.com>
[binutils.git] / gas / config / tc-avr.c
blob78954e11ad7f7e10e96cd0c2c71a55897362a006
1 /* tc-avr.c -- Assembler code for the ATMEL AVR
3 Copyright 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Denis Chertykov <denisc@overta.ru>
6 This file is part of GAS, the GNU Assembler.
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.
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.
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. */
23 #include <stdio.h>
24 #include <ctype.h>
25 #include "as.h"
26 #include "subsegs.h"
28 struct avr_opcodes_s
30 char *name;
31 char *constraints;
32 int insn_size; /* In words. */
33 int isa;
34 unsigned int bin_opcode;
37 #define AVR_INSN(NAME, CONSTR, OPCODE, SIZE, ISA, BIN) \
38 {#NAME, CONSTR, SIZE, ISA, BIN},
40 struct avr_opcodes_s avr_opcodes[] =
42 #include "opcode/avr.h"
43 {NULL, NULL, 0, 0, 0}
46 const char comment_chars[] = ";";
47 const char line_comment_chars[] = "#";
48 const char line_separator_chars[] = "$";
50 const char *md_shortopts = "m:";
51 struct mcu_type_s
53 char *name;
54 int isa;
55 int mach;
58 static struct mcu_type_s mcu_types[] =
60 {"avr1", AVR_ISA_TINY1, bfd_mach_avr1},
61 {"avr2", AVR_ISA_2xxx, bfd_mach_avr2},
62 {"avr3", AVR_ISA_M103, bfd_mach_avr3},
63 {"avr4", AVR_ISA_M83, bfd_mach_avr4},
64 {"avr5", AVR_ISA_ALL, bfd_mach_avr5},
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_2xxx, bfd_mach_avr2},
80 {"at90s8535", AVR_ISA_2xxx, bfd_mach_avr2},
81 {"at90c8534", AVR_ISA_2xxx, bfd_mach_avr2},
82 {"atmega603", AVR_ISA_M603, bfd_mach_avr3},
83 {"atmega103", AVR_ISA_M103, bfd_mach_avr3},
84 {"atmega83", AVR_ISA_M83, bfd_mach_avr4},
85 {"atmega85", AVR_ISA_M83, bfd_mach_avr4},
86 {"atmega161", AVR_ISA_M161, bfd_mach_avr5},
87 {"atmega163", AVR_ISA_M161, bfd_mach_avr5},
88 {"atmega32", AVR_ISA_M161, bfd_mach_avr5},
89 {"at94k", AVR_ISA_94K, bfd_mach_avr5},
90 {NULL, 0, 0}
93 /* Current MCU type. */
94 static struct mcu_type_s default_mcu = {"avr2", AVR_ISA_2xxx,bfd_mach_avr2};
95 static struct mcu_type_s *avr_mcu = &default_mcu;
97 /* AVR target-specific switches. */
98 struct avr_opt_s
100 int all_opcodes; /* -mall-opcodes: accept all known AVR opcodes */
101 int no_skip_bug; /* -mno-skip-bug: no warnings for skipping 2-word insns */
102 int no_wrap; /* -mno-wrap: reject rjmp/rcall with 8K wrap-around */
105 static struct avr_opt_s avr_opt = { 0, 0, 0 };
107 const char EXP_CHARS[] = "eE";
108 const char FLT_CHARS[] = "dD";
109 static void avr_set_arch (int dummy);
111 /* The target specific pseudo-ops which we support. */
112 const pseudo_typeS md_pseudo_table[] =
114 {"arch", avr_set_arch, 0},
115 { NULL, NULL, 0}
118 #define LDI_IMMEDIATE(x) (((x) & 0xf) | (((x) << 4) & 0xf00))
120 static void show_mcu_list PARAMS ((FILE *));
121 static char *skip_space PARAMS ((char *));
122 static char *extract_word PARAMS ((char *, char *, int));
123 static unsigned int avr_operand PARAMS ((struct avr_opcodes_s *,
124 int, char *, char **));
125 static unsigned int avr_operands PARAMS ((struct avr_opcodes_s *, char **));
126 static unsigned int avr_get_constant PARAMS ((char *, int));
127 static char *parse_exp PARAMS ((char *, expressionS *));
128 static bfd_reloc_code_real_type avr_ldi_expression PARAMS ((expressionS *));
130 #define EXP_MOD_NAME(i) exp_mod[i].name
131 #define EXP_MOD_RELOC(i) exp_mod[i].reloc
132 #define EXP_MOD_NEG_RELOC(i) exp_mod[i].neg_reloc
133 #define HAVE_PM_P(i) exp_mod[i].have_pm
135 struct exp_mod_s
137 char *name;
138 bfd_reloc_code_real_type reloc;
139 bfd_reloc_code_real_type neg_reloc;
140 int have_pm;
143 static struct exp_mod_s exp_mod[] =
145 {"hh8", BFD_RELOC_AVR_HH8_LDI, BFD_RELOC_AVR_HH8_LDI_NEG, 1},
146 {"pm_hh8", BFD_RELOC_AVR_HH8_LDI_PM, BFD_RELOC_AVR_HH8_LDI_PM_NEG, 0},
147 {"hi8", BFD_RELOC_AVR_HI8_LDI, BFD_RELOC_AVR_HI8_LDI_NEG, 1},
148 {"pm_hi8", BFD_RELOC_AVR_HI8_LDI_PM, BFD_RELOC_AVR_HI8_LDI_PM_NEG, 0},
149 {"lo8", BFD_RELOC_AVR_LO8_LDI, BFD_RELOC_AVR_LO8_LDI_NEG, 1},
150 {"pm_lo8", BFD_RELOC_AVR_LO8_LDI_PM, BFD_RELOC_AVR_LO8_LDI_PM_NEG, 0},
151 {"hlo8", -BFD_RELOC_AVR_LO8_LDI, -BFD_RELOC_AVR_LO8_LDI_NEG, 0},
152 {"hhi8", -BFD_RELOC_AVR_HI8_LDI, -BFD_RELOC_AVR_HI8_LDI_NEG, 0},
155 /* Opcode hash table. */
156 static struct hash_control *avr_hash;
158 /* Reloc modifiers hash control (hh8,hi8,lo8,pm_xx). */
159 static struct hash_control *avr_mod_hash;
161 #define OPTION_MMCU 'm'
162 #define OPTION_ALL_OPCODES (OPTION_MD_BASE + 1)
163 #define OPTION_NO_SKIP_BUG (OPTION_MD_BASE + 2)
164 #define OPTION_NO_WRAP (OPTION_MD_BASE + 3)
166 struct option md_longopts[] =
168 { "mmcu", required_argument, NULL, OPTION_MMCU },
169 { "mall-opcodes", no_argument, NULL, OPTION_ALL_OPCODES },
170 { "mno-skip-bug", no_argument, NULL, OPTION_NO_SKIP_BUG },
171 { "mno-wrap", no_argument, NULL, OPTION_NO_WRAP },
172 { NULL, no_argument, NULL, 0 }
175 size_t md_longopts_size = sizeof (md_longopts);
177 /* Display nicely formatted list of known MCU names. */
179 static void
180 show_mcu_list (stream)
181 FILE *stream;
183 int i, x;
185 fprintf (stream, _("Known MCU names:"));
186 x = 1000;
188 for (i = 0; mcu_types[i].name; i++)
190 int len = strlen (mcu_types[i].name);
192 x += len + 1;
194 if (x < 75)
195 fprintf (stream, " %s", mcu_types[i].name);
196 else
198 fprintf (stream, "\n %s", mcu_types[i].name);
199 x = len + 2;
203 fprintf (stream, "\n");
206 static inline char *
207 skip_space (s)
208 char *s;
210 while (*s == ' ' || *s == '\t')
211 ++s;
212 return s;
215 /* Extract one word from FROM and copy it to TO. */
217 static char *
218 extract_word (char *from, char *to, int limit)
220 char *op_start;
221 char *op_end;
222 int size = 0;
224 /* Drop leading whitespace. */
225 from = skip_space (from);
226 *to = 0;
228 /* Find the op code end. */
229 for (op_start = op_end = from; *op_end != 0 && is_part_of_name (*op_end);)
231 to[size++] = *op_end++;
232 if (size + 1 >= limit)
233 break;
236 to[size] = 0;
237 return op_end;
241 md_estimate_size_before_relax (fragp, seg)
242 fragS *fragp ATTRIBUTE_UNUSED;
243 asection *seg ATTRIBUTE_UNUSED;
245 abort ();
246 return 0;
249 void
250 md_show_usage (stream)
251 FILE *stream;
253 fprintf (stream,
254 _("AVR options:\n"
255 " -mmcu=[avr-name] select microcontroller variant\n"
256 " [avr-name] can be:\n"
257 " avr1 - AT90S1200, ATtiny1x, ATtiny28\n"
258 " avr2 - AT90S2xxx, AT90S4xxx, AT90S8xxx, ATtiny22\n"
259 " avr3 - ATmega103, ATmega603\n"
260 " avr4 - ATmega83, ATmega85\n"
261 " avr5 - ATmega161, ATmega163, ATmega32, AT94K\n"
262 " or immediate microcontroller name.\n"));
263 fprintf (stream,
264 _(" -mall-opcodes accept all AVR opcodes, even if not supported by MCU\n"
265 " -mno-skip-bug disable warnings for skipping two-word instructions\n"
266 " (default for avr4, avr5)\n"
267 " -mno-wrap reject rjmp/rcall instructions with 8K wrap-around\n"
268 " (default for avr3, avr5)\n"));
269 show_mcu_list (stream);
272 static void
273 avr_set_arch (dummy)
274 int dummy ATTRIBUTE_UNUSED;
276 char *str;
278 str = (char *) alloca (20);
279 input_line_pointer = extract_word (input_line_pointer, str, 20);
280 md_parse_option (OPTION_MMCU, str);
281 bfd_set_arch_mach (stdoutput, TARGET_ARCH, avr_mcu->mach);
285 md_parse_option (c, arg)
286 int c;
287 char *arg;
289 switch (c)
291 case OPTION_MMCU:
293 int i;
294 char *s = alloca (strlen (arg) + 1);
297 char *t = s;
298 char *arg1 = arg;
301 *t = tolower (*arg1++);
302 while (*t++);
305 for (i = 0; mcu_types[i].name; ++i)
306 if (strcmp (mcu_types[i].name, s) == 0)
307 break;
309 if (!mcu_types[i].name)
311 show_mcu_list (stderr);
312 as_fatal (_("unknown MCU: %s\n"), arg);
315 /* It is OK to redefine mcu type within the same avr[1-5] bfd machine
316 type - this for allows passing -mmcu=... via gcc ASM_SPEC as well
317 as .arch ... in the asm output at the same time. */
318 if (avr_mcu == &default_mcu || avr_mcu->mach == mcu_types[i].mach)
319 avr_mcu = &mcu_types[i];
320 else
321 as_fatal (_("redefinition of mcu type `%s' to `%s'"),
322 avr_mcu->name, mcu_types[i].name);
323 return 1;
325 case OPTION_ALL_OPCODES:
326 avr_opt.all_opcodes = 1;
327 return 1;
328 case OPTION_NO_SKIP_BUG:
329 avr_opt.no_skip_bug = 1;
330 return 1;
331 case OPTION_NO_WRAP:
332 avr_opt.no_wrap = 1;
333 return 1;
336 return 0;
339 symbolS *
340 md_undefined_symbol (name)
341 char *name ATTRIBUTE_UNUSED;
343 return 0;
346 /* Turn a string in input_line_pointer into a floating point constant
347 of type TYPE, and store the appropriate bytes in *LITP. The number
348 of LITTLENUMS emitted is stored in *SIZEP. An error message is
349 returned, or NULL on OK. */
351 char *
352 md_atof (type, litP, sizeP)
353 int type;
354 char *litP;
355 int *sizeP;
357 int prec;
358 LITTLENUM_TYPE words[4];
359 LITTLENUM_TYPE *wordP;
360 char *t;
362 switch (type)
364 case 'f':
365 prec = 2;
366 break;
367 case 'd':
368 prec = 4;
369 break;
370 default:
371 *sizeP = 0;
372 return _("bad call to md_atof");
375 t = atof_ieee (input_line_pointer, type, words);
376 if (t)
377 input_line_pointer = t;
379 *sizeP = prec * sizeof (LITTLENUM_TYPE);
381 /* This loop outputs the LITTLENUMs in REVERSE order. */
382 for (wordP = words + prec - 1; prec--;)
384 md_number_to_chars (litP, (valueT) (*wordP--), sizeof (LITTLENUM_TYPE));
385 litP += sizeof (LITTLENUM_TYPE);
388 return NULL;
391 void
392 md_convert_frag (abfd, sec, fragP)
393 bfd *abfd ATTRIBUTE_UNUSED;
394 asection *sec ATTRIBUTE_UNUSED;
395 fragS *fragP ATTRIBUTE_UNUSED;
397 abort ();
400 void
401 md_begin ()
403 unsigned int i;
404 struct avr_opcodes_s *opcode;
405 avr_hash = hash_new ();
407 /* Insert unique names into hash table. This hash table then provides a
408 quick index to the first opcode with a particular name in the opcode
409 table. */
410 for (opcode = avr_opcodes; opcode->name; opcode++)
411 hash_insert (avr_hash, opcode->name, (char *) opcode);
413 avr_mod_hash = hash_new ();
415 for (i = 0; i < sizeof (exp_mod) / sizeof (exp_mod[0]); ++i)
416 hash_insert (avr_mod_hash, EXP_MOD_NAME (i), (void *) (i + 10));
418 bfd_set_arch_mach (stdoutput, TARGET_ARCH, avr_mcu->mach);
421 /* Resolve STR as a constant expression and return the result.
422 If result greater than MAX then error. */
424 static unsigned int
425 avr_get_constant (str, max)
426 char *str;
427 int max;
429 expressionS ex;
430 str = skip_space (str);
431 input_line_pointer = str;
432 expression (&ex);
434 if (ex.X_op != O_constant)
435 as_bad (_("constant value required"));
437 if (ex.X_add_number > max || ex.X_add_number < 0)
438 as_bad (_("number must be less than %d"), max + 1);
440 return ex.X_add_number;
443 /* Parse instruction operands.
444 Return binary opcode. */
446 static unsigned int
447 avr_operands (opcode, line)
448 struct avr_opcodes_s *opcode;
449 char **line;
451 char *op = opcode->constraints;
452 unsigned int bin = opcode->bin_opcode;
453 char *frag = frag_more (opcode->insn_size * 2);
454 char *str = *line;
455 int where = frag - frag_now->fr_literal;
456 static unsigned int prev = 0; /* Previous opcode. */
458 /* Opcode have operands. */
459 if (*op)
461 unsigned int reg1 = 0;
462 unsigned int reg2 = 0;
463 int reg1_present = 0;
464 int reg2_present = 0;
466 /* Parse first operand. */
467 if (REGISTER_P (*op))
468 reg1_present = 1;
469 reg1 = avr_operand (opcode, where, op, &str);
470 ++op;
472 /* Parse second operand. */
473 if (*op)
475 if (*op == ',')
476 ++op;
478 if (*op == '=')
480 reg2 = reg1;
481 reg2_present = 1;
483 else
485 if (REGISTER_P (*op))
486 reg2_present = 1;
488 str = skip_space (str);
489 if (*str++ != ',')
490 as_bad (_("`,' required"));
491 str = skip_space (str);
493 reg2 = avr_operand (opcode, where, op, &str);
497 if (reg1_present && reg2_present)
498 reg2 = (reg2 & 0xf) | ((reg2 << 5) & 0x200);
499 else if (reg2_present)
500 reg2 <<= 4;
502 if (reg1_present)
503 reg1 <<= 4;
504 bin |= reg1 | reg2;
507 /* Detect undefined combinations (like ld r31,Z+). */
508 if (!avr_opt.all_opcodes && AVR_UNDEF_P (bin))
509 as_warn (_("undefined combination of operands"));
511 if (opcode->insn_size == 2)
513 /* Warn if the previous opcode was cpse/sbic/sbis/sbrc/sbrs
514 (AVR core bug, fixed in the newer devices). */
516 if (!(avr_opt.no_skip_bug || (avr_mcu->isa & AVR_ISA_MUL))
517 && AVR_SKIP_P (prev))
518 as_warn (_("skipping two-word instruction"));
520 bfd_putl32 ((bfd_vma) bin, frag);
522 else
523 bfd_putl16 ((bfd_vma) bin, frag);
525 prev = bin;
526 *line = str;
527 return bin;
530 /* Parse one instruction operand.
531 Return operand bitmask. Also fixups can be generated. */
533 static unsigned int
534 avr_operand (opcode, where, op, line)
535 struct avr_opcodes_s *opcode;
536 int where;
537 char *op;
538 char **line;
540 expressionS op_expr;
541 unsigned int op_mask = 0;
542 char *str = skip_space (*line);
544 switch (*op)
546 /* Any register operand. */
547 case 'w':
548 case 'd':
549 case 'r':
550 case 'a':
551 case 'v':
552 if (*str == 'r' || *str == 'R')
554 char r_name[20];
556 str = extract_word (str, r_name, sizeof (r_name));
557 op_mask = 0xff;
558 if (isdigit (r_name[1]))
560 if (r_name[2] == '\0')
561 op_mask = r_name[1] - '0';
562 else if (r_name[1] != '0'
563 && isdigit (r_name[2])
564 && r_name[3] == '\0')
565 op_mask = (r_name[1] - '0') * 10 + r_name[2] - '0';
568 else
570 op_mask = avr_get_constant (str, 31);
571 str = input_line_pointer;
574 if (op_mask <= 31)
576 switch (*op)
578 case 'a':
579 if (op_mask < 16 || op_mask > 23)
580 as_bad (_("register r16-r23 required"));
581 op_mask -= 16;
582 break;
584 case 'd':
585 if (op_mask < 16)
586 as_bad (_("register number above 15 required"));
587 op_mask -= 16;
588 break;
590 case 'v':
591 if (op_mask & 1)
592 as_bad (_("even register number required"));
593 op_mask >>= 1;
594 break;
596 case 'w':
597 if ((op_mask & 1) || op_mask < 24)
598 as_bad (_("register r24, r26, r28 or r30 required"));
599 op_mask = (op_mask - 24) >> 1;
600 break;
602 break;
604 as_bad (_("register name or number from 0 to 31 required"));
605 break;
607 case 'e':
609 char c;
611 if (*str == '-')
613 str = skip_space (str + 1);
614 op_mask = 0x1002;
616 c = tolower (*str);
617 if (c == 'x')
618 op_mask |= 0x100c;
619 else if (c == 'y')
620 op_mask |= 0x8;
621 else if (c != 'z')
622 as_bad (_("pointer register (X, Y or Z) required"));
624 str = skip_space (str + 1);
625 if (*str == '+')
627 ++str;
628 if (op_mask & 2)
629 as_bad (_("cannot both predecrement and postincrement"));
630 op_mask |= 0x1001;
633 /* avr1 can do "ld r,Z" and "st Z,r" but no other pointer
634 registers, no predecrement, no postincrement. */
635 if (!avr_opt.all_opcodes && (op_mask & 0x100F)
636 && !(avr_mcu->isa & AVR_ISA_SRAM))
637 as_bad (_("addressing mode not supported"));
639 break;
641 case 'z':
642 if (*str == '-')
643 as_bad (_("can't predecrement"));
645 if (! (*str == 'z' || *str == 'Z'))
646 as_bad (_("pointer register Z required"));
648 str = skip_space (str + 1);
650 if (*str == '+')
652 ++str;
653 op_mask |= 1;
655 break;
657 case 'b':
659 char c = tolower (*str++);
661 if (c == 'y')
662 op_mask |= 0x8;
663 else if (c != 'z')
664 as_bad (_("pointer register (Y or Z) required"));
665 str = skip_space (str);
666 if (*str++ == '+')
668 unsigned int x;
669 x = avr_get_constant (str, 63);
670 str = input_line_pointer;
671 op_mask |= (x & 7) | ((x & (3 << 3)) << 7) | ((x & (1 << 5)) << 8);
674 break;
676 case 'h':
677 str = parse_exp (str, &op_expr);
678 fix_new_exp (frag_now, where, opcode->insn_size * 2,
679 &op_expr, false, BFD_RELOC_AVR_CALL);
680 break;
682 case 'L':
683 str = parse_exp (str, &op_expr);
684 fix_new_exp (frag_now, where, opcode->insn_size * 2,
685 &op_expr, true, BFD_RELOC_AVR_13_PCREL);
686 break;
688 case 'l':
689 str = parse_exp (str, &op_expr);
690 fix_new_exp (frag_now, where, opcode->insn_size * 2,
691 &op_expr, true, BFD_RELOC_AVR_7_PCREL);
692 break;
694 case 'i':
695 str = parse_exp (str, &op_expr);
696 fix_new_exp (frag_now, where + 2, opcode->insn_size * 2,
697 &op_expr, false, BFD_RELOC_16);
698 break;
700 case 'M':
702 bfd_reloc_code_real_type r_type;
704 input_line_pointer = str;
705 r_type = avr_ldi_expression (&op_expr);
706 str = input_line_pointer;
707 fix_new_exp (frag_now, where, 3,
708 &op_expr, false, r_type);
710 break;
712 case 'n':
714 unsigned int x;
716 x = ~avr_get_constant (str, 255);
717 str = input_line_pointer;
718 op_mask |= (x & 0xf) | ((x << 4) & 0xf00);
720 break;
722 case 'K':
724 unsigned int x;
726 x = avr_get_constant (str, 63);
727 str = input_line_pointer;
728 op_mask |= (x & 0xf) | ((x & 0x30) << 2);
730 break;
732 case 'S':
733 case 's':
735 unsigned int x;
737 x = avr_get_constant (str, 7);
738 str = input_line_pointer;
739 if (*op == 'S')
740 x <<= 4;
741 op_mask |= x;
743 break;
745 case 'P':
747 unsigned int x;
749 x = avr_get_constant (str, 63);
750 str = input_line_pointer;
751 op_mask |= (x & 0xf) | ((x & 0x30) << 5);
753 break;
755 case 'p':
757 unsigned int x;
759 x = avr_get_constant (str, 31);
760 str = input_line_pointer;
761 op_mask |= x << 3;
763 break;
765 case '?':
766 break;
768 default:
769 as_bad (_("unknown constraint `%c'"), *op);
772 *line = str;
773 return op_mask;
776 /* GAS will call this function for each section at the end of the assembly,
777 to permit the CPU backend to adjust the alignment of a section. */
779 valueT
780 md_section_align (seg, addr)
781 asection *seg;
782 valueT addr;
784 int align = bfd_get_section_alignment (stdoutput, seg);
785 return ((addr + (1 << align) - 1) & (-1 << align));
788 /* If you define this macro, it should return the offset between the
789 address of a PC relative fixup and the position from which the PC
790 relative adjustment should be made. On many processors, the base
791 of a PC relative instruction is the next instruction, so this
792 macro would return the length of an instruction. */
794 long
795 md_pcrel_from_section (fixp, sec)
796 fixS *fixp;
797 segT sec;
799 if (fixp->fx_addsy != (symbolS *) NULL
800 && (!S_IS_DEFINED (fixp->fx_addsy)
801 || (S_GET_SEGMENT (fixp->fx_addsy) != sec)))
802 return 0;
804 return fixp->fx_frag->fr_address + fixp->fx_where;
807 /* GAS will call this for each fixup. It should store the correct
808 value in the object file. */
811 md_apply_fix3 (fixp, valuep, seg)
812 fixS *fixp;
813 valueT *valuep;
814 segT seg;
816 unsigned char *where;
817 unsigned long insn;
818 long value;
820 if (fixp->fx_addsy == (symbolS *) NULL)
822 value = *valuep;
823 fixp->fx_done = 1;
825 else if (fixp->fx_pcrel)
827 segT s = S_GET_SEGMENT (fixp->fx_addsy);
829 if (fixp->fx_addsy && (s == seg || s == absolute_section))
831 value = S_GET_VALUE (fixp->fx_addsy) + *valuep;
832 fixp->fx_done = 1;
834 else
835 value = *valuep;
837 else
839 value = fixp->fx_offset;
841 if (fixp->fx_subsy != (symbolS *) NULL)
843 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
845 value -= S_GET_VALUE (fixp->fx_subsy);
846 fixp->fx_done = 1;
848 else
850 /* We don't actually support subtracting a symbol. */
851 as_bad_where (fixp->fx_file, fixp->fx_line,
852 _("expression too complex"));
857 switch (fixp->fx_r_type)
859 default:
860 fixp->fx_no_overflow = 1;
861 break;
862 case BFD_RELOC_AVR_7_PCREL:
863 case BFD_RELOC_AVR_13_PCREL:
864 case BFD_RELOC_32:
865 case BFD_RELOC_16:
866 case BFD_RELOC_AVR_CALL:
867 break;
870 if (fixp->fx_done)
872 /* Fetch the instruction, insert the fully resolved operand
873 value, and stuff the instruction back again. */
874 where = fixp->fx_frag->fr_literal + fixp->fx_where;
875 insn = bfd_getl16 (where);
877 switch (fixp->fx_r_type)
879 case BFD_RELOC_AVR_7_PCREL:
880 if (value & 1)
881 as_bad_where (fixp->fx_file, fixp->fx_line,
882 _("odd address operand: %ld"), value);
884 /* Instruction addresses are always right-shifted by 1. */
885 value >>= 1;
886 --value; /* Correct PC. */
888 if (value < -64 || value > 63)
889 as_bad_where (fixp->fx_file, fixp->fx_line,
890 _("operand out of range: %ld"), value);
891 value = (value << 3) & 0x3f8;
892 bfd_putl16 ((bfd_vma) (value | insn), where);
893 break;
895 case BFD_RELOC_AVR_13_PCREL:
896 if (value & 1)
897 as_bad_where (fixp->fx_file, fixp->fx_line,
898 _("odd address operand: %ld"), value);
900 /* Instruction addresses are always right-shifted by 1. */
901 value >>= 1;
902 --value; /* Correct PC. */
904 if (value < -2048 || value > 2047)
906 /* No wrap for devices with >8K of program memory. */
907 if ((avr_mcu->isa & AVR_ISA_MEGA) || avr_opt.no_wrap)
908 as_bad_where (fixp->fx_file, fixp->fx_line,
909 _("operand out of range: %ld"), value);
912 value &= 0xfff;
913 bfd_putl16 ((bfd_vma) (value | insn), where);
914 break;
916 case BFD_RELOC_32:
917 bfd_putl16 ((bfd_vma) value, where);
918 break;
920 case BFD_RELOC_16:
921 bfd_putl16 ((bfd_vma) value, where);
922 break;
924 case BFD_RELOC_AVR_16_PM:
925 bfd_putl16 ((bfd_vma) (value >> 1), where);
926 break;
928 case BFD_RELOC_AVR_LO8_LDI:
929 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value), where);
930 break;
932 case -BFD_RELOC_AVR_LO8_LDI:
933 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 16), where);
934 break;
936 case BFD_RELOC_AVR_HI8_LDI:
937 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 8), where);
938 break;
940 case -BFD_RELOC_AVR_HI8_LDI:
941 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 24), where);
942 break;
944 case BFD_RELOC_AVR_HH8_LDI:
945 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 16), where);
946 break;
948 case BFD_RELOC_AVR_LO8_LDI_NEG:
949 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value), where);
950 break;
952 case -BFD_RELOC_AVR_LO8_LDI_NEG:
953 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 16), where);
954 break;
956 case BFD_RELOC_AVR_HI8_LDI_NEG:
957 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 8), where);
958 break;
960 case -BFD_RELOC_AVR_HI8_LDI_NEG:
961 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 24), where);
962 break;
964 case BFD_RELOC_AVR_HH8_LDI_NEG:
965 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 16), where);
966 break;
968 case BFD_RELOC_AVR_LO8_LDI_PM:
969 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 1), where);
970 break;
972 case BFD_RELOC_AVR_HI8_LDI_PM:
973 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 9), where);
974 break;
976 case BFD_RELOC_AVR_HH8_LDI_PM:
977 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 17), where);
978 break;
980 case BFD_RELOC_AVR_LO8_LDI_PM_NEG:
981 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 1), where);
982 break;
984 case BFD_RELOC_AVR_HI8_LDI_PM_NEG:
985 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 9), where);
986 break;
988 case BFD_RELOC_AVR_HH8_LDI_PM_NEG:
989 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 17), where);
990 break;
992 case BFD_RELOC_AVR_CALL:
994 unsigned long x;
996 x = bfd_getl16 (where);
997 if (value & 1)
998 as_bad_where (fixp->fx_file, fixp->fx_line,
999 _("odd address operand: %ld"), value);
1000 value >>= 1;
1001 x |= ((value & 0x10000) | ((value << 3) & 0x1f00000)) >> 16;
1002 bfd_putl16 ((bfd_vma) x, where);
1003 bfd_putl16 ((bfd_vma) (value & 0xffff), where + 2);
1005 break;
1007 default:
1008 as_fatal (_("line %d: unknown relocation type: 0x%x"),
1009 fixp->fx_line, fixp->fx_r_type);
1010 break;
1013 else
1015 switch (fixp->fx_r_type)
1017 case -BFD_RELOC_AVR_HI8_LDI_NEG:
1018 case -BFD_RELOC_AVR_HI8_LDI:
1019 case -BFD_RELOC_AVR_LO8_LDI_NEG:
1020 case -BFD_RELOC_AVR_LO8_LDI:
1021 as_bad_where (fixp->fx_file, fixp->fx_line,
1022 _("only constant expression allowed"));
1023 fixp->fx_done = 1;
1024 break;
1025 default:
1026 break;
1028 fixp->fx_addnumber = value;
1030 return 0;
1033 /* A `BFD_ASSEMBLER' GAS will call this to generate a reloc. GAS
1034 will pass the resulting reloc to `bfd_install_relocation'. This
1035 currently works poorly, as `bfd_install_relocation' often does the
1036 wrong thing, and instances of `tc_gen_reloc' have been written to
1037 work around the problems, which in turns makes it difficult to fix
1038 `bfd_install_relocation'. */
1040 /* If while processing a fixup, a reloc really needs to be created
1041 then it is done here. */
1043 arelent *
1044 tc_gen_reloc (seg, fixp)
1045 asection *seg ATTRIBUTE_UNUSED;
1046 fixS *fixp;
1048 arelent *reloc;
1050 reloc = (arelent *) xmalloc (sizeof (arelent));
1052 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
1053 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
1055 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
1056 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
1057 if (reloc->howto == (reloc_howto_type *) NULL)
1059 as_bad_where (fixp->fx_file, fixp->fx_line,
1060 _("reloc %d not supported by object file format"),
1061 (int) fixp->fx_r_type);
1062 return NULL;
1065 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
1066 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
1067 reloc->address = fixp->fx_offset;
1069 reloc->addend = fixp->fx_offset;
1071 return reloc;
1074 void
1075 md_assemble (str)
1076 char *str;
1078 struct avr_opcodes_s *opcode;
1079 char op[11];
1081 str = skip_space (extract_word (str, op, sizeof (op)));
1083 if (!op[0])
1084 as_bad (_("can't find opcode "));
1086 opcode = (struct avr_opcodes_s *) hash_find (avr_hash, op);
1088 if (opcode == NULL)
1090 as_bad (_("unknown opcode `%s'"), op);
1091 return;
1094 /* Special case for opcodes with optional operands (lpm, elpm) -
1095 version with operands exists in avr_opcodes[] in the next entry. */
1097 if (*str && *opcode->constraints == '?')
1098 ++opcode;
1100 if (!avr_opt.all_opcodes && (opcode->isa & avr_mcu->isa) != opcode->isa)
1101 as_bad (_("illegal opcode %s for mcu %s"), opcode->name, avr_mcu->name);
1103 /* We used to set input_line_pointer to the result of get_operands,
1104 but that is wrong. Our caller assumes we don't change it. */
1106 char *t = input_line_pointer;
1107 avr_operands (opcode, &str);
1108 if (*skip_space (str))
1109 as_bad (_("garbage at end of line"));
1110 input_line_pointer = t;
1114 /* Parse ordinary expression. */
1116 static char *
1117 parse_exp (s, op)
1118 char *s;
1119 expressionS *op;
1121 input_line_pointer = s;
1122 expression (op);
1123 if (op->X_op == O_absent)
1124 as_bad (_("missing operand"));
1125 return input_line_pointer;
1128 /* Parse special expressions (needed for LDI command):
1129 xx8 (address)
1130 xx8 (-address)
1131 pm_xx8 (address)
1132 pm_xx8 (-address)
1133 where xx is: hh, hi, lo. */
1135 static bfd_reloc_code_real_type
1136 avr_ldi_expression (exp)
1137 expressionS *exp;
1139 char *str = input_line_pointer;
1140 char *tmp;
1141 char op[8];
1142 int mod;
1143 tmp = str;
1145 str = extract_word (str, op, sizeof (op));
1147 if (op[0])
1149 mod = (int) hash_find (avr_mod_hash, op);
1151 if (mod)
1153 int closes = 0;
1155 mod -= 10;
1156 str = skip_space (str);
1158 if (*str == '(')
1160 int neg_p = 0;
1162 ++str;
1164 if (strncmp ("pm(", str, 3) == 0
1165 || strncmp ("-(pm(", str, 5) == 0)
1167 if (HAVE_PM_P (mod))
1169 ++mod;
1170 ++closes;
1172 else
1173 as_bad (_("illegal expression"));
1175 if (*str == '-')
1177 neg_p = 1;
1178 ++closes;
1179 str += 5;
1181 else
1182 str += 3;
1185 if (*str == '-' && *(str + 1) == '(')
1187 neg_p ^= 1;
1188 ++closes;
1189 str += 2;
1192 input_line_pointer = str;
1193 expression (exp);
1197 if (*input_line_pointer != ')')
1199 as_bad (_("`)' required"));
1200 break;
1202 input_line_pointer++;
1204 while (closes--);
1206 return neg_p ? EXP_MOD_NEG_RELOC (mod) : EXP_MOD_RELOC (mod);
1211 input_line_pointer = tmp;
1212 expression (exp);
1214 /* Warn about expressions that fail to use lo8 (). */
1215 if (exp->X_op == O_constant)
1217 int x = exp->X_add_number;
1218 if (x < -255 || x > 255)
1219 as_warn (_("constant out of 8-bit range: %d"), x);
1221 else
1222 as_warn (_("expression possibly out of 8-bit range"));
1224 return BFD_RELOC_AVR_LO8_LDI;
1227 /* Flag to pass `pm' mode between `avr_parse_cons_expression' and
1228 `avr_cons_fix_new'. */
1229 static int exp_mod_pm = 0;
1231 /* Parse special CONS expression: pm (expression)
1232 which is used for addressing to a program memory.
1233 Relocation: BFD_RELOC_AVR_16_PM. */
1235 void
1236 avr_parse_cons_expression (exp, nbytes)
1237 expressionS *exp;
1238 int nbytes;
1240 char *tmp;
1242 exp_mod_pm = 0;
1244 tmp = input_line_pointer = skip_space (input_line_pointer);
1246 if (nbytes == 2)
1248 char *pm_name = "pm";
1249 int len = strlen (pm_name);
1251 if (strncasecmp (input_line_pointer, pm_name, len) == 0)
1253 input_line_pointer = skip_space (input_line_pointer + len);
1255 if (*input_line_pointer == '(')
1257 input_line_pointer = skip_space (input_line_pointer + 1);
1258 exp_mod_pm = 1;
1259 expression (exp);
1261 if (*input_line_pointer == ')')
1262 ++input_line_pointer;
1263 else
1265 as_bad (_("`)' required"));
1266 exp_mod_pm = 0;
1269 return;
1272 input_line_pointer = tmp;
1276 expression (exp);
1279 void
1280 avr_cons_fix_new (frag, where, nbytes, exp)
1281 fragS *frag;
1282 int where;
1283 int nbytes;
1284 expressionS *exp;
1286 if (exp_mod_pm == 0)
1288 if (nbytes == 2)
1289 fix_new_exp (frag, where, nbytes, exp, false, BFD_RELOC_16);
1290 else if (nbytes == 4)
1291 fix_new_exp (frag, where, nbytes, exp, false, BFD_RELOC_32);
1292 else
1293 as_bad (_("illegal %srelocation size: %d"), "", nbytes);
1295 else
1297 if (nbytes == 2)
1298 fix_new_exp (frag, where, nbytes, exp, false, BFD_RELOC_AVR_16_PM);
1299 else
1300 as_bad (_("illegal %srelocation size: %d"), "`pm' ", nbytes);
1301 exp_mod_pm = 0;