Remove path name from test case
[binutils-gdb.git] / gdb / msp430-tdep.c
1 /* Target-dependent code for the Texas Instruments MSP430 for GDB, the
2 GNU debugger.
3
4 Copyright (C) 2012-2023 Free Software Foundation, Inc.
5
6 Contributed by Red Hat, Inc.
7
8 This file is part of GDB.
9
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.
14
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.
19
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/>. */
22
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"
39
40 #include "elf/msp430.h"
41 #include "opcode/msp430-decode.h"
42 #include "elf-bfd.h"
43
44 /* Register Numbers. */
45
46 enum
47 {
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,
64
65 MSP430_NUM_REGS,
66
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,
83
84 MSP430_NUM_TOTAL_REGS,
85 MSP430_NUM_PSEUDO_REGS = MSP430_NUM_TOTAL_REGS - MSP430_NUM_REGS
86 };
87
88 enum
89 {
90 /* TI MSP430 Architecture. */
91 MSP_ISA_MSP430,
92
93 /* TI MSP430X Architecture. */
94 MSP_ISA_MSP430X
95 };
96
97 enum
98 {
99 /* The small code model limits code addresses to 16 bits. */
100 MSP_SMALL_CODE_MODEL,
101
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
105 };
106
107 /* Architecture specific data. */
108
109 struct msp430_gdbarch_tdep : gdbarch_tdep_base
110 {
111 /* The ELF header flags specify the multilib used. */
112 int elf_flags = 0;
113
114 /* One of MSP_ISA_MSP430 or MSP_ISA_MSP430X. */
115 int isa = 0;
116
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;
122 };
123
124 /* This structure holds the results of a prologue analysis. */
125
126 struct msp430_prologue
127 {
128 /* The offset from the frame base to the stack pointer --- always
129 zero or negative.
130
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;
138
139 /* Non-zero if this function has initialized the frame pointer from
140 the stack pointer, zero otherwise. */
141 int has_frame_ptr;
142
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;
147
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;
152
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];
157 };
158
159 /* Implement the "register_type" gdbarch method. */
160
161 static struct type *
162 msp430_register_type (struct gdbarch *gdbarch, int reg_nr)
163 {
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;
170 }
171
172 /* Implement another version of the "register_type" gdbarch method
173 for msp430x. */
174
175 static struct type *
176 msp430x_register_type (struct gdbarch *gdbarch, int reg_nr)
177 {
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;
184 }
185
186 /* Implement the "register_name" gdbarch method. */
187
188 static const char *
189 msp430_register_name (struct gdbarch *gdbarch, int regnr)
190 {
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"
198 };
199
200 gdb_static_assert (ARRAY_SIZE (reg_names) == (MSP430_NUM_REGS
201 + MSP430_NUM_PSEUDO_REGS));
202 return reg_names[regnr];
203 }
204
205 /* Implement the "register_reggroup_p" gdbarch method. */
206
207 static int
208 msp430_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
209 const struct reggroup *group)
210 {
211 if (group == all_reggroup)
212 return 1;
213
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);
217
218 return group == general_reggroup;
219 }
220
221 /* Implement the "pseudo_register_read" gdbarch method. */
222
223 static enum register_status
224 msp430_pseudo_register_read (struct gdbarch *gdbarch,
225 readable_regcache *regcache,
226 int regnum, gdb_byte *buffer)
227 {
228 if (MSP430_NUM_REGS <= regnum && regnum < MSP430_NUM_TOTAL_REGS)
229 {
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;
235
236 status = regcache->raw_read (raw_regnum, &val);
237 if (status == REG_VALID)
238 store_unsigned_integer (buffer, regsize, byte_order, val);
239
240 return status;
241 }
242 else
243 gdb_assert_not_reached ("invalid pseudo register number");
244 }
245
246 /* Implement the "pseudo_register_write" gdbarch method. */
247
248 static void
249 msp430_pseudo_register_write (struct gdbarch *gdbarch,
250 struct regcache *regcache,
251 int regnum, const gdb_byte *buffer)
252 {
253 if (MSP430_NUM_REGS <= regnum && regnum < MSP430_NUM_TOTAL_REGS)
254
255 {
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;
260
261 val = extract_unsigned_integer (buffer, regsize, byte_order);
262 regcache_raw_write_unsigned (regcache, raw_regnum, val);
263
264 }
265 else
266 gdb_assert_not_reached ("invalid pseudo register number");
267 }
268
269 /* Implement the `register_sim_regno' gdbarch method. */
270
271 static int
272 msp430_register_sim_regno (struct gdbarch *gdbarch, int regnum)
273 {
274 gdb_assert (regnum < MSP430_NUM_REGS);
275
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;
280 }
281
282 constexpr gdb_byte msp430_break_insn[] = { 0x43, 0x43 };
283
284 typedef BP_MANIPULATION (msp430_break_insn) msp430_breakpoint;
285
286 /* Define a "handle" struct for fetching the next opcode. */
287
288 struct msp430_get_opcode_byte_handle
289 {
290 CORE_ADDR pc;
291 };
292
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. */
298
299 static int
300 msp430_get_opcode_byte (void *handle)
301 {
302 struct msp430_get_opcode_byte_handle *opcdata
303 = (struct msp430_get_opcode_byte_handle *) handle;
304 int status;
305 gdb_byte byte;
306
307 status = target_read_memory (opcdata->pc, &byte, 1);
308 if (status == 0)
309 {
310 opcdata->pc += 1;
311 return byte;
312 }
313 else
314 return -1;
315 }
316
317 /* Function for finding saved registers in a 'struct pv_area'; this
318 function is passed to pv_area::scan.
319
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. */
323
324 static void
325 check_for_saved (void *result_untyped, pv_t addr, CORE_ADDR size, pv_t value)
326 {
327 struct msp430_prologue *result = (struct msp430_prologue *) result_untyped;
328
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;
334 }
335
336 /* Analyze a prologue starting at START_PC, going no further than
337 LIMIT_PC. Fill in RESULT as appropriate. */
338
339 static void
340 msp430_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc,
341 CORE_ADDR limit_pc, struct msp430_prologue *result)
342 {
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;
350
351 memset (result, 0, sizeof (*result));
352
353 for (rn = 0; rn < MSP430_NUM_TOTAL_REGS; rn++)
354 {
355 reg[rn] = pv_register (rn, 0);
356 result->reg_offset[rn] = 1;
357 }
358
359 pv_area stack (MSP430_SP_REGNUM, gdbarch_addr_bit (gdbarch));
360
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]);
365
366 pc = start_pc;
367 while (pc < limit_pc)
368 {
369 int bytes_read;
370 struct msp430_get_opcode_byte_handle opcode_handle;
371 MSP430_Opcode_Decoded opc;
372
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;
377
378 if (opc.id == MSO_push && opc.op[0].type == MSP430_Operand_Register)
379 {
380 int rsrc = opc.op[0].reg;
381
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;
385 }
386 else if (opc.id == MSO_push /* PUSHM */
387 && opc.op[0].type == MSP430_Operand_None
388 && opc.op[1].type == MSP430_Operand_Register)
389 {
390 int rsrc = opc.op[1].reg;
391 int count = opc.repeats + 1;
392 int size = opc.size == 16 ? 2 : 4;
393
394 while (count > 0)
395 {
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--;
401 }
402 after_last_frame_setup_insn = next_pc;
403 }
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)
408 {
409 int addend = opc.op[1].addend;
410
411 reg[MSP430_SP_REGNUM] = pv_add_constant (reg[MSP430_SP_REGNUM],
412 -addend);
413 after_last_frame_setup_insn = next_pc;
414 }
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
420 {
421 /* Terminate the prologue scan. */
422 break;
423 }
424
425 pc = next_pc;
426 }
427
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;
431
432 /* Record where all the registers were saved. */
433 stack.scan (check_for_saved, result);
434
435 result->prologue_end = after_last_frame_setup_insn;
436 }
437
438 /* Implement the "skip_prologue" gdbarch method. */
439
440 static CORE_ADDR
441 msp430_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
442 {
443 const char *name;
444 CORE_ADDR func_addr, func_end;
445 struct msp430_prologue p;
446
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;
450
451 msp430_analyze_prologue (gdbarch, pc, func_end, &p);
452 return p.prologue_end;
453 }
454
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. */
459
460 static struct msp430_prologue *
461 msp430_analyze_frame_prologue (frame_info_ptr this_frame,
462 void **this_prologue_cache)
463 {
464 if (!*this_prologue_cache)
465 {
466 CORE_ADDR func_start, stop_addr;
467
468 *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct msp430_prologue);
469
470 func_start = get_frame_func (this_frame);
471 stop_addr = get_frame_pc (this_frame);
472
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;
477
478 msp430_analyze_prologue (get_frame_arch (this_frame), func_start,
479 stop_addr,
480 (struct msp430_prologue *) *this_prologue_cache);
481 }
482
483 return (struct msp430_prologue *) *this_prologue_cache;
484 }
485
486 /* Given a frame and a prologue cache, return this frame's base. */
487
488 static CORE_ADDR
489 msp430_frame_base (frame_info_ptr this_frame, void **this_prologue_cache)
490 {
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);
494
495 return sp - p->frame_size;
496 }
497
498 /* Implement the "frame_this_id" method for unwinding frames. */
499
500 static void
501 msp430_this_id (frame_info_ptr this_frame,
502 void **this_prologue_cache, struct frame_id *this_id)
503 {
504 *this_id = frame_id_build (msp430_frame_base (this_frame,
505 this_prologue_cache),
506 get_frame_func (this_frame));
507 }
508
509 /* Implement the "frame_prev_register" method for unwinding frames. */
510
511 static struct value *
512 msp430_prev_register (frame_info_ptr this_frame,
513 void **this_prologue_cache, int regnum)
514 {
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);
518
519 if (regnum == MSP430_SP_REGNUM)
520 return frame_unwind_got_constant (this_frame, regnum, frame_base);
521
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)
525 {
526 struct value *rv = frame_unwind_got_memory (this_frame, regnum,
527 frame_base +
528 p->reg_offset[regnum]);
529
530 if (regnum == MSP430_PC_REGNUM)
531 {
532 ULONGEST pc = value_as_long (rv);
533
534 return frame_unwind_got_constant (this_frame, regnum, pc);
535 }
536 return rv;
537 }
538
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);
543 }
544
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
553 };
554
555 /* Implement the "dwarf2_reg_to_regnum" gdbarch method. */
556
557 static int
558 msp430_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int reg)
559 {
560 if (reg >= 0 && reg < MSP430_NUM_REGS)
561 return reg + MSP430_NUM_REGS;
562 return -1;
563 }
564
565 /* Implement the "return_value" gdbarch method. */
566
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)
573 {
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;
578
579 if (valtype->length () > 8
580 || valtype->code () == TYPE_CODE_STRUCT
581 || valtype->code () == TYPE_CODE_UNION)
582 return RETURN_VALUE_STRUCT_CONVENTION;
583
584 if (readbuf)
585 {
586 ULONGEST u;
587 int argreg = MSP430_R12_REGNUM;
588 int offset = 0;
589
590 while (valtype_len > 0)
591 {
592 int size = 2;
593
594 if (code_model == MSP_LARGE_CODE_MODEL
595 && valtype->code () == TYPE_CODE_PTR)
596 {
597 size = 4;
598 }
599
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++;
605 }
606 }
607
608 if (writebuf)
609 {
610 ULONGEST u;
611 int argreg = MSP430_R12_REGNUM;
612 int offset = 0;
613
614 while (valtype_len > 0)
615 {
616 int size = 2;
617
618 if (code_model == MSP_LARGE_CODE_MODEL
619 && valtype->code () == TYPE_CODE_PTR)
620 {
621 size = 4;
622 }
623
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++;
629 }
630 }
631
632 return RETURN_VALUE_REGISTER_CONVENTION;
633 }
634
635
636 /* Implement the "frame_align" gdbarch method. */
637
638 static CORE_ADDR
639 msp430_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
640 {
641 return align_down (sp, 2);
642 }
643
644 /* Implement the "push_dummy_call" gdbarch method. */
645
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)
652 {
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;
659
660 struct type *func_type = function->type ();
661
662 /* Dereference function pointer types. */
663 while (func_type->code () == TYPE_CODE_PTR)
664 func_type = func_type->target_type ();
665
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);
669
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++)
673 {
674 int i;
675 int arg_reg = MSP430_R12_REGNUM;
676 int args_on_stack = 0;
677
678 if (write_pass)
679 sp = align_down (sp - sp_off, 4);
680 sp_off = 0;
681
682 if (return_method == return_method_struct)
683 {
684 if (write_pass)
685 regcache_cooked_write_unsigned (regcache, arg_reg, struct_addr);
686 arg_reg++;
687 }
688
689 /* Push the arguments. */
690 for (i = 0; i < nargs; i++)
691 {
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];
699
700 current_arg_on_stack = 0;
701
702 if (arg_type->code () == TYPE_CODE_STRUCT
703 || arg_type->code () == TYPE_CODE_UNION)
704 {
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;
710 }
711 else
712 {
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;
717 }
718
719
720 for (offset = 0; offset < arg_size; offset += 2)
721 {
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))
730 {
731 int size = 2;
732
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))
738 {
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;
747 }
748
749 if (write_pass)
750 regcache_cooked_write_unsigned (regcache, arg_reg,
751 extract_unsigned_integer
752 (arg_bits + offset, size,
753 byte_order));
754
755 arg_reg++;
756 }
757 else
758 {
759 if (write_pass)
760 write_memory (sp + sp_off, arg_bits + offset, 2);
761
762 sp_off += 2;
763 args_on_stack = 1;
764 current_arg_on_stack = 1;
765 }
766 }
767 }
768 }
769
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;
773
774 /* Push the return address. */
775 {
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);
779 }
780
781 /* Update the stack pointer. */
782 regcache_cooked_write_unsigned (regcache, MSP430_SP_REGNUM, sp);
783
784 return cfa;
785 }
786
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. */
793
794 static const char msp430_epilog_name_prefix[] = "__mspabi_func_epilog_";
795
796 /* Implement the "in_return_stub" gdbarch method. */
797
798 static int
799 msp430_in_return_stub (struct gdbarch *gdbarch, CORE_ADDR pc,
800 const char *name)
801 {
802 return (name != NULL
803 && startswith (name, msp430_epilog_name_prefix));
804 }
805
806 /* Implement the "skip_trampoline_code" gdbarch method. */
807 static CORE_ADDR
808 msp430_skip_trampoline_code (frame_info_ptr frame, CORE_ADDR pc)
809 {
810 struct bound_minimal_symbol bms;
811 const char *stub_name;
812 struct gdbarch *gdbarch = get_frame_arch (frame);
813
814 bms = lookup_minimal_symbol_by_pc (pc);
815 if (!bms.minsym)
816 return pc;
817
818 stub_name = bms.minsym->linkage_name ();
819
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))
823 {
824 CORE_ADDR sp = get_frame_register_unsigned (frame, MSP430_SP_REGNUM);
825
826 return read_memory_integer
827 (sp + 2 * (stub_name[strlen (msp430_epilog_name_prefix)] - '0'),
828 2, gdbarch_byte_order (gdbarch));
829 }
830
831 return pc;
832 }
833
834 /* Allocate and initialize a gdbarch object. */
835
836 static struct gdbarch *
837 msp430_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
838 {
839 int elf_flags, isa, code_model;
840
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;
847
848 if (info.abfd != NULL)
849 switch (bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
850 OFBA_MSPABI_Tag_ISA))
851 {
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))
860 {
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;
870 }
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. */
876 {
877 struct gdbarch *ca = get_current_arch ();
878 if (ca && gdbarch_bfd_arch_info (ca)->arch == bfd_arch_msp430)
879 {
880 msp430_gdbarch_tdep *ca_tdep
881 = gdbarch_tdep<msp430_gdbarch_tdep> (ca);
882
883 elf_flags = ca_tdep->elf_flags;
884 isa = ca_tdep->isa;
885 code_model = ca_tdep->code_model;
886 break;
887 }
888 }
889 /* Fall through. */
890 default:
891 error (_("Unknown msp430 isa"));
892 break;
893 }
894 else
895 {
896 isa = MSP_ISA_MSP430;
897 code_model = MSP_SMALL_CODE_MODEL;
898 }
899
900
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))
906 {
907 msp430_gdbarch_tdep *candidate_tdep
908 = gdbarch_tdep<msp430_gdbarch_tdep> (arches->gdbarch);
909
910 if (candidate_tdep->elf_flags != elf_flags
911 || candidate_tdep->isa != isa
912 || candidate_tdep->code_model != code_model)
913 continue;
914
915 return arches->gdbarch;
916 }
917
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);
923
924 tdep->elf_flags = elf_flags;
925 tdep->isa = isa;
926 tdep->code_model = code_model;
927
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_pseudo_register_write (gdbarch, msp430_pseudo_register_write);
941 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, msp430_dwarf2_reg_to_regnum);
942 set_gdbarch_register_sim_regno (gdbarch, msp430_register_sim_regno);
943
944 /* Data types. */
945 set_gdbarch_char_signed (gdbarch, 0);
946 set_gdbarch_short_bit (gdbarch, 16);
947 set_gdbarch_int_bit (gdbarch, 16);
948 set_gdbarch_long_bit (gdbarch, 32);
949 set_gdbarch_long_long_bit (gdbarch, 64);
950 if (code_model == MSP_SMALL_CODE_MODEL)
951 {
952 set_gdbarch_ptr_bit (gdbarch, 16);
953 set_gdbarch_addr_bit (gdbarch, 16);
954 }
955 else /* MSP_LARGE_CODE_MODEL */
956 {
957 set_gdbarch_ptr_bit (gdbarch, 32);
958 set_gdbarch_addr_bit (gdbarch, 32);
959 }
960 set_gdbarch_dwarf2_addr_size (gdbarch, 4);
961 set_gdbarch_float_bit (gdbarch, 32);
962 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
963 set_gdbarch_double_bit (gdbarch, 64);
964 set_gdbarch_long_double_bit (gdbarch, 64);
965 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
966 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
967
968 /* Breakpoints. */
969 set_gdbarch_breakpoint_kind_from_pc (gdbarch,
970 msp430_breakpoint::kind_from_pc);
971 set_gdbarch_sw_breakpoint_from_kind (gdbarch,
972 msp430_breakpoint::bp_from_kind);
973 set_gdbarch_decr_pc_after_break (gdbarch, 1);
974
975 /* Frames, prologues, etc. */
976 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
977 set_gdbarch_skip_prologue (gdbarch, msp430_skip_prologue);
978 set_gdbarch_frame_align (gdbarch, msp430_frame_align);
979 dwarf2_append_unwinders (gdbarch);
980 frame_unwind_append_unwinder (gdbarch, &msp430_unwind);
981
982 /* Dummy frames, return values. */
983 set_gdbarch_push_dummy_call (gdbarch, msp430_push_dummy_call);
984 set_gdbarch_return_value (gdbarch, msp430_return_value);
985
986 /* Trampolines. */
987 set_gdbarch_in_solib_return_trampoline (gdbarch, msp430_in_return_stub);
988 set_gdbarch_skip_trampoline_code (gdbarch, msp430_skip_trampoline_code);
989
990 /* Virtual tables. */
991 set_gdbarch_vbit_in_delta (gdbarch, 0);
992
993 return gdbarch;
994 }
995
996 /* Register the initialization routine. */
997
998 void _initialize_msp430_tdep ();
999 void
1000 _initialize_msp430_tdep ()
1001 {
1002 gdbarch_register (bfd_arch_msp430, msp430_gdbarch_init);
1003 }