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