]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-avr.c
Fix formatting.
[thirdparty/binutils-gdb.git] / gas / config / tc-avr.c
CommitLineData
adde6300
AM
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
1188e082
DC
28struct 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
40struct avr_opcodes_s avr_opcodes[] =
41{
42 #include "opcode/avr.h"
43 {NULL, NULL, 0, 0, 0}
44};
45
46
adde6300
AM
47const char comment_chars[] = ";";
48const char line_comment_chars[] = "#";
49const char line_separator_chars[] = "$";
50
adde6300
AM
51const char *md_shortopts = "m:";
52struct mcu_type_s
53{
54 char *name;
55 int isa;
56 int mach;
57};
58
59static struct mcu_type_s mcu_types[] =
60{
b170af93
DC
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},
adde6300 65 {"at90s1200", AVR_ISA_1200, bfd_mach_avr1},
b170af93
DC
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},
adde6300
AM
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},
b170af93
DC
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},
adde6300
AM
88 {NULL, 0, 0}
89};
90
91
92/* Current MCU type. */
93static struct mcu_type_s default_mcu = {"avr2", AVR_ISA_2xxx,bfd_mach_avr2};
94static struct mcu_type_s *avr_mcu = &default_mcu;
95
96const char EXP_CHARS[] = "eE";
97const char FLT_CHARS[] = "dD";
98static void avr_set_arch (int dummy);
99
100/* The target specific pseudo-ops which we support. */
101const 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))
adde6300
AM
108
109static char * skip_space (char * s);
110static char * extract_word (char *from, char *to, int limit);
111static unsigned int avr_operand (struct avr_opcodes_s *opcode,
112 int where, char *op, char **line);
113static unsigned int avr_operands (struct avr_opcodes_s *opcode, char **line);
df136245 114static unsigned int avr_get_constant (char * str, int max);
adde6300
AM
115static char *parse_exp (char *s, expressionS * op);
116static bfd_reloc_code_real_type avr_ldi_expression (expressionS *exp);
117long md_pcrel_from_section PARAMS ((fixS *, segT));
118
b170af93 119
adde6300
AM
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
125struct 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
133static 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. */
145static struct hash_control *avr_hash;
146
147/* Reloc modifiers hash control (hh8,hi8,lo8,pm_xx). */
148static struct hash_control *avr_mod_hash;
149
150#define OPTION_MMCU (OPTION_MD_BASE + 1)
151
152struct option md_longopts[] = {
153 {"mmcu", required_argument, NULL, 'm'},
154 {NULL, no_argument, NULL, 0}
155};
156size_t md_longopts_size = sizeof(md_longopts);
157
158static inline char *
159skip_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. */
168static char *
169extract_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
189int
190md_estimate_size_before_relax (fragp, seg)
df136245
DC
191 fragS *fragp ATTRIBUTE_UNUSED;
192 asection *seg ATTRIBUTE_UNUSED;
adde6300
AM
193{
194 abort ();
195 return 0;
196}
197
198void
199md_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
214static void
215avr_set_arch (dummy)
df136245 216 int dummy ATTRIBUTE_UNUSED;
adde6300
AM
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
225int
226md_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
256symbolS *
b170af93 257md_undefined_symbol(name)
df136245 258 char *name ATTRIBUTE_UNUSED;
adde6300
AM
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. */
267char *
268md_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
305void
306md_convert_frag (abfd, sec, fragP)
df136245
DC
307 bfd *abfd ATTRIBUTE_UNUSED;
308 asection *sec ATTRIBUTE_UNUSED;
309 fragS *fragP ATTRIBUTE_UNUSED;
adde6300
AM
310{
311 abort ();
312}
313
314
315void
316md_begin ()
317{
df136245 318 unsigned int i;
adde6300
AM
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));
b170af93 333
adde6300
AM
334 bfd_set_arch_mach (stdoutput, TARGET_ARCH, avr_mcu->mach);
335}
336
337
df136245
DC
338/* Resolve STR as a constant expression and return the result.
339 If result greater than MAX then error. */
340
341static unsigned int
342avr_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
adde6300
AM
363static unsigned int
364avr_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;
b170af93 373 static unsigned int prev = 0; /* previous opcode */
adde6300
AM
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 }
b170af93
DC
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
adde6300
AM
429 if (opcode->insn_size == 2)
430 {
b170af93
DC
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
adde6300
AM
438 bfd_putl32 ((bfd_vma)bin, frag);
439 }
440 else
b170af93
DC
441 bfd_putl16 ((bfd_vma)bin, frag);
442
443 prev = bin;
adde6300
AM
444 *line = str;
445 return bin;
446}
447
adde6300 448
df136245
DC
449/* Parse one instruction operand.
450 Returns operand bitmask. Also fixups can be generated. */
451
adde6300
AM
452static unsigned int
453avr_operand (opcode, where, op, line)
454 struct avr_opcodes_s *opcode;
455 int where;
456 char *op;
457 char **line;
458{
adde6300 459 expressionS op_expr;
df136245
DC
460 unsigned int op_mask = 0;
461 char *str = skip_space (*line);
adde6300 462
adde6300
AM
463 switch (*op)
464 {
465 /* Any register operand. */
466 case 'w':
467 case 'd':
468 case 'r':
b170af93
DC
469 case 'a':
470 case 'v':
adde6300 471 {
b170af93
DC
472 op_mask = -1;
473
df136245
DC
474 if (*str == 'r' || *str == 'R')
475 {
476 char r_name[20];
477
478 str = extract_word (str, r_name, sizeof (r_name));
b170af93
DC
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 {
df136245
DC
491 op_mask = avr_get_constant (str, 31);
492 str = input_line_pointer;
b170af93
DC
493 }
494
df136245 495 if (op_mask <= 31)
adde6300 496 {
b170af93 497 switch (*op)
adde6300 498 {
b170af93
DC
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;
adde6300
AM
522 break;
523 }
b170af93 524 break;
adde6300 525 }
df136245 526 as_bad (_ ("register name or number from 0 to 31 required"));
adde6300
AM
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 }
e38c9cc2 554
1188e082 555 /* avr1 can do "ld r,Z" and "st Z,r" but no other pointer
e38c9cc2
DC
556 registers, no predecrement, no postincrement. */
557
1188e082
DC
558 if ((op_mask & 0x100F) && !(avr_mcu->isa & AVR_ISA_SRAM))
559 as_bad (_ ("addressing mode not supported"));
adde6300
AM
560 }
561 break;
562
b170af93
DC
563 case 'z':
564 {
565 if (*str == '-')
566 as_bad (_ ("can't predecrement"));
567
568 if (! (*str == 'z' || *str == 'Z'))
569 as_bad (_ ("pointer register Z required"));
570
571 str = skip_space (str + 1);
572 if (*str == '+')
573 {
574 ++str;
575 op_mask |= 1;
576 }
577 }
578 break;
579
adde6300
AM
580 case 'b':
581 {
582 char c = tolower (*str++);
583 if (c == 'y')
584 op_mask |= 0x8;
585 else if (c != 'z')
586 as_bad (_ ("pointer register (Y or Z) required"));
587 str = skip_space (str);
588 if (*str++ == '+')
589 {
590 unsigned int x;
591 x = avr_get_constant (str, 63);
592 str = input_line_pointer;
593 op_mask |= (x & 7) | ((x & (3 << 3)) << 7) | ((x & (1 << 5)) << 8);
594 }
595 }
596 break;
597
598 case 'h':
599 {
600 str = parse_exp (str, &op_expr);
601 fix_new_exp (frag_now, where, opcode->insn_size * 2,
602 &op_expr, false, BFD_RELOC_AVR_CALL);
603
604 }
605 break;
606
607 case 'L':
608 {
609 str = parse_exp (str, &op_expr);
610 fix_new_exp (frag_now, where, opcode->insn_size * 2,
611 &op_expr, true, BFD_RELOC_AVR_13_PCREL);
612
613 }
614 break;
615
616 case 'l':
617 {
618 str = parse_exp (str, &op_expr);
619 fix_new_exp (frag_now, where, opcode->insn_size * 2,
620 &op_expr, true, BFD_RELOC_AVR_7_PCREL);
621
622 }
623 break;
624
625 case 'i':
626 {
627 str = parse_exp (str, &op_expr);
628 fix_new_exp (frag_now, where+2, opcode->insn_size * 2,
629 &op_expr, false, BFD_RELOC_16);
630
631 }
632 break;
633
634 case 'M':
635 {
636 bfd_reloc_code_real_type r_type;
637 input_line_pointer = str;
638 r_type = avr_ldi_expression (&op_expr);
639 str = input_line_pointer;
640 fix_new_exp (frag_now, where, 3,
641 &op_expr, false, r_type);
642 }
643 break;
644
645 case 'n':
646 {
647 unsigned int x;
648 x = ~avr_get_constant (str, 255);
649 str = input_line_pointer;
650 op_mask |= (x & 0xf) | ((x << 4) & 0xf00);
651 }
652 break;
653
654 case 'K':
655 {
656 unsigned int x;
657 x = avr_get_constant (str, 63);
658 str = input_line_pointer;
659 op_mask |= (x & 0xf) | ((x & 0x30) << 2);
660 }
661 break;
662
663 case 'S':
664 case 's':
665 {
666 unsigned int x;
667 x = avr_get_constant (str, 7);
668 str = input_line_pointer;
669 if (*op == 'S')
670 x <<= 4;
671 op_mask |= x;
672 }
673 break;
674
675 case 'P':
676 {
677 unsigned int x;
678 x = avr_get_constant (str, 63);
679 str = input_line_pointer;
680 op_mask |= (x & 0xf) | ((x & 0x30) << 5);
681 }
682 break;
683
684 case 'p':
685 {
686 unsigned int x;
687 x = avr_get_constant (str, 31);
688 str = input_line_pointer;
689 op_mask |= x << 3;
690 }
691 break;
1188e082
DC
692 case '?':
693 break;
adde6300
AM
694 default:
695 as_bad (_ ("unknown constraint `%c'"), *op);
696 }
697 *line = str;
698 return op_mask;
699}
700
701/* GAS will call this function for each section at the end of the assembly,
702 to permit the CPU backend to adjust the alignment of a section. */
703valueT
704md_section_align (seg, addr)
705 asection *seg;
706 valueT addr;
707{
708 int align = bfd_get_section_alignment (stdoutput, seg);
709 return ((addr + (1 << align) - 1) & (-1 << align));
710}
711
712/* If you define this macro, it should return the offset between the
713 address of a PC relative fixup and the position from which the PC
714 relative adjustment should be made. On many processors, the base
715 of a PC relative instruction is the next instruction, so this
716 macro would return the length of an instruction. */
717long
718md_pcrel_from_section (fixp, sec)
719 fixS *fixp;
720 segT sec;
721{
722 if (fixp->fx_addsy != (symbolS *)NULL
723 && (!S_IS_DEFINED (fixp->fx_addsy)
724 || (S_GET_SEGMENT (fixp->fx_addsy) != sec)))
725 return 0;
726 return fixp->fx_frag->fr_address + fixp->fx_where;
727}
728
729/* GAS will call this for each fixup. It should store the correct
730 value in the object file. */
731int
732md_apply_fix3 (fixp, valuep, seg)
733 fixS *fixp;
734 valueT *valuep;
735 segT seg;
736{
737 unsigned char *where;
738 unsigned long insn;
739 long value;
740
741 if (fixp->fx_addsy == (symbolS *) NULL)
742 {
743 value = *valuep;
744 fixp->fx_done = 1;
745 }
746 else if (fixp->fx_pcrel)
747 {
748 segT s = S_GET_SEGMENT (fixp->fx_addsy);
749 if (fixp->fx_addsy && (s == seg || s == absolute_section))
750 {
751 value = S_GET_VALUE (fixp->fx_addsy) + *valuep;
752 fixp->fx_done = 1;
753 }
754 else
755 value = *valuep;
756 }
757 else
758 {
759 value = fixp->fx_offset;
760 if (fixp->fx_subsy != (symbolS *) NULL)
761 {
762 if (S_GET_SEGMENT (fixp->fx_subsy) == absolute_section)
763 {
764 value -= S_GET_VALUE (fixp->fx_subsy);
765 fixp->fx_done = 1;
766 }
767 else
768 {
769 /* We don't actually support subtracting a symbol. */
770 as_bad_where (fixp->fx_file, fixp->fx_line,
771 _ ("expression too complex"));
772 }
773 }
774 }
775 switch (fixp->fx_r_type)
776 {
777 default:
778 fixp->fx_no_overflow = 1;
779 break;
780 case BFD_RELOC_AVR_7_PCREL:
781 case BFD_RELOC_AVR_13_PCREL:
782 case BFD_RELOC_32:
783 case BFD_RELOC_16:
784 case BFD_RELOC_AVR_CALL:
785 break;
786 }
787
788 if (fixp->fx_done)
789 {
790 /* Fetch the instruction, insert the fully resolved operand
791 value, and stuff the instruction back again. */
792 where = fixp->fx_frag->fr_literal + fixp->fx_where;
793 insn = bfd_getl16 (where);
794
795 switch (fixp->fx_r_type)
796 {
797 case BFD_RELOC_AVR_7_PCREL:
798 if (value & 1)
799 as_bad_where (fixp->fx_file, fixp->fx_line,
800 _("odd address operand: %ld"), value);
801 /* Instruction addresses are always right-shifted by 1. */
802 value >>= 1;
803 --value; /* Correct PC. */
804 if (value < -64 || value > 63)
805 as_bad_where (fixp->fx_file, fixp->fx_line,
806 _("operand out of range: %ld"), value);
807 value = (value << 3) & 0x3f8;
808 bfd_putl16 ((bfd_vma) (value | insn), where);
809 break;
810
811 case BFD_RELOC_AVR_13_PCREL:
812 if (value & 1)
813 as_bad_where (fixp->fx_file, fixp->fx_line,
814 _("odd address operand: %ld"), value);
815 /* Instruction addresses are always right-shifted by 1. */
816 value >>= 1;
817 --value; /* Correct PC. */
adde6300
AM
818
819 if (value < -2048 || value > 2047)
820 {
b170af93 821 if (avr_mcu->isa & AVR_ISA_WRAP)
adde6300
AM
822 {
823 if (value > 2047)
824 value -= 4096;
825 else
826 value += 4096;
827 }
828 else
829 as_bad_where (fixp->fx_file, fixp->fx_line,
830 _("operand out of range: %ld"), value);
831 }
832
833 value &= 0xfff;
834 bfd_putl16 ((bfd_vma) (value | insn), where);
835 break;
836
837 case BFD_RELOC_32:
838 bfd_putl16 ((bfd_vma) value, where);
839 break;
840
841 case BFD_RELOC_16:
842 bfd_putl16 ((bfd_vma) value, where);
843 break;
844
845 case BFD_RELOC_AVR_16_PM:
846 bfd_putl16 ((bfd_vma) (value>>1), where);
847 break;
848
849 case BFD_RELOC_AVR_LO8_LDI:
850 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value), where);
851 break;
852
853 case -BFD_RELOC_AVR_LO8_LDI:
854 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 16), where);
855 break;
856
857 case BFD_RELOC_AVR_HI8_LDI:
858 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 8), where);
859 break;
860
861 case -BFD_RELOC_AVR_HI8_LDI:
862 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 24), where);
863 break;
864
865 case BFD_RELOC_AVR_HH8_LDI:
866 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 16), where);
867 break;
868
869 case BFD_RELOC_AVR_LO8_LDI_NEG:
870 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value), where);
871 break;
872
873 case -BFD_RELOC_AVR_LO8_LDI_NEG:
874 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 16), where);
875 break;
876
877 case BFD_RELOC_AVR_HI8_LDI_NEG:
878 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 8), where);
879 break;
880
881 case -BFD_RELOC_AVR_HI8_LDI_NEG:
882 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 24), where);
883 break;
884
885 case BFD_RELOC_AVR_HH8_LDI_NEG:
886 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 16), where);
887 break;
888
889 case BFD_RELOC_AVR_LO8_LDI_PM:
890 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 1), where);
891 break;
892
893 case BFD_RELOC_AVR_HI8_LDI_PM:
894 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 9), where);
895 break;
896
897 case BFD_RELOC_AVR_HH8_LDI_PM:
898 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (value >> 17), where);
899 break;
900
901 case BFD_RELOC_AVR_LO8_LDI_PM_NEG:
902 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 1), where);
903 break;
904
905 case BFD_RELOC_AVR_HI8_LDI_PM_NEG:
906 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 9), where);
907 break;
908
909 case BFD_RELOC_AVR_HH8_LDI_PM_NEG:
910 bfd_putl16 ((bfd_vma) insn | LDI_IMMEDIATE (-value >> 17), where);
911 break;
912
913 case BFD_RELOC_AVR_CALL:
914 {
915 unsigned long x;
916 x = bfd_getl16 (where);
917 if (value & 1)
918 as_bad_where (fixp->fx_file, fixp->fx_line,
919 _("odd address operand: %ld"), value);
920 value >>= 1;
921 x |= ((value & 0x10000) | ((value << 3) & 0x1f00000)) >> 16;
922 bfd_putl16 ((bfd_vma) x, where);
923 bfd_putl16 ((bfd_vma) (value & 0xffff), where+2);
924 }
925 break;
926
927 default:
928 as_fatal ( _("line %d: unknown relocation type: 0x%x"),
929 fixp->fx_line, fixp->fx_r_type);
930 break;
931 }
932 }
933 else
934 {
935 switch (fixp->fx_r_type)
936 {
937 case -BFD_RELOC_AVR_HI8_LDI_NEG:
938 case -BFD_RELOC_AVR_HI8_LDI:
939 case -BFD_RELOC_AVR_LO8_LDI_NEG:
940 case -BFD_RELOC_AVR_LO8_LDI:
941 as_bad_where (fixp->fx_file, fixp->fx_line,
942 _("only constant expression allowed"));
943 fixp->fx_done = 1;
944 break;
945 default:
946 break;
947 }
948 fixp->fx_addnumber = value;
949 }
950 return 0;
951}
952
953
954/* A `BFD_ASSEMBLER' GAS will call this to generate a reloc. GAS
955 will pass the resulting reloc to `bfd_install_relocation'. This
956 currently works poorly, as `bfd_install_relocation' often does the
957 wrong thing, and instances of `tc_gen_reloc' have been written to
958 work around the problems, which in turns makes it difficult to fix
959 `bfd_install_relocation'. */
960
961/* If while processing a fixup, a reloc really needs to be created
962 then it is done here. */
963
964arelent *
965tc_gen_reloc (seg, fixp)
df136245 966 asection *seg ATTRIBUTE_UNUSED;
adde6300
AM
967 fixS *fixp;
968{
969 arelent *reloc;
970
971 reloc = (arelent *) xmalloc (sizeof (arelent));
972
973 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
974 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
975
976 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
977 reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
978 if (reloc->howto == (reloc_howto_type *) NULL)
979 {
980 as_bad_where (fixp->fx_file, fixp->fx_line,
981 _("reloc %d not supported by object file format"),
982 (int)fixp->fx_r_type);
983 return NULL;
984 }
985
986 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
987 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
988 reloc->address = fixp->fx_offset;
989
990 reloc->addend = fixp->fx_offset;
991
992 return reloc;
993}
994
995
996void
997md_assemble (str)
998 char *str;
999{
1000 struct avr_opcodes_s * opcode;
1001 char op[11];
1002
df136245 1003 str = skip_space (extract_word (str, op, sizeof(op)));
adde6300
AM
1004
1005 if (!op[0])
1006 as_bad (_ ("can't find opcode "));
1007
1008 opcode = (struct avr_opcodes_s *) hash_find (avr_hash, op);
1009
1010 if (opcode == NULL)
1011 {
1012 as_bad (_ ("unknown opcode `%s'"), op);
1013 return;
1014 }
1015
b170af93 1016 /* Special case for opcodes with optional operands (lpm, elpm) -
1188e082 1017 version with operands exists in avr_opcodes[] in the next entry. */
b170af93 1018
1188e082
DC
1019 if (*str && *opcode->constraints == '?')
1020 ++opcode;
b170af93 1021
adde6300
AM
1022 if ((opcode->isa & avr_mcu->isa) != opcode->isa)
1023 as_bad (_ ("illegal opcode %s for mcu %s"), opcode->name, avr_mcu->name);
1024
1025 /* We used to set input_line_pointer to the result of get_operands,
1026 but that is wrong. Our caller assumes we don't change it. */
1027 {
1028 char *t = input_line_pointer;
1029 avr_operands (opcode, &str);
b170af93 1030 if (*skip_space (str))
adde6300
AM
1031 as_bad (_ ("garbage at end of line"));
1032 input_line_pointer = t;
1033 }
1034}
1035
1036/* Parse ordinary expression. */
1037static char *
1038parse_exp (s, op)
1039 char *s;
1040 expressionS * op;
1041{
1042 input_line_pointer = s;
1043 expression (op);
1044 if (op->X_op == O_absent)
1045 as_bad (_("missing operand"));
1046 return input_line_pointer;
1047}
1048
1049
1050/* Parse special expressions (needed for LDI command):
1051 xx8 (address)
1052 xx8 (-address)
1053 pm_xx8 (address)
1054 pm_xx8 (-address)
1055 where xx is: hh, hi, lo
1056*/
1057static bfd_reloc_code_real_type
1058avr_ldi_expression (exp)
1059 expressionS *exp;
1060{
1061 char *str = input_line_pointer;
1062 char *tmp;
1063 char op[8];
1064 int mod;
1065 tmp = str;
1066
1067 str = extract_word (str, op, sizeof (op));
1068 if (op[0])
1069 {
1070 mod = (int) hash_find (avr_mod_hash, op);
1071 if (mod)
1072 {
1073 int closes = 0;
1074 mod -= 10;
1075 str = skip_space (str);
1076 if (*str == '(')
1077 {
1078 int neg_p = 0;
1079 ++str;
1080 if (strncmp ("pm(", str, 3) == 0
1081 || strncmp ("-(pm(", str, 5) == 0)
1082 {
1083 if (HAVE_PM_P(mod))
1084 {
1085 ++mod;
1086 ++closes;
1087 }
1088 else
1089 as_bad (_ ("illegal expression"));
1090 if (*str == '-')
1091 {
1092 neg_p = 1;
1093 ++closes;
1094 str += 5;
1095 }
1096 else
1097 str += 3;
1098 }
1099 if (*str == '-' && *(str + 1) == '(')
1100 {
1101 neg_p ^= 1;
1102 ++closes;
1103 str += 2;
1104 }
1105 input_line_pointer = str;
1106 expression (exp);
1107 do
1108 {
1109 if (*input_line_pointer != ')')
1110 {
1111 as_bad (_ ("`)' required"));
1112 break;
1113 }
1114 input_line_pointer++;
1115 }
1116 while (closes--);
1117 return neg_p ? EXP_MOD_NEG_RELOC (mod) : EXP_MOD_RELOC (mod);
1118 }
1119 }
1120 }
1121 input_line_pointer = tmp;
1122 expression (exp);
1123 return BFD_RELOC_AVR_LO8_LDI;
1124}
1125
1126/* Flag to pass `pm' mode between `avr_parse_cons_expression' and
1127 `avr_cons_fix_new' */
1128static int exp_mod_pm = 0;
1129
1130/* Parse special CONS expression: pm (expression)
1131 which is used for addressing to a program memory.
1132 Relocation: BFD_RELOC_AVR_16_PM */
1133void
1134avr_parse_cons_expression (exp, nbytes)
1135 expressionS *exp;
1136 int nbytes;
1137{
1138 char * tmp;
1139
1140 exp_mod_pm = 0;
1141
1142 tmp = input_line_pointer = skip_space (input_line_pointer);
1143
1144 if (nbytes == 2)
1145 {
1146 char * pm_name = "pm";
1147 int len = strlen (pm_name);
1148 if (strncasecmp (input_line_pointer, pm_name, len) == 0)
1149 {
1150 input_line_pointer = skip_space (input_line_pointer + len);
1151 if (*input_line_pointer == '(')
1152 {
1153 input_line_pointer = skip_space (input_line_pointer + 1);
1154 exp_mod_pm = 1;
1155 expression (exp);
1156 if (*input_line_pointer == ')')
1157 ++input_line_pointer;
1158 else
1159 {
1160 as_bad (_ ("`)' required"));
1161 exp_mod_pm = 0;
1162 }
1163 return;
1164 }
1165 input_line_pointer = tmp;
1166 }
1167 }
1168 expression (exp);
1169}
1170
1171void
1172avr_cons_fix_new(frag, where, nbytes, exp)
1173 fragS *frag;
1174 int where;
1175 int nbytes;
1176 expressionS *exp;
1177{
1178 if (exp_mod_pm == 0)
1179 {
1180 if (nbytes == 2)
1181 fix_new_exp (frag, where, nbytes, exp, false, BFD_RELOC_16);
1182 else if (nbytes == 4)
1183 fix_new_exp (frag, where, nbytes, exp, false, BFD_RELOC_32);
1184 else
1185 as_bad (_ ("illegal %srelocation size: %d"), "", nbytes);
1186 }
1187 else
1188 {
1189 if (nbytes == 2)
1190 fix_new_exp (frag, where, nbytes, exp, false, BFD_RELOC_AVR_16_PM);
1191 else
1192 as_bad (_ ("illegal %srelocation size: %d"), "`pm' ", nbytes);
1193 exp_mod_pm = 0;
1194 }
1195}