Regenerate AArch64 opcodes files
[binutils-gdb.git] / gdb / msp430-tdep.c
blobe07e3cc4be2b8b367132cdded90b648a5d991897
1 /* Target-dependent code for the Texas Instruments MSP430 for GDB, the
2 GNU debugger.
4 Copyright (C) 2012-2024 Free Software Foundation, Inc.
6 Contributed by Red Hat, Inc.
8 This file is part of GDB.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "defs.h"
24 #include "arch-utils.h"
25 #include "prologue-value.h"
26 #include "target.h"
27 #include "regcache.h"
28 #include "dis-asm.h"
29 #include "gdbtypes.h"
30 #include "frame.h"
31 #include "frame-unwind.h"
32 #include "frame-base.h"
33 #include "value.h"
34 #include "gdbcore.h"
35 #include "dwarf2/frame.h"
36 #include "reggroups.h"
37 #include "gdbarch.h"
38 #include "inferior.h"
40 #include "elf/msp430.h"
41 #include "opcode/msp430-decode.h"
42 #include "elf-bfd.h"
44 /* Register Numbers. */
46 enum
48 MSP430_PC_RAW_REGNUM,
49 MSP430_SP_RAW_REGNUM,
50 MSP430_SR_RAW_REGNUM,
51 MSP430_CG_RAW_REGNUM,
52 MSP430_R4_RAW_REGNUM,
53 MSP430_R5_RAW_REGNUM,
54 MSP430_R6_RAW_REGNUM,
55 MSP430_R7_RAW_REGNUM,
56 MSP430_R8_RAW_REGNUM,
57 MSP430_R9_RAW_REGNUM,
58 MSP430_R10_RAW_REGNUM,
59 MSP430_R11_RAW_REGNUM,
60 MSP430_R12_RAW_REGNUM,
61 MSP430_R13_RAW_REGNUM,
62 MSP430_R14_RAW_REGNUM,
63 MSP430_R15_RAW_REGNUM,
65 MSP430_NUM_REGS,
67 MSP430_PC_REGNUM = MSP430_NUM_REGS,
68 MSP430_SP_REGNUM,
69 MSP430_SR_REGNUM,
70 MSP430_CG_REGNUM,
71 MSP430_R4_REGNUM,
72 MSP430_R5_REGNUM,
73 MSP430_R6_REGNUM,
74 MSP430_R7_REGNUM,
75 MSP430_R8_REGNUM,
76 MSP430_R9_REGNUM,
77 MSP430_R10_REGNUM,
78 MSP430_R11_REGNUM,
79 MSP430_R12_REGNUM,
80 MSP430_R13_REGNUM,
81 MSP430_R14_REGNUM,
82 MSP430_R15_REGNUM,
84 MSP430_NUM_TOTAL_REGS,
85 MSP430_NUM_PSEUDO_REGS = MSP430_NUM_TOTAL_REGS - MSP430_NUM_REGS
88 enum
90 /* TI MSP430 Architecture. */
91 MSP_ISA_MSP430,
93 /* TI MSP430X Architecture. */
94 MSP_ISA_MSP430X
97 enum
99 /* The small code model limits code addresses to 16 bits. */
100 MSP_SMALL_CODE_MODEL,
102 /* The large code model uses 20 bit addresses for function
103 pointers. These are stored in memory using four bytes (32 bits). */
104 MSP_LARGE_CODE_MODEL
107 /* Architecture specific data. */
109 struct msp430_gdbarch_tdep : gdbarch_tdep_base
111 /* The ELF header flags specify the multilib used. */
112 int elf_flags = 0;
114 /* One of MSP_ISA_MSP430 or MSP_ISA_MSP430X. */
115 int isa = 0;
117 /* One of MSP_SMALL_CODE_MODEL or MSP_LARGE_CODE_MODEL. If, at
118 some point, we support different data models too, we'll probably
119 structure things so that we can combine values using logical
120 "or". */
121 int code_model = 0;
124 /* This structure holds the results of a prologue analysis. */
126 struct msp430_prologue
128 /* The offset from the frame base to the stack pointer --- always
129 zero or negative.
131 Calling this a "size" is a bit misleading, but given that the
132 stack grows downwards, using offsets for everything keeps one
133 from going completely sign-crazy: you never change anything's
134 sign for an ADD instruction; always change the second operand's
135 sign for a SUB instruction; and everything takes care of
136 itself. */
137 int frame_size;
139 /* Non-zero if this function has initialized the frame pointer from
140 the stack pointer, zero otherwise. */
141 int has_frame_ptr;
143 /* If has_frame_ptr is non-zero, this is the offset from the frame
144 base to where the frame pointer points. This is always zero or
145 negative. */
146 int frame_ptr_offset;
148 /* The address of the first instruction at which the frame has been
149 set up and the arguments are where the debug info says they are
150 --- as best as we can tell. */
151 CORE_ADDR prologue_end;
153 /* reg_offset[R] is the offset from the CFA at which register R is
154 saved, or 1 if register R has not been saved. (Real values are
155 always zero or negative.) */
156 int reg_offset[MSP430_NUM_TOTAL_REGS];
159 /* Implement the "register_type" gdbarch method. */
161 static struct type *
162 msp430_register_type (struct gdbarch *gdbarch, int reg_nr)
164 if (reg_nr < MSP430_NUM_REGS)
165 return builtin_type (gdbarch)->builtin_uint32;
166 else if (reg_nr == MSP430_PC_REGNUM)
167 return builtin_type (gdbarch)->builtin_func_ptr;
168 else
169 return builtin_type (gdbarch)->builtin_uint16;
172 /* Implement another version of the "register_type" gdbarch method
173 for msp430x. */
175 static struct type *
176 msp430x_register_type (struct gdbarch *gdbarch, int reg_nr)
178 if (reg_nr < MSP430_NUM_REGS)
179 return builtin_type (gdbarch)->builtin_uint32;
180 else if (reg_nr == MSP430_PC_REGNUM)
181 return builtin_type (gdbarch)->builtin_func_ptr;
182 else
183 return builtin_type (gdbarch)->builtin_uint32;
186 /* Implement the "register_name" gdbarch method. */
188 static const char *
189 msp430_register_name (struct gdbarch *gdbarch, int regnr)
191 static const char *const reg_names[] = {
192 /* Raw registers. */
193 "", "", "", "", "", "", "", "",
194 "", "", "", "", "", "", "", "",
195 /* Pseudo registers. */
196 "pc", "sp", "sr", "cg", "r4", "r5", "r6", "r7",
197 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
200 static_assert (ARRAY_SIZE (reg_names) == (MSP430_NUM_REGS
201 + MSP430_NUM_PSEUDO_REGS));
202 return reg_names[regnr];
205 /* Implement the "register_reggroup_p" gdbarch method. */
207 static int
208 msp430_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
209 const struct reggroup *group)
211 if (group == all_reggroup)
212 return 1;
214 /* All other registers are saved and restored. */
215 if (group == save_reggroup || group == restore_reggroup)
216 return (MSP430_NUM_REGS <= regnum && regnum < MSP430_NUM_TOTAL_REGS);
218 return group == general_reggroup;
221 /* Implement the "pseudo_register_read" gdbarch method. */
223 static enum register_status
224 msp430_pseudo_register_read (struct gdbarch *gdbarch,
225 readable_regcache *regcache,
226 int regnum, gdb_byte *buffer)
228 if (MSP430_NUM_REGS <= regnum && regnum < MSP430_NUM_TOTAL_REGS)
230 enum register_status status;
231 ULONGEST val;
232 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
233 int regsize = register_size (gdbarch, regnum);
234 int raw_regnum = regnum - MSP430_NUM_REGS;
236 status = regcache->raw_read (raw_regnum, &val);
237 if (status == REG_VALID)
238 store_unsigned_integer (buffer, regsize, byte_order, val);
240 return status;
242 else
243 gdb_assert_not_reached ("invalid pseudo register number");
246 /* Implement the "pseudo_register_write" gdbarch method. */
248 static void
249 msp430_pseudo_register_write (struct gdbarch *gdbarch,
250 struct regcache *regcache,
251 int regnum, const gdb_byte *buffer)
253 if (MSP430_NUM_REGS <= regnum && regnum < MSP430_NUM_TOTAL_REGS)
256 ULONGEST val;
257 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
258 int regsize = register_size (gdbarch, regnum);
259 int raw_regnum = regnum - MSP430_NUM_REGS;
261 val = extract_unsigned_integer (buffer, regsize, byte_order);
262 regcache_raw_write_unsigned (regcache, raw_regnum, val);
265 else
266 gdb_assert_not_reached ("invalid pseudo register number");
269 /* Implement the `register_sim_regno' gdbarch method. */
271 static int
272 msp430_register_sim_regno (struct gdbarch *gdbarch, int regnum)
274 gdb_assert (regnum < MSP430_NUM_REGS);
276 /* So long as regnum is in [0, RL78_NUM_REGS), it's valid. We
277 just want to override the default here which disallows register
278 numbers which have no names. */
279 return regnum;
282 constexpr gdb_byte msp430_break_insn[] = { 0x43, 0x43 };
284 typedef BP_MANIPULATION (msp430_break_insn) msp430_breakpoint;
286 /* Define a "handle" struct for fetching the next opcode. */
288 struct msp430_get_opcode_byte_handle
290 CORE_ADDR pc;
293 /* Fetch a byte on behalf of the opcode decoder. HANDLE contains
294 the memory address of the next byte to fetch. If successful,
295 the address in the handle is updated and the byte fetched is
296 returned as the value of the function. If not successful, -1
297 is returned. */
299 static int
300 msp430_get_opcode_byte (void *handle)
302 struct msp430_get_opcode_byte_handle *opcdata
303 = (struct msp430_get_opcode_byte_handle *) handle;
304 int status;
305 gdb_byte byte;
307 status = target_read_memory (opcdata->pc, &byte, 1);
308 if (status == 0)
310 opcdata->pc += 1;
311 return byte;
313 else
314 return -1;
317 /* Function for finding saved registers in a 'struct pv_area'; this
318 function is passed to pv_area::scan.
320 If VALUE is a saved register, ADDR says it was saved at a constant
321 offset from the frame base, and SIZE indicates that the whole
322 register was saved, record its offset. */
324 static void
325 check_for_saved (void *result_untyped, pv_t addr, CORE_ADDR size, pv_t value)
327 struct msp430_prologue *result = (struct msp430_prologue *) result_untyped;
329 if (value.kind == pvk_register
330 && value.k == 0
331 && pv_is_register (addr, MSP430_SP_REGNUM)
332 && size == register_size (current_inferior ()->arch (), value.reg))
333 result->reg_offset[value.reg] = addr.k;
336 /* Analyze a prologue starting at START_PC, going no further than
337 LIMIT_PC. Fill in RESULT as appropriate. */
339 static void
340 msp430_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc,
341 CORE_ADDR limit_pc, struct msp430_prologue *result)
343 CORE_ADDR pc, next_pc;
344 int rn;
345 pv_t reg[MSP430_NUM_TOTAL_REGS];
346 CORE_ADDR after_last_frame_setup_insn = start_pc;
347 msp430_gdbarch_tdep *tdep = gdbarch_tdep<msp430_gdbarch_tdep> (gdbarch);
348 int code_model = tdep->code_model;
349 int sz;
351 memset (result, 0, sizeof (*result));
353 for (rn = 0; rn < MSP430_NUM_TOTAL_REGS; rn++)
355 reg[rn] = pv_register (rn, 0);
356 result->reg_offset[rn] = 1;
359 pv_area stack (MSP430_SP_REGNUM, gdbarch_addr_bit (gdbarch));
361 /* The call instruction has saved the return address on the stack. */
362 sz = code_model == MSP_LARGE_CODE_MODEL ? 4 : 2;
363 reg[MSP430_SP_REGNUM] = pv_add_constant (reg[MSP430_SP_REGNUM], -sz);
364 stack.store (reg[MSP430_SP_REGNUM], sz, reg[MSP430_PC_REGNUM]);
366 pc = start_pc;
367 while (pc < limit_pc)
369 int bytes_read;
370 struct msp430_get_opcode_byte_handle opcode_handle;
371 MSP430_Opcode_Decoded opc;
373 opcode_handle.pc = pc;
374 bytes_read = msp430_decode_opcode (pc, &opc, msp430_get_opcode_byte,
375 &opcode_handle);
376 next_pc = pc + bytes_read;
378 if (opc.id == MSO_push && opc.op[0].type == MSP430_Operand_Register)
380 int rsrc = opc.op[0].reg;
382 reg[MSP430_SP_REGNUM] = pv_add_constant (reg[MSP430_SP_REGNUM], -2);
383 stack.store (reg[MSP430_SP_REGNUM], 2, reg[rsrc]);
384 after_last_frame_setup_insn = next_pc;
386 else if (opc.id == MSO_push /* PUSHM */
387 && opc.op[0].type == MSP430_Operand_None
388 && opc.op[1].type == MSP430_Operand_Register)
390 int rsrc = opc.op[1].reg;
391 int count = opc.repeats + 1;
392 int size = opc.size == 16 ? 2 : 4;
394 while (count > 0)
396 reg[MSP430_SP_REGNUM]
397 = pv_add_constant (reg[MSP430_SP_REGNUM], -size);
398 stack.store (reg[MSP430_SP_REGNUM], size, reg[rsrc]);
399 rsrc--;
400 count--;
402 after_last_frame_setup_insn = next_pc;
404 else if (opc.id == MSO_sub
405 && opc.op[0].type == MSP430_Operand_Register
406 && opc.op[0].reg == MSR_SP
407 && opc.op[1].type == MSP430_Operand_Immediate)
409 int addend = opc.op[1].addend;
411 reg[MSP430_SP_REGNUM] = pv_add_constant (reg[MSP430_SP_REGNUM],
412 -addend);
413 after_last_frame_setup_insn = next_pc;
415 else if (opc.id == MSO_mov
416 && opc.op[0].type == MSP430_Operand_Immediate
417 && 12 <= opc.op[0].reg && opc.op[0].reg <= 15)
418 after_last_frame_setup_insn = next_pc;
419 else
421 /* Terminate the prologue scan. */
422 break;
425 pc = next_pc;
428 /* Is the frame size (offset, really) a known constant? */
429 if (pv_is_register (reg[MSP430_SP_REGNUM], MSP430_SP_REGNUM))
430 result->frame_size = reg[MSP430_SP_REGNUM].k;
432 /* Record where all the registers were saved. */
433 stack.scan (check_for_saved, result);
435 result->prologue_end = after_last_frame_setup_insn;
438 /* Implement the "skip_prologue" gdbarch method. */
440 static CORE_ADDR
441 msp430_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
443 const char *name;
444 CORE_ADDR func_addr, func_end;
445 struct msp430_prologue p;
447 /* Try to find the extent of the function that contains PC. */
448 if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
449 return pc;
451 msp430_analyze_prologue (gdbarch, pc, func_end, &p);
452 return p.prologue_end;
455 /* Given a frame described by THIS_FRAME, decode the prologue of its
456 associated function if there is not cache entry as specified by
457 THIS_PROLOGUE_CACHE. Save the decoded prologue in the cache and
458 return that struct as the value of this function. */
460 static struct msp430_prologue *
461 msp430_analyze_frame_prologue (const frame_info_ptr &this_frame,
462 void **this_prologue_cache)
464 if (!*this_prologue_cache)
466 CORE_ADDR func_start, stop_addr;
468 *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct msp430_prologue);
470 func_start = get_frame_func (this_frame);
471 stop_addr = get_frame_pc (this_frame);
473 /* If we couldn't find any function containing the PC, then
474 just initialize the prologue cache, but don't do anything. */
475 if (!func_start)
476 stop_addr = func_start;
478 msp430_analyze_prologue (get_frame_arch (this_frame), func_start,
479 stop_addr,
480 (struct msp430_prologue *) *this_prologue_cache);
483 return (struct msp430_prologue *) *this_prologue_cache;
486 /* Given a frame and a prologue cache, return this frame's base. */
488 static CORE_ADDR
489 msp430_frame_base (const frame_info_ptr &this_frame, void **this_prologue_cache)
491 struct msp430_prologue *p
492 = msp430_analyze_frame_prologue (this_frame, this_prologue_cache);
493 CORE_ADDR sp = get_frame_register_unsigned (this_frame, MSP430_SP_REGNUM);
495 return sp - p->frame_size;
498 /* Implement the "frame_this_id" method for unwinding frames. */
500 static void
501 msp430_this_id (const frame_info_ptr &this_frame,
502 void **this_prologue_cache, struct frame_id *this_id)
504 *this_id = frame_id_build (msp430_frame_base (this_frame,
505 this_prologue_cache),
506 get_frame_func (this_frame));
509 /* Implement the "frame_prev_register" method for unwinding frames. */
511 static struct value *
512 msp430_prev_register (const frame_info_ptr &this_frame,
513 void **this_prologue_cache, int regnum)
515 struct msp430_prologue *p
516 = msp430_analyze_frame_prologue (this_frame, this_prologue_cache);
517 CORE_ADDR frame_base = msp430_frame_base (this_frame, this_prologue_cache);
519 if (regnum == MSP430_SP_REGNUM)
520 return frame_unwind_got_constant (this_frame, regnum, frame_base);
522 /* If prologue analysis says we saved this register somewhere,
523 return a description of the stack slot holding it. */
524 else if (p->reg_offset[regnum] != 1)
526 struct value *rv = frame_unwind_got_memory (this_frame, regnum,
527 frame_base +
528 p->reg_offset[regnum]);
530 if (regnum == MSP430_PC_REGNUM)
532 ULONGEST pc = value_as_long (rv);
534 return frame_unwind_got_constant (this_frame, regnum, pc);
536 return rv;
539 /* Otherwise, presume we haven't changed the value of this
540 register, and get it from the next frame. */
541 else
542 return frame_unwind_got_register (this_frame, regnum, regnum);
545 static const struct frame_unwind msp430_unwind = {
546 "msp430 prologue",
547 NORMAL_FRAME,
548 default_frame_unwind_stop_reason,
549 msp430_this_id,
550 msp430_prev_register,
551 NULL,
552 default_frame_sniffer
555 /* Implement the "dwarf2_reg_to_regnum" gdbarch method. */
557 static int
558 msp430_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int reg)
560 if (reg >= 0 && reg < MSP430_NUM_REGS)
561 return reg + MSP430_NUM_REGS;
562 return -1;
565 /* Implement the "return_value" gdbarch method. */
567 static enum return_value_convention
568 msp430_return_value (struct gdbarch *gdbarch,
569 struct value *function,
570 struct type *valtype,
571 struct regcache *regcache,
572 gdb_byte *readbuf, const gdb_byte *writebuf)
574 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
575 LONGEST valtype_len = valtype->length ();
576 msp430_gdbarch_tdep *tdep = gdbarch_tdep<msp430_gdbarch_tdep> (gdbarch);
577 int code_model = tdep->code_model;
579 if (valtype->length () > 8
580 || valtype->code () == TYPE_CODE_STRUCT
581 || valtype->code () == TYPE_CODE_UNION)
582 return RETURN_VALUE_STRUCT_CONVENTION;
584 if (readbuf)
586 ULONGEST u;
587 int argreg = MSP430_R12_REGNUM;
588 int offset = 0;
590 while (valtype_len > 0)
592 int size = 2;
594 if (code_model == MSP_LARGE_CODE_MODEL
595 && valtype->code () == TYPE_CODE_PTR)
597 size = 4;
600 regcache_cooked_read_unsigned (regcache, argreg, &u);
601 store_unsigned_integer (readbuf + offset, size, byte_order, u);
602 valtype_len -= size;
603 offset += size;
604 argreg++;
608 if (writebuf)
610 ULONGEST u;
611 int argreg = MSP430_R12_REGNUM;
612 int offset = 0;
614 while (valtype_len > 0)
616 int size = 2;
618 if (code_model == MSP_LARGE_CODE_MODEL
619 && valtype->code () == TYPE_CODE_PTR)
621 size = 4;
624 u = extract_unsigned_integer (writebuf + offset, size, byte_order);
625 regcache_cooked_write_unsigned (regcache, argreg, u);
626 valtype_len -= size;
627 offset += size;
628 argreg++;
632 return RETURN_VALUE_REGISTER_CONVENTION;
636 /* Implement the "frame_align" gdbarch method. */
638 static CORE_ADDR
639 msp430_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
641 return align_down (sp, 2);
644 /* Implement the "push_dummy_call" gdbarch method. */
646 static CORE_ADDR
647 msp430_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
648 struct regcache *regcache, CORE_ADDR bp_addr,
649 int nargs, struct value **args, CORE_ADDR sp,
650 function_call_return_method return_method,
651 CORE_ADDR struct_addr)
653 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
654 int write_pass;
655 int sp_off = 0;
656 CORE_ADDR cfa;
657 msp430_gdbarch_tdep *tdep = gdbarch_tdep<msp430_gdbarch_tdep> (gdbarch);
658 int code_model = tdep->code_model;
660 struct type *func_type = function->type ();
662 /* Dereference function pointer types. */
663 while (func_type->code () == TYPE_CODE_PTR)
664 func_type = func_type->target_type ();
666 /* The end result had better be a function or a method. */
667 gdb_assert (func_type->code () == TYPE_CODE_FUNC
668 || func_type->code () == TYPE_CODE_METHOD);
670 /* We make two passes; the first does the stack allocation,
671 the second actually stores the arguments. */
672 for (write_pass = 0; write_pass <= 1; write_pass++)
674 int i;
675 int arg_reg = MSP430_R12_REGNUM;
676 int args_on_stack = 0;
678 if (write_pass)
679 sp = align_down (sp - sp_off, 4);
680 sp_off = 0;
682 if (return_method == return_method_struct)
684 if (write_pass)
685 regcache_cooked_write_unsigned (regcache, arg_reg, struct_addr);
686 arg_reg++;
689 /* Push the arguments. */
690 for (i = 0; i < nargs; i++)
692 struct value *arg = args[i];
693 const gdb_byte *arg_bits = arg->contents_all ().data ();
694 struct type *arg_type = check_typedef (arg->type ());
695 ULONGEST arg_size = arg_type->length ();
696 int offset;
697 int current_arg_on_stack;
698 gdb_byte struct_addr_buf[4];
700 current_arg_on_stack = 0;
702 if (arg_type->code () == TYPE_CODE_STRUCT
703 || arg_type->code () == TYPE_CODE_UNION)
705 /* Aggregates of any size are passed by reference. */
706 store_unsigned_integer (struct_addr_buf, 4, byte_order,
707 arg->address ());
708 arg_bits = struct_addr_buf;
709 arg_size = (code_model == MSP_LARGE_CODE_MODEL) ? 4 : 2;
711 else
713 /* Scalars bigger than 8 bytes such as complex doubles are passed
714 on the stack. */
715 if (arg_size > 8)
716 current_arg_on_stack = 1;
720 for (offset = 0; offset < arg_size; offset += 2)
722 /* The condition below prevents 8 byte scalars from being split
723 between registers and memory (stack). It also prevents other
724 splits once the stack has been written to. */
725 if (!current_arg_on_stack
726 && (arg_reg
727 + ((arg_size == 8 || args_on_stack)
728 ? ((arg_size - offset) / 2 - 1)
729 : 0) <= MSP430_R15_REGNUM))
731 int size = 2;
733 if (code_model == MSP_LARGE_CODE_MODEL
734 && (arg_type->code () == TYPE_CODE_PTR
735 || TYPE_IS_REFERENCE (arg_type)
736 || arg_type->code () == TYPE_CODE_STRUCT
737 || arg_type->code () == TYPE_CODE_UNION))
739 /* When using the large memory model, pointer,
740 reference, struct, and union arguments are
741 passed using the entire register. (As noted
742 earlier, aggregates are always passed by
743 reference.) */
744 if (offset != 0)
745 continue;
746 size = 4;
749 if (write_pass)
750 regcache_cooked_write_unsigned (regcache, arg_reg,
751 extract_unsigned_integer
752 (arg_bits + offset, size,
753 byte_order));
755 arg_reg++;
757 else
759 if (write_pass)
760 write_memory (sp + sp_off, arg_bits + offset, 2);
762 sp_off += 2;
763 args_on_stack = 1;
764 current_arg_on_stack = 1;
770 /* Keep track of the stack address prior to pushing the return address.
771 This is the value that we'll return. */
772 cfa = sp;
774 /* Push the return address. */
776 int sz = tdep->code_model == MSP_SMALL_CODE_MODEL ? 2 : 4;
777 sp = sp - sz;
778 write_memory_unsigned_integer (sp, sz, byte_order, bp_addr);
781 /* Update the stack pointer. */
782 regcache_cooked_write_unsigned (regcache, MSP430_SP_REGNUM, sp);
784 return cfa;
787 /* In order to keep code size small, the compiler may create epilogue
788 code through which more than one function epilogue is routed. I.e.
789 the epilogue and return may just be a branch to some common piece of
790 code which is responsible for tearing down the frame and performing
791 the return. These epilog (label) names will have the common prefix
792 defined here. */
794 static const char msp430_epilog_name_prefix[] = "__mspabi_func_epilog_";
796 /* Implement the "in_return_stub" gdbarch method. */
798 static int
799 msp430_in_return_stub (struct gdbarch *gdbarch, CORE_ADDR pc,
800 const char *name)
802 return (name != NULL
803 && startswith (name, msp430_epilog_name_prefix));
806 /* Implement the "skip_trampoline_code" gdbarch method. */
807 static CORE_ADDR
808 msp430_skip_trampoline_code (const frame_info_ptr &frame, CORE_ADDR pc)
810 struct bound_minimal_symbol bms;
811 const char *stub_name;
812 struct gdbarch *gdbarch = get_frame_arch (frame);
814 bms = lookup_minimal_symbol_by_pc (pc);
815 if (!bms.minsym)
816 return pc;
818 stub_name = bms.minsym->linkage_name ();
820 msp430_gdbarch_tdep *tdep = gdbarch_tdep<msp430_gdbarch_tdep> (gdbarch);
821 if (tdep->code_model == MSP_SMALL_CODE_MODEL
822 && msp430_in_return_stub (gdbarch, pc, stub_name))
824 CORE_ADDR sp = get_frame_register_unsigned (frame, MSP430_SP_REGNUM);
826 return read_memory_integer
827 (sp + 2 * (stub_name[strlen (msp430_epilog_name_prefix)] - '0'),
828 2, gdbarch_byte_order (gdbarch));
831 return pc;
834 /* Allocate and initialize a gdbarch object. */
836 static struct gdbarch *
837 msp430_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
839 int elf_flags, isa, code_model;
841 /* Extract the elf_flags if available. */
842 if (info.abfd != NULL
843 && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
844 elf_flags = elf_elfheader (info.abfd)->e_flags;
845 else
846 elf_flags = 0;
848 if (info.abfd != NULL)
849 switch (bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
850 OFBA_MSPABI_Tag_ISA))
852 case 1:
853 isa = MSP_ISA_MSP430;
854 code_model = MSP_SMALL_CODE_MODEL;
855 break;
856 case 2:
857 isa = MSP_ISA_MSP430X;
858 switch (bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
859 OFBA_MSPABI_Tag_Code_Model))
861 case 1:
862 code_model = MSP_SMALL_CODE_MODEL;
863 break;
864 case 2:
865 code_model = MSP_LARGE_CODE_MODEL;
866 break;
867 default:
868 internal_error (_("Unknown msp430x code memory model"));
869 break;
871 break;
872 case 0:
873 /* This can happen when loading a previously dumped data structure.
874 Use the ISA and code model from the current architecture, provided
875 it's compatible. */
877 struct gdbarch *ca = get_current_arch ();
878 if (ca && gdbarch_bfd_arch_info (ca)->arch == bfd_arch_msp430)
880 msp430_gdbarch_tdep *ca_tdep
881 = gdbarch_tdep<msp430_gdbarch_tdep> (ca);
883 elf_flags = ca_tdep->elf_flags;
884 isa = ca_tdep->isa;
885 code_model = ca_tdep->code_model;
886 break;
889 [[fallthrough]];
890 default:
891 error (_("Unknown msp430 isa"));
892 break;
894 else
896 isa = MSP_ISA_MSP430;
897 code_model = MSP_SMALL_CODE_MODEL;
901 /* Try to find the architecture in the list of already defined
902 architectures. */
903 for (arches = gdbarch_list_lookup_by_info (arches, &info);
904 arches != NULL;
905 arches = gdbarch_list_lookup_by_info (arches->next, &info))
907 msp430_gdbarch_tdep *candidate_tdep
908 = gdbarch_tdep<msp430_gdbarch_tdep> (arches->gdbarch);
910 if (candidate_tdep->elf_flags != elf_flags
911 || candidate_tdep->isa != isa
912 || candidate_tdep->code_model != code_model)
913 continue;
915 return arches->gdbarch;
918 /* None found, create a new architecture from the information
919 provided. */
920 gdbarch *gdbarch
921 = gdbarch_alloc (&info, gdbarch_tdep_up (new msp430_gdbarch_tdep));
922 msp430_gdbarch_tdep *tdep = gdbarch_tdep<msp430_gdbarch_tdep> (gdbarch);
924 tdep->elf_flags = elf_flags;
925 tdep->isa = isa;
926 tdep->code_model = code_model;
928 /* Registers. */
929 set_gdbarch_num_regs (gdbarch, MSP430_NUM_REGS);
930 set_gdbarch_num_pseudo_regs (gdbarch, MSP430_NUM_PSEUDO_REGS);
931 set_gdbarch_register_name (gdbarch, msp430_register_name);
932 if (isa == MSP_ISA_MSP430)
933 set_gdbarch_register_type (gdbarch, msp430_register_type);
934 else
935 set_gdbarch_register_type (gdbarch, msp430x_register_type);
936 set_gdbarch_pc_regnum (gdbarch, MSP430_PC_REGNUM);
937 set_gdbarch_sp_regnum (gdbarch, MSP430_SP_REGNUM);
938 set_gdbarch_register_reggroup_p (gdbarch, msp430_register_reggroup_p);
939 set_gdbarch_pseudo_register_read (gdbarch, msp430_pseudo_register_read);
940 set_gdbarch_deprecated_pseudo_register_write (gdbarch,
941 msp430_pseudo_register_write);
942 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, msp430_dwarf2_reg_to_regnum);
943 set_gdbarch_register_sim_regno (gdbarch, msp430_register_sim_regno);
945 /* Data types. */
946 set_gdbarch_char_signed (gdbarch, 0);
947 set_gdbarch_short_bit (gdbarch, 16);
948 set_gdbarch_int_bit (gdbarch, 16);
949 set_gdbarch_long_bit (gdbarch, 32);
950 set_gdbarch_long_long_bit (gdbarch, 64);
951 if (code_model == MSP_SMALL_CODE_MODEL)
953 set_gdbarch_ptr_bit (gdbarch, 16);
954 set_gdbarch_addr_bit (gdbarch, 16);
956 else /* MSP_LARGE_CODE_MODEL */
958 set_gdbarch_ptr_bit (gdbarch, 32);
959 set_gdbarch_addr_bit (gdbarch, 32);
961 set_gdbarch_dwarf2_addr_size (gdbarch, 4);
962 set_gdbarch_float_bit (gdbarch, 32);
963 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
964 set_gdbarch_double_bit (gdbarch, 64);
965 set_gdbarch_long_double_bit (gdbarch, 64);
966 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
967 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
969 /* Breakpoints. */
970 set_gdbarch_breakpoint_kind_from_pc (gdbarch,
971 msp430_breakpoint::kind_from_pc);
972 set_gdbarch_sw_breakpoint_from_kind (gdbarch,
973 msp430_breakpoint::bp_from_kind);
974 set_gdbarch_decr_pc_after_break (gdbarch, 1);
976 /* Frames, prologues, etc. */
977 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
978 set_gdbarch_skip_prologue (gdbarch, msp430_skip_prologue);
979 set_gdbarch_frame_align (gdbarch, msp430_frame_align);
980 dwarf2_append_unwinders (gdbarch);
981 frame_unwind_append_unwinder (gdbarch, &msp430_unwind);
983 /* Dummy frames, return values. */
984 set_gdbarch_push_dummy_call (gdbarch, msp430_push_dummy_call);
985 set_gdbarch_return_value (gdbarch, msp430_return_value);
987 /* Trampolines. */
988 set_gdbarch_in_solib_return_trampoline (gdbarch, msp430_in_return_stub);
989 set_gdbarch_skip_trampoline_code (gdbarch, msp430_skip_trampoline_code);
991 /* Virtual tables. */
992 set_gdbarch_vbit_in_delta (gdbarch, 0);
994 return gdbarch;
997 /* Register the initialization routine. */
999 void _initialize_msp430_tdep ();
1000 void
1001 _initialize_msp430_tdep ()
1003 gdbarch_register (bfd_arch_msp430, msp430_gdbarch_init);