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