1 /* tc-msp430.c -- Assembler code for the Texas Instruments MSP430
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
5 Contributed by Dmitry Diky <diwil@mail.ru>
7 This file is part of GAS, the GNU Assembler.
9 GAS is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GAS is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GAS; see the file COPYING. If not, write to
21 the Free Software Foundation, 51 Franklin Street - Fifth Floor,
22 Boston, MA 02110-1301, USA. */
26 #define PUSH_1X_WORKAROUND
29 #include "opcode/msp430.h"
30 #include "safe-ctype.h"
31 #include "dwarf2dbg.h"
34 We will disable polymorphs by default because it is dangerous.
35 The potential problem here is the following: assume we got the
40 jump subroutine ; external symbol
45 In case of assembly time relaxation we'll get:
46 0: jmp .l1 <.text +0x08> (reloc deleted)
53 If the 'subroutine' wiys thin +-1024 bytes range then linker
60 8: ret ; 'jmp .text +0x08' will land here. WRONG!!!
63 The workaround is the following:
64 1. Declare global var enable_polymorphs which set to 1 via option -mP.
65 2. Declare global var enable_relax which set to 1 via option -mQ.
67 If polymorphs are enabled, and relax isn't, treat all jumps as long jumps,
68 do not delete any relocs and leave them for linker.
70 If relax is enabled, relax at assembly time and kill relocs as necessary.
73 int msp430_enable_relax
;
74 int msp430_enable_polys
;
76 /* GCC uses the some condition codes which we'll
77 implement as new polymorph instructions.
79 COND EXPL SHORT JUMP LONG JUMP
80 ===============================================
81 eq == jeq jne +4; br lab
82 ne != jne jeq +4; br lab
84 ltn honours no-overflow flag
85 ltn < jn jn +2; jmp +4; br lab
87 lt < jl jge +4; br lab
88 ltu < jlo lhs +4; br lab
94 ge >= jge jl +4; br lab
95 geu >= jhs jlo +4; br lab
96 ===============================================
98 Therefore, new opcodes are (BranchEQ -> beq; and so on...)
99 beq,bne,blt,bltn,bltu,bge,bgeu
100 'u' means unsigned compares
102 Also, we add 'jump' instruction:
103 jump UNCOND -> jmp br lab
105 They will have fmt == 4, and insn_opnumb == number of instruction. */
110 int index
; /* Corresponding insn_opnumb. */
111 int sop
; /* Opcode if jump length is short. */
112 long lpos
; /* Label position. */
113 long lop0
; /* Opcode 1 _word_ (16 bits). */
114 long lop1
; /* Opcode second word. */
115 long lop2
; /* Opcode third word. */
118 #define MSP430_RLC(n,i,sop,o1) \
119 {#n, i, sop, 2, (o1 + 2), 0x4010, 0}
121 static struct rcodes_s msp430_rcodes
[] =
123 MSP430_RLC (beq
, 0, 0x2400, 0x2000),
124 MSP430_RLC (bne
, 1, 0x2000, 0x2400),
125 MSP430_RLC (blt
, 2, 0x3800, 0x3400),
126 MSP430_RLC (bltu
, 3, 0x2800, 0x2c00),
127 MSP430_RLC (bge
, 4, 0x3400, 0x3800),
128 MSP430_RLC (bgeu
, 5, 0x2c00, 0x2800),
129 {"bltn", 6, 0x3000, 3, 0x3000 + 1, 0x3c00 + 2,0x4010},
130 {"jump", 7, 0x3c00, 1, 0x4010, 0, 0},
136 /* More difficult than above and they have format 5.
139 =================================================================
140 gt > jeq +2; jge label jeq +6; jl +4; br label
141 gtu > jeq +2; jhs label jeq +6; jlo +4; br label
142 leu <= jeq label; jlo label jeq +2; jhs +4; br label
143 le <= jeq label; jl label jeq +2; jge +4; br label
144 ================================================================= */
149 int index
; /* Corresponding insn_opnumb. */
150 int tlab
; /* Number of labels in short mode. */
151 int op0
; /* Opcode for first word of short jump. */
152 int op1
; /* Opcode for second word of short jump. */
153 int lop0
; /* Opcodes for long jump mode. */
158 static struct hcodes_s msp430_hcodes
[] =
160 {"bgt", 0, 1, 0x2401, 0x3400, 0x2403, 0x3802, 0x4010 },
161 {"bgtu", 1, 1, 0x2401, 0x2c00, 0x2403, 0x2802, 0x4010 },
162 {"bleu", 2, 2, 0x2400, 0x2800, 0x2401, 0x2c02, 0x4010 },
163 {"ble", 3, 2, 0x2400, 0x3800, 0x2401, 0x3402, 0x4010 },
167 const char comment_chars
[] = ";";
168 const char line_comment_chars
[] = "#";
169 const char line_separator_chars
[] = "{";
170 const char EXP_CHARS
[] = "eE";
171 const char FLT_CHARS
[] = "dD";
173 /* Handle long expressions. */
174 extern LITTLENUM_TYPE generic_bignum
[];
176 static struct hash_control
*msp430_hash
;
179 #define STATE_UNCOND_BRANCH 1 /* jump */
180 #define STATE_NOOV_BRANCH 3 /* bltn */
181 #define STATE_SIMPLE_BRANCH 2 /* bne, beq, etc... */
182 #define STATE_EMUL_BRANCH 4
191 #define STATE_BITS10 1 /* wild guess. short jump */
192 #define STATE_WORD 2 /* 2 bytes pc rel. addr. more */
193 #define STATE_UNDEF 3 /* cannot handle this yet. convert to word mode */
195 #define ENCODE_RELAX(what,length) (((what) << 2) + (length))
196 #define RELAX_STATE(s) ((s) & 3)
197 #define RELAX_LEN(s) ((s) >> 2)
198 #define RELAX_NEXT(a,b) ENCODE_RELAX (a, b + 1)
200 relax_typeS md_relax_table
[] =
208 /* Unconditional jump. */
210 {1024, -1024, CNRL
, RELAX_NEXT (STATE_UNCOND_BRANCH
, STATE_BITS10
)}, /* state 10 bits displ */
211 {0, 0, CUBL
, RELAX_NEXT (STATE_UNCOND_BRANCH
, STATE_WORD
)}, /* state word */
212 {1, 1, CUBL
, 0}, /* state undef */
214 /* Simple branches. */
216 {1024, -1024, CNRL
, RELAX_NEXT (STATE_SIMPLE_BRANCH
, STATE_BITS10
)}, /* state 10 bits displ */
217 {0, 0, CSBL
, RELAX_NEXT (STATE_SIMPLE_BRANCH
, STATE_WORD
)}, /* state word */
220 /* blt no overflow branch. */
222 {1024, -1024, CNRL
, RELAX_NEXT (STATE_NOOV_BRANCH
, STATE_BITS10
)}, /* state 10 bits displ */
223 {0, 0, CNOL
, RELAX_NEXT (STATE_NOOV_BRANCH
, STATE_WORD
)}, /* state word */
226 /* Emulated branches. */
228 {1020, -1020, CEBL
, RELAX_NEXT (STATE_EMUL_BRANCH
, STATE_BITS10
)}, /* state 10 bits displ */
229 {0, 0, CNOL
, RELAX_NEXT (STATE_EMUL_BRANCH
, STATE_WORD
)}, /* state word */
234 #define MAX_OP_LEN 256
243 #define MSP430_ISA_11 11
244 #define MSP430_ISA_110 110
245 #define MSP430_ISA_12 12
246 #define MSP430_ISA_13 13
247 #define MSP430_ISA_14 14
248 #define MSP430_ISA_15 15
249 #define MSP430_ISA_16 16
250 #define MSP430_ISA_21 21
251 #define MSP430_ISA_31 31
252 #define MSP430_ISA_32 32
253 #define MSP430_ISA_33 33
254 #define MSP430_ISA_41 41
255 #define MSP430_ISA_42 42
256 #define MSP430_ISA_43 43
257 #define MSP430_ISA_44 44
259 #define CHECK_RELOC_MSP430 ((imm_op || byte_op)?BFD_RELOC_MSP430_16_BYTE:BFD_RELOC_MSP430_16)
260 #define CHECK_RELOC_MSP430_PCREL ((imm_op || byte_op)?BFD_RELOC_MSP430_16_PCREL_BYTE:BFD_RELOC_MSP430_16_PCREL)
262 static struct mcu_type_s mcu_types
[] =
264 {"msp1", MSP430_ISA_11
, bfd_mach_msp11
},
265 {"msp2", MSP430_ISA_14
, bfd_mach_msp14
},
266 {"msp430x110", MSP430_ISA_11
, bfd_mach_msp11
},
267 {"msp430x112", MSP430_ISA_11
, bfd_mach_msp11
},
268 {"msp430x1101", MSP430_ISA_110
, bfd_mach_msp110
},
269 {"msp430x1111", MSP430_ISA_110
, bfd_mach_msp110
},
270 {"msp430x1121", MSP430_ISA_110
, bfd_mach_msp110
},
271 {"msp430x1122", MSP430_ISA_11
, bfd_mach_msp110
},
272 {"msp430x1132", MSP430_ISA_11
, bfd_mach_msp110
},
274 {"msp430x122", MSP430_ISA_12
, bfd_mach_msp12
},
275 {"msp430x123", MSP430_ISA_12
, bfd_mach_msp12
},
276 {"msp430x1222", MSP430_ISA_12
, bfd_mach_msp12
},
277 {"msp430x1232", MSP430_ISA_12
, bfd_mach_msp12
},
279 {"msp430x133", MSP430_ISA_13
, bfd_mach_msp13
},
280 {"msp430x135", MSP430_ISA_13
, bfd_mach_msp13
},
281 {"msp430x1331", MSP430_ISA_13
, bfd_mach_msp13
},
282 {"msp430x1351", MSP430_ISA_13
, bfd_mach_msp13
},
283 {"msp430x147", MSP430_ISA_14
, bfd_mach_msp14
},
284 {"msp430x148", MSP430_ISA_14
, bfd_mach_msp14
},
285 {"msp430x149", MSP430_ISA_14
, bfd_mach_msp14
},
287 {"msp430x155", MSP430_ISA_15
, bfd_mach_msp15
},
288 {"msp430x156", MSP430_ISA_15
, bfd_mach_msp15
},
289 {"msp430x157", MSP430_ISA_15
, bfd_mach_msp15
},
290 {"msp430x167", MSP430_ISA_16
, bfd_mach_msp16
},
291 {"msp430x168", MSP430_ISA_16
, bfd_mach_msp16
},
292 {"msp430x169", MSP430_ISA_16
, bfd_mach_msp16
},
293 {"msp430x1610", MSP430_ISA_16
, bfd_mach_msp16
},
294 {"msp430x1611", MSP430_ISA_16
, bfd_mach_msp16
},
295 {"msp430x1612", MSP430_ISA_16
, bfd_mach_msp16
},
297 {"msp430x2101", MSP430_ISA_21
, bfd_mach_msp21
},
298 {"msp430x2111", MSP430_ISA_21
, bfd_mach_msp21
},
299 {"msp430x2121", MSP430_ISA_21
, bfd_mach_msp21
},
300 {"msp430x2131", MSP430_ISA_21
, bfd_mach_msp21
},
302 {"msp430x311", MSP430_ISA_31
, bfd_mach_msp31
},
303 {"msp430x312", MSP430_ISA_31
, bfd_mach_msp31
},
304 {"msp430x313", MSP430_ISA_31
, bfd_mach_msp31
},
305 {"msp430x314", MSP430_ISA_31
, bfd_mach_msp31
},
306 {"msp430x315", MSP430_ISA_31
, bfd_mach_msp31
},
307 {"msp430x323", MSP430_ISA_32
, bfd_mach_msp32
},
308 {"msp430x325", MSP430_ISA_32
, bfd_mach_msp32
},
309 {"msp430x336", MSP430_ISA_33
, bfd_mach_msp33
},
310 {"msp430x337", MSP430_ISA_33
, bfd_mach_msp33
},
312 {"msp430x412", MSP430_ISA_41
, bfd_mach_msp41
},
313 {"msp430x413", MSP430_ISA_41
, bfd_mach_msp41
},
314 {"msp430x415", MSP430_ISA_41
, bfd_mach_msp41
},
315 {"msp430x417", MSP430_ISA_41
, bfd_mach_msp41
},
317 {"msp430xE423", MSP430_ISA_42
, bfd_mach_msp42
},
318 {"msp430xE425", MSP430_ISA_42
, bfd_mach_msp42
},
319 {"msp430xE427", MSP430_ISA_42
, bfd_mach_msp42
},
321 {"msp430xW423", MSP430_ISA_42
, bfd_mach_msp42
},
322 {"msp430xW425", MSP430_ISA_42
, bfd_mach_msp42
},
323 {"msp430xW427", MSP430_ISA_42
, bfd_mach_msp42
},
325 {"msp430xG437", MSP430_ISA_43
, bfd_mach_msp43
},
326 {"msp430xG438", MSP430_ISA_43
, bfd_mach_msp43
},
327 {"msp430xG439", MSP430_ISA_43
, bfd_mach_msp43
},
329 {"msp430x435", MSP430_ISA_43
, bfd_mach_msp43
},
330 {"msp430x436", MSP430_ISA_43
, bfd_mach_msp43
},
331 {"msp430x437", MSP430_ISA_43
, bfd_mach_msp43
},
332 {"msp430x447", MSP430_ISA_44
, bfd_mach_msp44
},
333 {"msp430x448", MSP430_ISA_44
, bfd_mach_msp44
},
334 {"msp430x449", MSP430_ISA_44
, bfd_mach_msp44
},
340 static struct mcu_type_s default_mcu
=
341 { "msp430x11", MSP430_ISA_11
, bfd_mach_msp11
};
343 static struct mcu_type_s
* msp430_mcu
= & default_mcu
;
345 /* Profiling capability:
346 It is a performance hit to use gcc's profiling approach for this tiny target.
347 Even more -- jtag hardware facility does not perform any profiling functions.
348 However we've got gdb's built-in simulator where we can do anything.
349 Therefore my suggestion is:
351 We define new section ".profiler" which holds all profiling information.
352 We define new pseudo operation .profiler which will instruct assembler to
353 add new profile entry to the object file. Profile should take place at the
358 .profiler flags,function_to_profile [, cycle_corrector, extra]
360 where 'flags' is a combination of the following chars:
363 i - function is in Init section
364 f - function is in Fini section
366 c - libC standard call
367 d - stack value Demand (saved at run-time in simulator)
368 I - Interrupt service routine
373 j - long Jump/ sjlj unwind
374 a - an Arbitrary code fragment
375 t - exTra parameter saved (constant value like frame size)
376 '""' optional: "sil" == sil
378 function_to_profile - function's address
379 cycle_corrector - a value which should be added to the cycle
380 counter, zero if omitted
381 extra - some extra parameter, zero if omitted.
384 ------------------------------
388 .LFrameOffset_fxx=0x08
389 .profiler "scdP", fxx ; function entry.
390 ; we also demand stack value to be displayed
395 .profiler "cdp",fxx,0, .LFrameOffset_fxx ; check stack value at this point
396 ; (this is a prologue end)
397 ; note, that spare var filled with the farme size
400 .profiler cdE,fxx ; check stack
405 .profiler xcde,fxx,3 ; exit adds 3 to the cycle counter
406 ret ; cause 'ret' insn takes 3 cycles
407 -------------------------------
409 This profiling approach does not produce any overhead and
411 So, even profiled code can be uploaded to the MCU. */
412 #define MSP430_PROFILER_FLAG_ENTRY 1 /* s */
413 #define MSP430_PROFILER_FLAG_EXIT 2 /* x */
414 #define MSP430_PROFILER_FLAG_INITSECT 4 /* i */
415 #define MSP430_PROFILER_FLAG_FINISECT 8 /* f */
416 #define MSP430_PROFILER_FLAG_LIBCALL 0x10 /* l */
417 #define MSP430_PROFILER_FLAG_STDCALL 0x20 /* c */
418 #define MSP430_PROFILER_FLAG_STACKDMD 0x40 /* d */
419 #define MSP430_PROFILER_FLAG_ISR 0x80 /* I */
420 #define MSP430_PROFILER_FLAG_PROLSTART 0x100 /* P */
421 #define MSP430_PROFILER_FLAG_PROLEND 0x200 /* p */
422 #define MSP430_PROFILER_FLAG_EPISTART 0x400 /* E */
423 #define MSP430_PROFILER_FLAG_EPIEND 0x800 /* e */
424 #define MSP430_PROFILER_FLAG_JUMP 0x1000 /* j */
425 #define MSP430_PROFILER_FLAG_FRAGMENT 0x2000 /* a */
426 #define MSP430_PROFILER_FLAG_EXTRA 0x4000 /* t */
427 #define MSP430_PROFILER_FLAG_notyet 0x8000 /* ? */
440 for (; x
; x
= x
>> 1)
447 /* Parse ordinary expression. */
450 parse_exp (char * s
, expressionS
* op
)
452 input_line_pointer
= s
;
454 if (op
->X_op
== O_absent
)
455 as_bad (_("missing operand"));
456 return input_line_pointer
;
460 /* Delete spaces from s: X ( r 1 2) => X(r12). */
463 del_spaces (char * s
)
471 while (ISSPACE (*m
) && *m
)
473 memmove (s
, m
, strlen (m
) + 1);
481 skip_space (char * s
)
488 /* Extract one word from FROM and copy it to TO. Delimiters are ",;\n" */
491 extract_operand (char * from
, char * to
, int limit
)
495 /* Drop leading whitespace. */
496 from
= skip_space (from
);
498 while (size
< limit
&& *from
)
500 *(to
+ size
) = *from
;
501 if (*from
== ',' || *from
== ';' || *from
== '\n')
516 msp430_profiler (int dummy ATTRIBUTE_UNUSED
)
533 s
= input_line_pointer
;
534 end
= input_line_pointer
;
536 while (*end
&& *end
!= '\n')
539 while (*s
&& *s
!= '\n')
550 as_bad (_(".profiler pseudo requires at least two operands."));
551 input_line_pointer
= end
;
555 input_line_pointer
= extract_operand (input_line_pointer
, flags
, 32);
564 p_flags
|= MSP430_PROFILER_FLAG_FRAGMENT
;
567 p_flags
|= MSP430_PROFILER_FLAG_JUMP
;
570 p_flags
|= MSP430_PROFILER_FLAG_PROLSTART
;
573 p_flags
|= MSP430_PROFILER_FLAG_PROLEND
;
576 p_flags
|= MSP430_PROFILER_FLAG_EPISTART
;
579 p_flags
|= MSP430_PROFILER_FLAG_EPIEND
;
582 p_flags
|= MSP430_PROFILER_FLAG_ENTRY
;
585 p_flags
|= MSP430_PROFILER_FLAG_EXIT
;
588 p_flags
|= MSP430_PROFILER_FLAG_INITSECT
;
591 p_flags
|= MSP430_PROFILER_FLAG_FINISECT
;
594 p_flags
|= MSP430_PROFILER_FLAG_LIBCALL
;
597 p_flags
|= MSP430_PROFILER_FLAG_STDCALL
;
600 p_flags
|= MSP430_PROFILER_FLAG_STACKDMD
;
603 p_flags
|= MSP430_PROFILER_FLAG_ISR
;
606 p_flags
|= MSP430_PROFILER_FLAG_EXTRA
;
609 as_warn (_("unknown profiling flag - ignored."));
616 && ( ! pow2value (p_flags
& ( MSP430_PROFILER_FLAG_ENTRY
617 | MSP430_PROFILER_FLAG_EXIT
))
618 || ! pow2value (p_flags
& ( MSP430_PROFILER_FLAG_PROLSTART
619 | MSP430_PROFILER_FLAG_PROLEND
620 | MSP430_PROFILER_FLAG_EPISTART
621 | MSP430_PROFILER_FLAG_EPIEND
))
622 || ! pow2value (p_flags
& ( MSP430_PROFILER_FLAG_INITSECT
623 | MSP430_PROFILER_FLAG_FINISECT
))))
625 as_bad (_("ambigious flags combination - '.profiler' directive ignored."));
626 input_line_pointer
= end
;
630 /* Generate temp symbol which denotes current location. */
631 if (now_seg
== absolute_section
) /* Paranoja ? */
633 exp1
.X_op
= O_constant
;
634 exp1
.X_add_number
= abs_section_offset
;
635 as_warn (_("profiling in absolute section? Hm..."));
639 exp1
.X_op
= O_symbol
;
640 exp1
.X_add_symbol
= symbol_temp_new_now ();
641 exp1
.X_add_number
= 0;
644 /* Generate a symbol which holds flags value. */
645 exp
.X_op
= O_constant
;
646 exp
.X_add_number
= p_flags
;
648 /* Save current section. */
652 /* Now go to .profiler section. */
653 obj_elf_change_section (".profiler", SHT_PROGBITS
, 0, 0, 0, 0, 0);
656 emit_expr (& exp
, 2);
658 /* Save label value. */
659 emit_expr (& exp1
, 2);
663 /* Now get profiling info. */
664 halt
= extract_operand (input_line_pointer
, str
, 1024);
665 /* Process like ".word xxx" directive. */
666 parse_exp (str
, & exp
);
667 emit_expr (& exp
, 2);
668 input_line_pointer
= halt
;
671 /* Fill the rest with zeros. */
672 exp
.X_op
= O_constant
;
673 exp
.X_add_number
= 0;
675 emit_expr (& exp
, 2);
677 /* Return to current section. */
678 subseg_set (seg
, subseg
);
682 extract_word (char * from
, char * to
, int limit
)
688 /* Drop leading whitespace. */
689 from
= skip_space (from
);
692 /* Find the op code end. */
693 for (op_start
= op_end
= from
; *op_end
!= 0 && is_part_of_name (*op_end
);)
695 to
[size
++] = *op_end
++;
696 if (size
+ 1 >= limit
)
704 #define OPTION_MMCU 'm'
705 #define OPTION_RELAX 'Q'
706 #define OPTION_POLYMORPHS 'P'
709 msp430_set_arch (int dummy ATTRIBUTE_UNUSED
)
711 char *str
= (char *) alloca (32); /* 32 for good measure. */
713 input_line_pointer
= extract_word (input_line_pointer
, str
, 32);
715 md_parse_option (OPTION_MMCU
, str
);
716 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, msp430_mcu
->mach
);
720 show_mcu_list (FILE * stream
)
724 fprintf (stream
, _("Known MCU names:\n"));
726 for (i
= 0; mcu_types
[i
].name
; i
++)
727 fprintf (stream
, _("\t %s\n"), mcu_types
[i
].name
);
729 fprintf (stream
, "\n");
733 md_parse_option (int c
, char * arg
)
740 for (i
= 0; mcu_types
[i
].name
; ++i
)
741 if (strcmp (mcu_types
[i
].name
, arg
) == 0)
744 if (!mcu_types
[i
].name
)
746 show_mcu_list (stderr
);
747 as_fatal (_("unknown MCU: %s\n"), arg
);
750 if (msp430_mcu
== &default_mcu
|| msp430_mcu
->mach
== mcu_types
[i
].mach
)
751 msp430_mcu
= &mcu_types
[i
];
753 as_fatal (_("redefinition of mcu type %s' to %s'"),
754 msp430_mcu
->name
, mcu_types
[i
].name
);
759 msp430_enable_relax
= 1;
763 case OPTION_POLYMORPHS
:
764 msp430_enable_polys
= 1;
773 const pseudo_typeS md_pseudo_table
[] =
775 {"arch", msp430_set_arch
, 0},
776 {"profiler", msp430_profiler
, 0},
780 const char *md_shortopts
= "m:";
782 struct option md_longopts
[] =
784 {"mmcu", required_argument
, NULL
, OPTION_MMCU
},
785 {"mP", no_argument
, NULL
, OPTION_POLYMORPHS
},
786 {"mQ", no_argument
, NULL
, OPTION_RELAX
},
787 {NULL
, no_argument
, NULL
, 0}
790 size_t md_longopts_size
= sizeof (md_longopts
);
793 md_show_usage (FILE * stream
)
796 _("MSP430 options:\n"
797 " -mmcu=[msp430-name] select microcontroller type\n"
798 " msp430x110 msp430x112\n"
799 " msp430x1101 msp430x1111\n"
800 " msp430x1121 msp430x1122 msp430x1132\n"
801 " msp430x122 msp430x123\n"
802 " msp430x1222 msp430x1232\n"
803 " msp430x133 msp430x135\n"
804 " msp430x1331 msp430x1351\n"
805 " msp430x147 msp430x148 msp430x149\n"
806 " msp430x155 msp430x156 msp430x157\n"
807 " msp430x167 msp430x168 msp430x169\n"
808 " msp430x1610 msp430x1611 msp430x1612\n"
809 " msp430x311 msp430x312 msp430x313 msp430x314 msp430x315\n"
810 " msp430x323 msp430x325\n"
811 " msp430x336 msp430x337\n"
812 " msp430x412 msp430x413 msp430x415 msp430x417\n"
813 " msp430xE423 msp430xE425 msp430E427\n"
814 " msp430xW423 msp430xW425 msp430W427\n"
815 " msp430xG437 msp430xG438 msp430G439\n"
816 " msp430x435 msp430x436 msp430x437\n"
817 " msp430x447 msp430x448 msp430x449\n"));
819 _(" -mQ - enable relaxation at assembly time. DANGEROUS!\n"
820 " -mP - enable polymorph instructions\n"));
822 show_mcu_list (stream
);
826 md_undefined_symbol (char * name ATTRIBUTE_UNUSED
)
832 extract_cmd (char * from
, char * to
, int limit
)
836 while (*from
&& ! ISSPACE (*from
) && *from
!= '.' && limit
> size
)
838 *(to
+ size
) = *from
;
848 /* Turn a string in input_line_pointer into a floating point constant
849 of type TYPE, and store the appropriate bytes in *LITP. The number
850 of LITTLENUMS emitted is stored in *SIZEP. An error message is
851 returned, or NULL on OK. */
854 md_atof (int type
, char * litP
, int * sizeP
)
857 LITTLENUM_TYPE words
[4];
858 LITTLENUM_TYPE
*wordP
;
871 return _("bad call to md_atof");
874 t
= atof_ieee (input_line_pointer
, type
, words
);
876 input_line_pointer
= t
;
878 *sizeP
= prec
* sizeof (LITTLENUM_TYPE
);
880 /* This loop outputs the LITTLENUMs in REVERSE order. */
881 for (wordP
= words
+ prec
- 1; prec
--;)
883 md_number_to_chars (litP
, (valueT
) (*wordP
--), sizeof (LITTLENUM_TYPE
));
884 litP
+= sizeof (LITTLENUM_TYPE
);
893 struct msp430_opcode_s
* opcode
;
894 msp430_hash
= hash_new ();
896 for (opcode
= msp430_opcodes
; opcode
->name
; opcode
++)
897 hash_insert (msp430_hash
, opcode
->name
, (char *) opcode
);
899 bfd_set_arch_mach (stdoutput
, TARGET_ARCH
, msp430_mcu
->mach
);
905 /* If this is a reg numb, str 't' must be a number from 0 - 15. */
907 if (strlen (t
) > 2 && *(t
+ 2) != '+')
912 if ((*t
< '0' || *t
> '9') && *t
!= '+')
925 msp430_srcoperand (struct msp430_operand_s
* op
,
926 char * l
, int bin
, int * imm_op
)
930 /* Check if an immediate #VALUE. The hash sign should be only at the beginning! */
937 /* Check if there is:
938 llo(x) - least significant 16 bits, x &= 0xffff
939 lhi(x) - x = (x >> 16) & 0xffff,
940 hlo(x) - x = (x >> 32) & 0xffff,
941 hhi(x) - x = (x >> 48) & 0xffff
942 The value _MUST_ be constant expression: #hlo(1231231231). */
946 if (strncasecmp (h
, "#llo(", 5) == 0)
951 else if (strncasecmp (h
, "#lhi(", 5) == 0)
956 else if (strncasecmp (h
, "#hlo(", 5) == 0)
961 else if (strncasecmp (h
, "#hhi(", 5) == 0)
966 else if (strncasecmp (h
, "#lo(", 4) == 0)
971 else if (strncasecmp (h
, "#hi(", 4) == 0)
977 op
->reg
= 0; /* Reg PC. */
979 op
->ol
= 1; /* Immediate will follow an instruction. */
983 parse_exp (__tl
, &(op
->exp
));
984 if (op
->exp
.X_op
== O_constant
)
986 int x
= op
->exp
.X_add_number
;
991 op
->exp
.X_add_number
= x
;
993 else if (vshift
== 1)
995 x
= (x
>> 16) & 0xffff;
996 op
->exp
.X_add_number
= x
;
1001 op
->exp
.X_add_number
= -1;
1003 op
->exp
.X_add_number
= 0; /* Nothing left. */
1004 x
= op
->exp
.X_add_number
;
1007 if (op
->exp
.X_add_number
> 65535 || op
->exp
.X_add_number
< -32768)
1009 as_bad (_("value %d out of range. Use #lo() or #hi()"), x
);
1013 /* Now check constants. */
1014 /* Substitute register mode with a constant generator if applicable. */
1016 x
= (short) x
; /* Extend sign. */
1048 #ifdef PUSH_1X_WORKAROUND
1051 /* Remove warning as confusing.
1052 as_warn(_("Hardware push bug workaround")); */
1065 #ifdef PUSH_1X_WORKAROUND
1068 /* Remove warning as confusing.
1069 as_warn(_("Hardware push bug workaround")); */
1081 else if (op
->exp
.X_op
== O_symbol
)
1085 else if (op
->exp
.X_op
== O_big
)
1090 op
->exp
.X_op
= O_constant
;
1091 op
->exp
.X_add_number
= 0xffff & generic_bignum
[vshift
];
1092 x
= op
->exp
.X_add_number
;
1097 ("unknown expression in operand %s. use #llo() #lhi() #hlo() #hhi() "),
1145 /* Redudant (yet) check. */
1146 else if (op
->exp
.X_op
== O_register
)
1148 (_("Registers cannot be used within immediate expression [%s]"), l
);
1150 as_bad (_("unknown operand %s"), l
);
1155 /* Check if absolute &VALUE (assume that we can construct something like ((a&b)<<7 + 25). */
1160 op
->reg
= 2; /* reg 2 in absolute addr mode. */
1161 op
->am
= 1; /* mode As == 01 bin. */
1162 op
->ol
= 1; /* Immediate value followed by instruction. */
1164 parse_exp (__tl
, &(op
->exp
));
1166 if (op
->exp
.X_op
== O_constant
)
1168 int x
= op
->exp
.X_add_number
;
1170 if (x
> 65535 || x
< -32768)
1172 as_bad (_("value out of range: %d"), x
);
1176 else if (op
->exp
.X_op
== O_symbol
)
1180 /* Redudant (yet) check. */
1181 if (op
->exp
.X_op
== O_register
)
1183 (_("Registers cannot be used within absolute expression [%s]"), l
);
1185 as_bad (_("unknown expression in operand %s"), l
);
1191 /* Check if indirect register mode @Rn / postincrement @Rn+. */
1195 char *m
= strchr (l
, '+');
1199 as_bad (_("unknown addressing mode %s"), l
);
1204 if (*t
!= 'r' && *t
!= 'R')
1206 as_bad (_("unknown addressing mode %s"), l
);
1210 t
++; /* Points to the reg value. */
1214 as_bad (_("Bad register name r%s"), t
);
1222 *m
= 0; /* strip '+' */
1224 if (op
->reg
< 0 || op
->reg
> 15)
1226 as_bad (_("MSP430 does not have %d registers"), op
->reg
);
1233 /* Check if register indexed X(Rn). */
1236 char *h
= strrchr (l
, '(');
1237 char *m
= strrchr (l
, ')');
1246 as_bad (_("')' required"));
1253 /* Extract a register. */
1254 t
++; /* Advance pointer. */
1256 if (*t
!= 'r' && *t
!= 'R')
1259 ("unknown operator %s. Did you mean X(Rn) or #[hl][hl][oi](CONST) ?"),
1266 if (op
->reg
> 9 || op
->reg
< 0)
1268 as_bad (_("unknown operator (r%s substituded as a register name"),
1275 op
->reg
= op
->reg
* 10;
1276 op
->reg
+= *t
- '0';
1280 as_bad (_("unknown operator %s"), l
);
1285 as_bad (_("r2 should not be used in indexed addressing mode"));
1289 if (*(t
+ 1) != ')')
1291 as_bad (_("unknown operator %s"), l
);
1296 /* Extract constant. */
1300 parse_exp (__tl
, &(op
->exp
));
1301 if (op
->exp
.X_op
== O_constant
)
1303 int x
= op
->exp
.X_add_number
;
1305 if (x
> 65535 || x
< -32768)
1307 as_bad (_("value out of range: %d"), x
);
1319 else if (op
->exp
.X_op
== O_symbol
)
1323 /* Redudant (yet) check. */
1324 if (op
->exp
.X_op
== O_register
)
1326 (_("Registers cannot be used as a prefix of indexed expression [%s]"), l
);
1328 as_bad (_("unknown expression in operand %s"), l
);
1336 /* Register mode 'mov r1,r2'. */
1341 /* Operand should be a register. */
1342 if (*t
== 'r' || *t
== 'R')
1344 int x
= atoi (t
+ 1);
1346 if (check_reg (t
+ 1))
1349 if (x
< 0 || x
> 15)
1350 break; /* Symbolic mode. */
1361 /* Symbolic mode 'mov a, b' == 'mov x(pc), y(pc)'. */
1365 op
->reg
= 0; /* PC relative... be careful. */
1369 parse_exp (__tl
, &(op
->exp
));
1375 as_bad (_("unknown addressing mode for operand %s"), l
);
1381 msp430_dstoperand (struct msp430_operand_s
* op
, char * l
, int bin
)
1384 int ret
= msp430_srcoperand (op
, l
, bin
, & dummy
);
1396 parse_exp (__tl
, &(op
->exp
));
1398 if (op
->exp
.X_op
!= O_constant
|| op
->exp
.X_add_number
!= 0)
1400 as_bad (_("Internal bug. Try to use 0(r%d) instead of @r%d"),
1410 ("this addressing mode is not applicable for destination operand"));
1417 /* Parse instruction operands.
1418 Return binary opcode. */
1421 msp430_operands (struct msp430_opcode_s
* opcode
, char * line
)
1423 int bin
= opcode
->bin_opcode
; /* Opcode mask. */
1425 char l1
[MAX_OP_LEN
], l2
[MAX_OP_LEN
];
1428 struct msp430_operand_s op1
, op2
;
1430 static short ZEROS
= 0;
1431 int byte_op
, imm_op
;
1433 /* Opcode is the one from opcodes table
1434 line contains something like
1439 /* Check if byte or word operation. */
1440 if (*line
== '.' && TOLOWER (*(line
+ 1)) == 'b')
1442 bin
|= BYTE_OPERATION
;
1449 while (! ISSPACE (*line
) && *line
)
1452 if (opcode
->insn_opnumb
&& (!*line
|| *line
== '\n'))
1454 as_bad (_("instruction %s requires %d operand(s)"),
1455 opcode
->name
, opcode
->insn_opnumb
);
1459 memset (l1
, 0, sizeof (l1
));
1460 memset (l2
, 0, sizeof (l2
));
1461 memset (&op1
, 0, sizeof (op1
));
1462 memset (&op2
, 0, sizeof (op2
));
1466 switch (opcode
->fmt
)
1468 case 0: /* Emulated. */
1469 switch (opcode
->insn_opnumb
)
1472 /* Set/clear bits instructions. */
1474 frag
= frag_more (__is
);
1475 bfd_putl16 ((bfd_vma
) bin
, frag
);
1476 dwarf2_emit_insn (__is
);
1479 /* Something which works with destination operand. */
1480 line
= extract_operand (line
, l1
, sizeof (l1
));
1481 res
= msp430_dstoperand (&op1
, l1
, opcode
->bin_opcode
);
1485 bin
|= (op1
.reg
| (op1
.am
<< 7));
1487 frag
= frag_more (2 * __is
);
1488 where
= frag
- frag_now
->fr_literal
;
1489 bfd_putl16 ((bfd_vma
) bin
, frag
);
1490 dwarf2_emit_insn (2 * __is
);
1492 if (op1
.mode
== OP_EXP
)
1495 bfd_putl16 ((bfd_vma
) ZEROS
, frag
+ 2);
1498 fix_new_exp (frag_now
, where
, 2,
1499 &(op1
.exp
), FALSE
, CHECK_RELOC_MSP430
);
1501 fix_new_exp (frag_now
, where
, 2,
1502 &(op1
.exp
), TRUE
, CHECK_RELOC_MSP430_PCREL
);
1508 /* Shift instruction. */
1509 line
= extract_operand (line
, l1
, sizeof (l1
));
1510 strncpy (l2
, l1
, sizeof (l2
));
1511 l2
[sizeof (l2
) - 1] = '\0';
1512 res
= msp430_srcoperand (&op1
, l1
, opcode
->bin_opcode
, &imm_op
);
1513 res
+= msp430_dstoperand (&op2
, l2
, opcode
->bin_opcode
);
1516 break; /* An error occurred. All warnings were done before. */
1518 bin
|= (op2
.reg
| (op1
.reg
<< 8) | (op1
.am
<< 4) | (op2
.am
<< 7));
1520 __is
= 1 + op1
.ol
+ op2
.ol
; /* insn size in words. */
1521 frag
= frag_more (2 * __is
);
1522 where
= frag
- frag_now
->fr_literal
;
1523 bfd_putl16 ((bfd_vma
) bin
, frag
);
1524 dwarf2_emit_insn (2 * __is
);
1526 if (op1
.mode
== OP_EXP
)
1528 where
+= 2; /* Advance 'where' as we do not know _where_. */
1529 bfd_putl16 ((bfd_vma
) ZEROS
, frag
+ 2);
1531 if (op1
.reg
|| (op1
.reg
== 0 && op1
.am
== 3)) /* Not PC relative. */
1532 fix_new_exp (frag_now
, where
, 2,
1533 &(op1
.exp
), FALSE
, CHECK_RELOC_MSP430
);
1535 fix_new_exp (frag_now
, where
, 2,
1536 &(op1
.exp
), TRUE
, CHECK_RELOC_MSP430_PCREL
);
1539 if (op2
.mode
== OP_EXP
)
1542 bfd_putl16 ((bfd_vma
) ZEROS
, frag
+ 2 + ((__is
== 3) ? 2 : 0));
1544 if (op2
.reg
) /* Not PC relative. */
1545 fix_new_exp (frag_now
, where
+ 2, 2,
1546 &(op2
.exp
), FALSE
, CHECK_RELOC_MSP430
);
1548 fix_new_exp (frag_now
, where
+ 2, 2,
1549 &(op2
.exp
), TRUE
, CHECK_RELOC_MSP430_PCREL
);
1554 /* Branch instruction => mov dst, r0. */
1555 line
= extract_operand (line
, l1
, sizeof (l1
));
1557 res
= msp430_srcoperand (&op1
, l1
, opcode
->bin_opcode
, &imm_op
);
1564 bin
|= ((op1
.reg
<< 8) | (op1
.am
<< 4));
1566 frag
= frag_more (2 * __is
);
1567 where
= frag
- frag_now
->fr_literal
;
1568 bfd_putl16 ((bfd_vma
) bin
, frag
);
1569 dwarf2_emit_insn (2 * __is
);
1571 if (op1
.mode
== OP_EXP
)
1574 bfd_putl16 ((bfd_vma
) ZEROS
, frag
+ 2);
1576 if (op1
.reg
|| (op1
.reg
== 0 && op1
.am
== 3))
1577 fix_new_exp (frag_now
, where
, 2,
1578 &(op1
.exp
), FALSE
, CHECK_RELOC_MSP430
);
1580 fix_new_exp (frag_now
, where
, 2,
1581 &(op1
.exp
), TRUE
, CHECK_RELOC_MSP430_PCREL
);
1587 case 1: /* Format 1, double operand. */
1588 line
= extract_operand (line
, l1
, sizeof (l1
));
1589 line
= extract_operand (line
, l2
, sizeof (l2
));
1590 res
= msp430_srcoperand (&op1
, l1
, opcode
->bin_opcode
, &imm_op
);
1591 res
+= msp430_dstoperand (&op2
, l2
, opcode
->bin_opcode
);
1594 break; /* Error occurred. All warnings were done before. */
1596 bin
|= (op2
.reg
| (op1
.reg
<< 8) | (op1
.am
<< 4) | (op2
.am
<< 7));
1598 __is
= 1 + op1
.ol
+ op2
.ol
; /* insn size in words. */
1599 frag
= frag_more (2 * __is
);
1600 where
= frag
- frag_now
->fr_literal
;
1601 bfd_putl16 ((bfd_vma
) bin
, frag
);
1602 dwarf2_emit_insn (2 * __is
);
1604 if (op1
.mode
== OP_EXP
)
1606 where
+= 2; /* Advance where as we do not know _where_. */
1607 bfd_putl16 ((bfd_vma
) ZEROS
, frag
+ 2);
1609 if (op1
.reg
|| (op1
.reg
== 0 && op1
.am
== 3)) /* Not PC relative. */
1610 fix_new_exp (frag_now
, where
, 2,
1611 &(op1
.exp
), FALSE
, CHECK_RELOC_MSP430
);
1613 fix_new_exp (frag_now
, where
, 2,
1614 &(op1
.exp
), TRUE
, CHECK_RELOC_MSP430_PCREL
);
1617 if (op2
.mode
== OP_EXP
)
1620 bfd_putl16 ((bfd_vma
) ZEROS
, frag
+ 2 + ((__is
== 3) ? 2 : 0));
1622 if (op2
.reg
) /* Not PC relative. */
1623 fix_new_exp (frag_now
, where
+ 2, 2,
1624 &(op2
.exp
), FALSE
, CHECK_RELOC_MSP430
);
1626 fix_new_exp (frag_now
, where
+ 2, 2,
1627 &(op2
.exp
), TRUE
, CHECK_RELOC_MSP430_PCREL
);
1631 case 2: /* Single-operand mostly instr. */
1632 if (opcode
->insn_opnumb
== 0)
1634 /* reti instruction. */
1635 frag
= frag_more (2);
1636 bfd_putl16 ((bfd_vma
) bin
, frag
);
1637 dwarf2_emit_insn (2);
1641 line
= extract_operand (line
, l1
, sizeof (l1
));
1642 res
= msp430_srcoperand (&op1
, l1
, opcode
->bin_opcode
, &imm_op
);
1644 break; /* Error in operand. */
1646 bin
|= op1
.reg
| (op1
.am
<< 4);
1648 frag
= frag_more (2 * __is
);
1649 where
= frag
- frag_now
->fr_literal
;
1650 bfd_putl16 ((bfd_vma
) bin
, frag
);
1651 dwarf2_emit_insn (2 * __is
);
1653 if (op1
.mode
== OP_EXP
)
1655 bfd_putl16 ((bfd_vma
) ZEROS
, frag
+ 2);
1657 if (op1
.reg
|| (op1
.reg
== 0 && op1
.am
== 3)) /* Not PC relative. */
1658 fix_new_exp (frag_now
, where
+ 2, 2,
1659 &(op1
.exp
), FALSE
, CHECK_RELOC_MSP430
);
1661 fix_new_exp (frag_now
, where
+ 2, 2,
1662 &(op1
.exp
), TRUE
, CHECK_RELOC_MSP430_PCREL
);
1666 case 3: /* Conditional jumps instructions. */
1667 line
= extract_operand (line
, l1
, sizeof (l1
));
1668 /* l1 is a label. */
1677 parse_exp (m
, &exp
);
1678 frag
= frag_more (2); /* Instr size is 1 word. */
1680 /* In order to handle something like:
1684 jz 4 ; skip next 4 bytes
1687 nop ; will jump here if r5 positive or zero
1689 jCOND -n ;assumes jump n bytes backward:
1699 jCOND $n ; jump from PC in either direction. */
1701 if (exp
.X_op
== O_constant
)
1703 int x
= exp
.X_add_number
;
1707 as_warn (_("Even number required. Rounded to %d"), x
+ 1);
1711 if ((*l1
== '$' && x
> 0) || x
< 0)
1716 if (x
> 512 || x
< -511)
1718 as_bad (_("Wrong displacement %d"), x
<< 1);
1723 bfd_putl16 ((bfd_vma
) bin
, frag
);
1725 else if (exp
.X_op
== O_symbol
&& *l1
!= '$')
1727 where
= frag
- frag_now
->fr_literal
;
1728 fix_new_exp (frag_now
, where
, 2,
1729 &exp
, TRUE
, BFD_RELOC_MSP430_10_PCREL
);
1731 bfd_putl16 ((bfd_vma
) bin
, frag
);
1733 else if (*l1
== '$')
1735 as_bad (_("instruction requires label sans '$'"));
1740 ("instruction requires label or value in range -511:512"));
1742 dwarf2_emit_insn (2 * __is
);
1747 as_bad (_("instruction requires label"));
1752 case 4: /* Extended jumps. */
1753 if (!msp430_enable_polys
)
1755 as_bad(_("polymorphs are not enabled. Use -mP option to enable."));
1759 line
= extract_operand (line
, l1
, sizeof (l1
));
1765 /* Ignore absolute addressing. make it PC relative anyway. */
1766 if (*m
== '#' || *m
== '$')
1769 parse_exp (m
, & exp
);
1770 if (exp
.X_op
== O_symbol
)
1772 /* Relaxation required. */
1773 struct rcodes_s rc
= msp430_rcodes
[opcode
->insn_opnumb
];
1775 /* The parameter to dwarf2_emit_insn is actually the offset to the start
1776 of the insn from the fix piece of instruction that was emitted.
1777 Since next fragments may have variable size we tie debug info
1778 to the beginning of the instruction. */
1779 frag
= frag_more (8);
1780 dwarf2_emit_insn (0);
1781 bfd_putl16 ((bfd_vma
) rc
.sop
, frag
);
1782 frag
= frag_variant (rs_machine_dependent
, 8, 2,
1783 ENCODE_RELAX (rc
.lpos
, STATE_BITS10
), /* Wild guess. */
1785 0, /* Offset is zero if jump dist less than 1K. */
1791 as_bad (_("instruction requires label"));
1794 case 5: /* Emulated extended branches. */
1795 if (!msp430_enable_polys
)
1797 as_bad(_("polymorphs are not enabled. Use -mP option to enable."));
1800 line
= extract_operand (line
, l1
, sizeof (l1
));
1806 /* Ignore absolute addressing. make it PC relative anyway. */
1807 if (*m
== '#' || *m
== '$')
1810 parse_exp (m
, & exp
);
1811 if (exp
.X_op
== O_symbol
)
1813 /* Relaxation required. */
1814 struct hcodes_s hc
= msp430_hcodes
[opcode
->insn_opnumb
];
1816 frag
= frag_more (8);
1817 dwarf2_emit_insn (0);
1818 bfd_putl16 ((bfd_vma
) hc
.op0
, frag
);
1819 bfd_putl16 ((bfd_vma
) hc
.op1
, frag
+2);
1821 frag
= frag_variant (rs_machine_dependent
, 8, 2,
1822 ENCODE_RELAX (STATE_EMUL_BRANCH
, STATE_BITS10
), /* Wild guess. */
1824 0, /* Offset is zero if jump dist less than 1K. */
1830 as_bad (_("instruction requires label"));
1834 as_bad (_("Ilegal instruction or not implmented opcode."));
1837 input_line_pointer
= line
;
1842 md_assemble (char * str
)
1844 struct msp430_opcode_s
* opcode
;
1848 str
= skip_space (str
); /* Skip leading spaces. */
1849 str
= extract_cmd (str
, cmd
, sizeof (cmd
));
1851 while (cmd
[i
] && i
< sizeof (cmd
))
1853 char a
= TOLOWER (cmd
[i
]);
1860 as_bad (_("can't find opcode "));
1864 opcode
= (struct msp430_opcode_s
*) hash_find (msp430_hash
, cmd
);
1868 as_bad (_("unknown opcode `%s'"), cmd
);
1873 char *__t
= input_line_pointer
;
1875 msp430_operands (opcode
, str
);
1876 input_line_pointer
= __t
;
1880 /* GAS will call this function for each section at the end of the assembly,
1881 to permit the CPU backend to adjust the alignment of a section. */
1884 md_section_align (asection
* seg
, valueT addr
)
1886 int align
= bfd_get_section_alignment (stdoutput
, seg
);
1888 return ((addr
+ (1 << align
) - 1) & (-1 << align
));
1891 /* If you define this macro, it should return the offset between the
1892 address of a PC relative fixup and the position from which the PC
1893 relative adjustment should be made. On many processors, the base
1894 of a PC relative instruction is the next instruction, so this
1895 macro would return the length of an instruction. */
1898 md_pcrel_from_section (fixS
* fixp
, segT sec
)
1900 if (fixp
->fx_addsy
!= (symbolS
*) NULL
1901 && (!S_IS_DEFINED (fixp
->fx_addsy
)
1902 || (S_GET_SEGMENT (fixp
->fx_addsy
) != sec
)))
1905 return fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
1908 /* Replaces standard TC_FORCE_RELOCATION_LOCAL.
1909 Now it handles the situation when relocations
1910 have to be passed to linker. */
1912 msp430_force_relocation_local(fixS
*fixp
)
1914 if (msp430_enable_polys
1915 && !msp430_enable_relax
)
1918 return (!fixp
->fx_pcrel
1919 || generic_force_reloc(fixp
));
1923 /* GAS will call this for each fixup. It should store the correct
1924 value in the object file. */
1926 md_apply_fix (fixS
* fixp
, valueT
* valuep
, segT seg
)
1928 unsigned char * where
;
1932 if (fixp
->fx_addsy
== (symbolS
*) NULL
)
1937 else if (fixp
->fx_pcrel
)
1939 segT s
= S_GET_SEGMENT (fixp
->fx_addsy
);
1941 if (fixp
->fx_addsy
&& (s
== seg
|| s
== absolute_section
))
1943 /* FIXME: We can appear here only in case if we perform a pc
1944 relative jump to the label which is i) global, ii) locally
1945 defined or this is a jump to an absolute symbol.
1946 If this is an absolute symbol -- everything is OK.
1947 If this is a global label, we've got a symbol value defined
1949 1. S_GET_VALUE (fixp->fx_addsy) will contain a symbol offset
1950 from this section start
1951 2. *valuep will contain the real offset from jump insn to the
1953 So, the result of S_GET_VALUE (fixp->fx_addsy) + (* valuep);
1954 will be incorrect. Therefore remove s_get_value. */
1955 value
= /* S_GET_VALUE (fixp->fx_addsy) + */ * valuep
;
1963 value
= fixp
->fx_offset
;
1965 if (fixp
->fx_subsy
!= (symbolS
*) NULL
)
1967 if (S_GET_SEGMENT (fixp
->fx_subsy
) == absolute_section
)
1969 value
-= S_GET_VALUE (fixp
->fx_subsy
);
1974 /* We don't actually support subtracting a symbol. */
1975 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
1976 _("expression too complex"));
1981 fixp
->fx_no_overflow
= 1;
1983 /* if polymorphs are enabled and relax disabled.
1984 do not kill any relocs and pass them to linker. */
1985 if (msp430_enable_polys
1986 && !msp430_enable_relax
)
1988 if (!fixp
->fx_addsy
|| (fixp
->fx_addsy
1989 && S_GET_SEGMENT (fixp
->fx_addsy
) == absolute_section
))
1990 fixp
->fx_done
= 1; /* it is ok to kill 'abs' reloc */
1997 /* Fetch the instruction, insert the fully resolved operand
1998 value, and stuff the instruction back again. */
2000 where
= (unsigned char *) fixp
->fx_frag
->fr_literal
+ fixp
->fx_where
;
2002 insn
= bfd_getl16 (where
);
2004 switch (fixp
->fx_r_type
)
2006 case BFD_RELOC_MSP430_10_PCREL
:
2008 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
2009 _("odd address operand: %ld"), value
);
2011 /* Jumps are in words. */
2013 --value
; /* Correct PC. */
2015 if (value
< -512 || value
> 511)
2016 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
2017 _("operand out of range: %ld"), value
);
2019 value
&= 0x3ff; /* get rid of extended sign */
2020 bfd_putl16 ((bfd_vma
) (value
| insn
), where
);
2023 case BFD_RELOC_MSP430_RL_PCREL
:
2024 case BFD_RELOC_MSP430_16_PCREL
:
2026 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
2027 _("odd address operand: %ld"), value
);
2029 /* Nothing to be corrected here. */
2030 if (value
< -32768 || value
> 65536)
2031 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
2032 _("operand out of range: %ld"), value
);
2034 value
&= 0xffff; /* Get rid of extended sign. */
2035 bfd_putl16 ((bfd_vma
) value
, where
);
2038 case BFD_RELOC_MSP430_16_PCREL_BYTE
:
2039 /* Nothing to be corrected here. */
2040 if (value
< -32768 || value
> 65536)
2041 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
2042 _("operand out of range: %ld"), value
);
2044 value
&= 0xffff; /* Get rid of extended sign. */
2045 bfd_putl16 ((bfd_vma
) value
, where
);
2049 bfd_putl16 ((bfd_vma
) value
, where
);
2052 case BFD_RELOC_MSP430_16
:
2054 case BFD_RELOC_MSP430_16_BYTE
:
2056 bfd_putl16 ((bfd_vma
) value
, where
);
2060 as_fatal (_("line %d: unknown relocation type: 0x%x"),
2061 fixp
->fx_line
, fixp
->fx_r_type
);
2067 fixp
->fx_addnumber
= value
;
2071 /* GAS will call this to generate a reloc, passing the resulting reloc
2072 to `bfd_install_relocation'. This currently works poorly, as
2073 `bfd_install_relocation' often does the wrong thing, and instances of
2074 `tc_gen_reloc' have been written to work around the problems, which
2075 in turns makes it difficult to fix `bfd_install_relocation'. */
2077 /* If while processing a fixup, a reloc really needs to be created
2078 then it is done here. */
2081 tc_gen_reloc (asection
* seg ATTRIBUTE_UNUSED
, fixS
* fixp
)
2085 reloc
= xmalloc (sizeof (arelent
));
2087 reloc
->sym_ptr_ptr
= xmalloc (sizeof (asymbol
*));
2088 *reloc
->sym_ptr_ptr
= symbol_get_bfdsym (fixp
->fx_addsy
);
2090 reloc
->address
= fixp
->fx_frag
->fr_address
+ fixp
->fx_where
;
2091 reloc
->howto
= bfd_reloc_type_lookup (stdoutput
, fixp
->fx_r_type
);
2092 if (reloc
->howto
== (reloc_howto_type
*) NULL
)
2094 as_bad_where (fixp
->fx_file
, fixp
->fx_line
,
2095 _("reloc %d not supported by object file format"),
2096 (int) fixp
->fx_r_type
);
2100 if (fixp
->fx_r_type
== BFD_RELOC_VTABLE_INHERIT
2101 || fixp
->fx_r_type
== BFD_RELOC_VTABLE_ENTRY
)
2102 reloc
->address
= fixp
->fx_offset
;
2104 reloc
->addend
= fixp
->fx_offset
;
2110 md_estimate_size_before_relax (fragS
* fragP ATTRIBUTE_UNUSED
,
2111 asection
* segment_type ATTRIBUTE_UNUSED
)
2113 if (fragP
->fr_symbol
&& S_GET_SEGMENT (fragP
->fr_symbol
) == segment_type
)
2115 /* This is a jump -> pcrel mode. Nothing to do much here.
2116 Return value == 2. */
2118 ENCODE_RELAX (RELAX_LEN (fragP
->fr_subtype
), STATE_BITS10
);
2120 else if (fragP
->fr_symbol
)
2122 /* Its got a segment, but its not ours. Even if fr_symbol is in
2123 an absolute segment, we dont know a displacement until we link
2124 object files. So it will always be long. This also applies to
2125 labels in a subsegment of current. Liker may relax it to short
2126 jump later. Return value == 8. */
2128 ENCODE_RELAX (RELAX_LEN (fragP
->fr_subtype
), STATE_WORD
);
2132 /* We know the abs value. may be it is a jump to fixed address.
2133 Impossible in our case, cause all constants already handeled. */
2135 ENCODE_RELAX (RELAX_LEN (fragP
->fr_subtype
), STATE_UNDEF
);
2138 return md_relax_table
[fragP
->fr_subtype
].rlx_length
;
2142 md_convert_frag (bfd
* abfd ATTRIBUTE_UNUSED
,
2143 asection
* sec ATTRIBUTE_UNUSED
,
2149 struct rcodes_s
* cc
= NULL
;
2150 struct hcodes_s
* hc
= NULL
;
2152 switch (fragP
->fr_subtype
)
2154 case ENCODE_RELAX (STATE_UNCOND_BRANCH
, STATE_BITS10
):
2155 case ENCODE_RELAX (STATE_SIMPLE_BRANCH
, STATE_BITS10
):
2156 case ENCODE_RELAX (STATE_NOOV_BRANCH
, STATE_BITS10
):
2157 /* We do not have to convert anything here.
2158 Just apply a fix. */
2159 rela
= BFD_RELOC_MSP430_10_PCREL
;
2162 case ENCODE_RELAX (STATE_UNCOND_BRANCH
, STATE_WORD
):
2163 case ENCODE_RELAX (STATE_UNCOND_BRANCH
, STATE_UNDEF
):
2164 /* Convert uncond branch jmp lab -> br lab. */
2165 cc
= & msp430_rcodes
[7];
2166 where
= fragP
->fr_literal
+ fragP
->fr_fix
;
2167 bfd_putl16 (cc
->lop0
, where
);
2168 rela
= BFD_RELOC_MSP430_RL_PCREL
;
2172 case ENCODE_RELAX (STATE_SIMPLE_BRANCH
, STATE_WORD
):
2173 case ENCODE_RELAX (STATE_SIMPLE_BRANCH
, STATE_UNDEF
):
2175 /* Other simple branches. */
2176 int insn
= bfd_getl16 (fragP
->fr_opcode
);
2179 /* Find actual instruction. */
2180 for (i
= 0; i
< 7 && !cc
; i
++)
2181 if (msp430_rcodes
[i
].sop
== insn
)
2182 cc
= & msp430_rcodes
[i
];
2183 if (!cc
|| !cc
->name
)
2184 as_fatal (_("internal inconsistency problem in %s: insn %04lx"),
2185 __FUNCTION__
, (long) insn
);
2186 where
= fragP
->fr_literal
+ fragP
->fr_fix
;
2187 bfd_putl16 (cc
->lop0
, where
);
2188 bfd_putl16 (cc
->lop1
, where
+ 2);
2189 rela
= BFD_RELOC_MSP430_RL_PCREL
;
2194 case ENCODE_RELAX (STATE_NOOV_BRANCH
, STATE_WORD
):
2195 case ENCODE_RELAX (STATE_NOOV_BRANCH
, STATE_UNDEF
):
2196 cc
= & msp430_rcodes
[6];
2197 where
= fragP
->fr_literal
+ fragP
->fr_fix
;
2198 bfd_putl16 (cc
->lop0
, where
);
2199 bfd_putl16 (cc
->lop1
, where
+ 2);
2200 bfd_putl16 (cc
->lop2
, where
+ 4);
2201 rela
= BFD_RELOC_MSP430_RL_PCREL
;
2205 case ENCODE_RELAX (STATE_EMUL_BRANCH
, STATE_BITS10
):
2207 int insn
= bfd_getl16 (fragP
->fr_opcode
+ 2);
2210 for (i
= 0; i
< 4 && !hc
; i
++)
2211 if (msp430_hcodes
[i
].op1
== insn
)
2212 hc
= &msp430_hcodes
[i
];
2213 if (!hc
|| !hc
->name
)
2214 as_fatal (_("internal inconsistency problem in %s: ext. insn %04lx"),
2215 __FUNCTION__
, (long) insn
);
2216 rela
= BFD_RELOC_MSP430_10_PCREL
;
2217 /* Apply a fix for a first label if necessary.
2218 another fix will be applied to the next word of insn anyway. */
2220 fix_new (fragP
, fragP
->fr_fix
, 2, fragP
->fr_symbol
,
2221 fragP
->fr_offset
, TRUE
, rela
);
2227 case ENCODE_RELAX (STATE_EMUL_BRANCH
, STATE_WORD
):
2228 case ENCODE_RELAX (STATE_EMUL_BRANCH
, STATE_UNDEF
):
2230 int insn
= bfd_getl16 (fragP
->fr_opcode
+ 2);
2233 for (i
= 0; i
< 4 && !hc
; i
++)
2234 if (msp430_hcodes
[i
].op1
== insn
)
2235 hc
= & msp430_hcodes
[i
];
2236 if (!hc
|| !hc
->name
)
2237 as_fatal (_("internal inconsistency problem in %s: ext. insn %04lx"),
2238 __FUNCTION__
, (long) insn
);
2239 rela
= BFD_RELOC_MSP430_RL_PCREL
;
2240 where
= fragP
->fr_literal
+ fragP
->fr_fix
;
2241 bfd_putl16 (hc
->lop0
, where
);
2242 bfd_putl16 (hc
->lop1
, where
+ 2);
2243 bfd_putl16 (hc
->lop2
, where
+ 4);
2249 as_fatal (_("internal inconsistency problem in %s: %lx"),
2250 __FUNCTION__
, (long) fragP
->fr_subtype
);
2254 /* Now apply fix. */
2255 fix_new (fragP
, fragP
->fr_fix
, 2, fragP
->fr_symbol
,
2256 fragP
->fr_offset
, TRUE
, rela
);
2257 /* Just fixed 2 bytes. */
2261 /* Relax fragment. Mostly stolen from hc11 and mcore
2262 which arches I think I know. */
2265 msp430_relax_frag (segT seg ATTRIBUTE_UNUSED
, fragS
* fragP
,
2266 long stretch ATTRIBUTE_UNUSED
)
2271 const relax_typeS
*this_type
;
2272 const relax_typeS
*start_type
;
2273 relax_substateT next_state
;
2274 relax_substateT this_state
;
2275 const relax_typeS
*table
= md_relax_table
;
2277 /* Nothing to be done if the frag has already max size. */
2278 if (RELAX_STATE (fragP
->fr_subtype
) == STATE_UNDEF
2279 || RELAX_STATE (fragP
->fr_subtype
) == STATE_WORD
)
2282 if (RELAX_STATE (fragP
->fr_subtype
) == STATE_BITS10
)
2284 symbolP
= fragP
->fr_symbol
;
2285 if (symbol_resolved_p (symbolP
))
2286 as_fatal (_("internal inconsistency problem in %s: resolved symbol"),
2288 /* We know the offset. calculate a distance. */
2289 aim
= S_GET_VALUE (symbolP
) - fragP
->fr_address
- fragP
->fr_fix
;
2292 if (!msp430_enable_relax
)
2294 /* Relaxation is not enabled. So, make all jump as long ones
2295 by setting 'aim' to quite high value. */
2299 this_state
= fragP
->fr_subtype
;
2300 start_type
= this_type
= table
+ this_state
;
2304 /* Look backwards. */
2305 for (next_state
= this_type
->rlx_more
; next_state
;)
2306 if (aim
>= this_type
->rlx_backward
|| !this_type
->rlx_backward
)
2310 /* Grow to next state. */
2311 this_state
= next_state
;
2312 this_type
= table
+ this_state
;
2313 next_state
= this_type
->rlx_more
;
2318 /* Look forwards. */
2319 for (next_state
= this_type
->rlx_more
; next_state
;)
2320 if (aim
<= this_type
->rlx_forward
|| !this_type
->rlx_forward
)
2324 /* Grow to next state. */
2325 this_state
= next_state
;
2326 this_type
= table
+ this_state
;
2327 next_state
= this_type
->rlx_more
;
2331 growth
= this_type
->rlx_length
- start_type
->rlx_length
;
2333 fragP
->fr_subtype
= this_state
;