]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/msp430-tdep.c
gdb: include gdbarch.h in all files extending gdbarch_tdep
[thirdparty/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-2021 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
39 #include "elf/msp430.h"
40 #include "opcode/msp430-decode.h"
41 #include "elf-bfd.h"
42
43 /* Register Numbers. */
44
45 enum
46 {
47 MSP430_PC_RAW_REGNUM,
48 MSP430_SP_RAW_REGNUM,
49 MSP430_SR_RAW_REGNUM,
50 MSP430_CG_RAW_REGNUM,
51 MSP430_R4_RAW_REGNUM,
52 MSP430_R5_RAW_REGNUM,
53 MSP430_R6_RAW_REGNUM,
54 MSP430_R7_RAW_REGNUM,
55 MSP430_R8_RAW_REGNUM,
56 MSP430_R9_RAW_REGNUM,
57 MSP430_R10_RAW_REGNUM,
58 MSP430_R11_RAW_REGNUM,
59 MSP430_R12_RAW_REGNUM,
60 MSP430_R13_RAW_REGNUM,
61 MSP430_R14_RAW_REGNUM,
62 MSP430_R15_RAW_REGNUM,
63
64 MSP430_NUM_REGS,
65
66 MSP430_PC_REGNUM = MSP430_NUM_REGS,
67 MSP430_SP_REGNUM,
68 MSP430_SR_REGNUM,
69 MSP430_CG_REGNUM,
70 MSP430_R4_REGNUM,
71 MSP430_R5_REGNUM,
72 MSP430_R6_REGNUM,
73 MSP430_R7_REGNUM,
74 MSP430_R8_REGNUM,
75 MSP430_R9_REGNUM,
76 MSP430_R10_REGNUM,
77 MSP430_R11_REGNUM,
78 MSP430_R12_REGNUM,
79 MSP430_R13_REGNUM,
80 MSP430_R14_REGNUM,
81 MSP430_R15_REGNUM,
82
83 MSP430_NUM_TOTAL_REGS,
84 MSP430_NUM_PSEUDO_REGS = MSP430_NUM_TOTAL_REGS - MSP430_NUM_REGS
85 };
86
87 enum
88 {
89 /* TI MSP430 Architecture. */
90 MSP_ISA_MSP430,
91
92 /* TI MSP430X Architecture. */
93 MSP_ISA_MSP430X
94 };
95
96 enum
97 {
98 /* The small code model limits code addresses to 16 bits. */
99 MSP_SMALL_CODE_MODEL,
100
101 /* The large code model uses 20 bit addresses for function
102 pointers. These are stored in memory using four bytes (32 bits). */
103 MSP_LARGE_CODE_MODEL
104 };
105
106 /* Architecture specific data. */
107
108 struct msp430_gdbarch_tdep : gdbarch_tdep
109 {
110 /* The ELF header flags specify the multilib used. */
111 int elf_flags = 0;
112
113 /* One of MSP_ISA_MSP430 or MSP_ISA_MSP430X. */
114 int isa = 0;
115
116 /* One of MSP_SMALL_CODE_MODEL or MSP_LARGE_CODE_MODEL. If, at
117 some point, we support different data models too, we'll probably
118 structure things so that we can combine values using logical
119 "or". */
120 int code_model = 0;
121 };
122
123 /* This structure holds the results of a prologue analysis. */
124
125 struct msp430_prologue
126 {
127 /* The offset from the frame base to the stack pointer --- always
128 zero or negative.
129
130 Calling this a "size" is a bit misleading, but given that the
131 stack grows downwards, using offsets for everything keeps one
132 from going completely sign-crazy: you never change anything's
133 sign for an ADD instruction; always change the second operand's
134 sign for a SUB instruction; and everything takes care of
135 itself. */
136 int frame_size;
137
138 /* Non-zero if this function has initialized the frame pointer from
139 the stack pointer, zero otherwise. */
140 int has_frame_ptr;
141
142 /* If has_frame_ptr is non-zero, this is the offset from the frame
143 base to where the frame pointer points. This is always zero or
144 negative. */
145 int frame_ptr_offset;
146
147 /* The address of the first instruction at which the frame has been
148 set up and the arguments are where the debug info says they are
149 --- as best as we can tell. */
150 CORE_ADDR prologue_end;
151
152 /* reg_offset[R] is the offset from the CFA at which register R is
153 saved, or 1 if register R has not been saved. (Real values are
154 always zero or negative.) */
155 int reg_offset[MSP430_NUM_TOTAL_REGS];
156 };
157
158 /* Implement the "register_type" gdbarch method. */
159
160 static struct type *
161 msp430_register_type (struct gdbarch *gdbarch, int reg_nr)
162 {
163 if (reg_nr < MSP430_NUM_REGS)
164 return builtin_type (gdbarch)->builtin_uint32;
165 else if (reg_nr == MSP430_PC_REGNUM)
166 return builtin_type (gdbarch)->builtin_func_ptr;
167 else
168 return builtin_type (gdbarch)->builtin_uint16;
169 }
170
171 /* Implement another version of the "register_type" gdbarch method
172 for msp430x. */
173
174 static struct type *
175 msp430x_register_type (struct gdbarch *gdbarch, int reg_nr)
176 {
177 if (reg_nr < MSP430_NUM_REGS)
178 return builtin_type (gdbarch)->builtin_uint32;
179 else if (reg_nr == MSP430_PC_REGNUM)
180 return builtin_type (gdbarch)->builtin_func_ptr;
181 else
182 return builtin_type (gdbarch)->builtin_uint32;
183 }
184
185 /* Implement the "register_name" gdbarch method. */
186
187 static const char *
188 msp430_register_name (struct gdbarch *gdbarch, int regnr)
189 {
190 static const char *const reg_names[] = {
191 /* Raw registers. */
192 "", "", "", "", "", "", "", "",
193 "", "", "", "", "", "", "", "",
194 /* Pseudo registers. */
195 "pc", "sp", "sr", "cg", "r4", "r5", "r6", "r7",
196 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15"
197 };
198
199 return reg_names[regnr];
200 }
201
202 /* Implement the "register_reggroup_p" gdbarch method. */
203
204 static int
205 msp430_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
206 struct reggroup *group)
207 {
208 if (group == all_reggroup)
209 return 1;
210
211 /* All other registers are saved and restored. */
212 if (group == save_reggroup || group == restore_reggroup)
213 return (MSP430_NUM_REGS <= regnum && regnum < MSP430_NUM_TOTAL_REGS);
214
215 return group == general_reggroup;
216 }
217
218 /* Implement the "pseudo_register_read" gdbarch method. */
219
220 static enum register_status
221 msp430_pseudo_register_read (struct gdbarch *gdbarch,
222 readable_regcache *regcache,
223 int regnum, gdb_byte *buffer)
224 {
225 if (MSP430_NUM_REGS <= regnum && regnum < MSP430_NUM_TOTAL_REGS)
226 {
227 enum register_status status;
228 ULONGEST val;
229 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
230 int regsize = register_size (gdbarch, regnum);
231 int raw_regnum = regnum - MSP430_NUM_REGS;
232
233 status = regcache->raw_read (raw_regnum, &val);
234 if (status == REG_VALID)
235 store_unsigned_integer (buffer, regsize, byte_order, val);
236
237 return status;
238 }
239 else
240 gdb_assert_not_reached ("invalid pseudo register number");
241 }
242
243 /* Implement the "pseudo_register_write" gdbarch method. */
244
245 static void
246 msp430_pseudo_register_write (struct gdbarch *gdbarch,
247 struct regcache *regcache,
248 int regnum, const gdb_byte *buffer)
249 {
250 if (MSP430_NUM_REGS <= regnum && regnum < MSP430_NUM_TOTAL_REGS)
251
252 {
253 ULONGEST val;
254 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
255 int regsize = register_size (gdbarch, regnum);
256 int raw_regnum = regnum - MSP430_NUM_REGS;
257
258 val = extract_unsigned_integer (buffer, regsize, byte_order);
259 regcache_raw_write_unsigned (regcache, raw_regnum, val);
260
261 }
262 else
263 gdb_assert_not_reached ("invalid pseudo register number");
264 }
265
266 /* Implement the `register_sim_regno' gdbarch method. */
267
268 static int
269 msp430_register_sim_regno (struct gdbarch *gdbarch, int regnum)
270 {
271 gdb_assert (regnum < MSP430_NUM_REGS);
272
273 /* So long as regnum is in [0, RL78_NUM_REGS), it's valid. We
274 just want to override the default here which disallows register
275 numbers which have no names. */
276 return regnum;
277 }
278
279 constexpr gdb_byte msp430_break_insn[] = { 0x43, 0x43 };
280
281 typedef BP_MANIPULATION (msp430_break_insn) msp430_breakpoint;
282
283 /* Define a "handle" struct for fetching the next opcode. */
284
285 struct msp430_get_opcode_byte_handle
286 {
287 CORE_ADDR pc;
288 };
289
290 /* Fetch a byte on behalf of the opcode decoder. HANDLE contains
291 the memory address of the next byte to fetch. If successful,
292 the address in the handle is updated and the byte fetched is
293 returned as the value of the function. If not successful, -1
294 is returned. */
295
296 static int
297 msp430_get_opcode_byte (void *handle)
298 {
299 struct msp430_get_opcode_byte_handle *opcdata
300 = (struct msp430_get_opcode_byte_handle *) handle;
301 int status;
302 gdb_byte byte;
303
304 status = target_read_memory (opcdata->pc, &byte, 1);
305 if (status == 0)
306 {
307 opcdata->pc += 1;
308 return byte;
309 }
310 else
311 return -1;
312 }
313
314 /* Function for finding saved registers in a 'struct pv_area'; this
315 function is passed to pv_area::scan.
316
317 If VALUE is a saved register, ADDR says it was saved at a constant
318 offset from the frame base, and SIZE indicates that the whole
319 register was saved, record its offset. */
320
321 static void
322 check_for_saved (void *result_untyped, pv_t addr, CORE_ADDR size, pv_t value)
323 {
324 struct msp430_prologue *result = (struct msp430_prologue *) result_untyped;
325
326 if (value.kind == pvk_register
327 && value.k == 0
328 && pv_is_register (addr, MSP430_SP_REGNUM)
329 && size == register_size (target_gdbarch (), value.reg))
330 result->reg_offset[value.reg] = addr.k;
331 }
332
333 /* Analyze a prologue starting at START_PC, going no further than
334 LIMIT_PC. Fill in RESULT as appropriate. */
335
336 static void
337 msp430_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc,
338 CORE_ADDR limit_pc, struct msp430_prologue *result)
339 {
340 CORE_ADDR pc, next_pc;
341 int rn;
342 pv_t reg[MSP430_NUM_TOTAL_REGS];
343 CORE_ADDR after_last_frame_setup_insn = start_pc;
344 msp430_gdbarch_tdep *tdep = (msp430_gdbarch_tdep *) gdbarch_tdep (gdbarch);
345 int code_model = tdep->code_model;
346 int sz;
347
348 memset (result, 0, sizeof (*result));
349
350 for (rn = 0; rn < MSP430_NUM_TOTAL_REGS; rn++)
351 {
352 reg[rn] = pv_register (rn, 0);
353 result->reg_offset[rn] = 1;
354 }
355
356 pv_area stack (MSP430_SP_REGNUM, gdbarch_addr_bit (gdbarch));
357
358 /* The call instruction has saved the return address on the stack. */
359 sz = code_model == MSP_LARGE_CODE_MODEL ? 4 : 2;
360 reg[MSP430_SP_REGNUM] = pv_add_constant (reg[MSP430_SP_REGNUM], -sz);
361 stack.store (reg[MSP430_SP_REGNUM], sz, reg[MSP430_PC_REGNUM]);
362
363 pc = start_pc;
364 while (pc < limit_pc)
365 {
366 int bytes_read;
367 struct msp430_get_opcode_byte_handle opcode_handle;
368 MSP430_Opcode_Decoded opc;
369
370 opcode_handle.pc = pc;
371 bytes_read = msp430_decode_opcode (pc, &opc, msp430_get_opcode_byte,
372 &opcode_handle);
373 next_pc = pc + bytes_read;
374
375 if (opc.id == MSO_push && opc.op[0].type == MSP430_Operand_Register)
376 {
377 int rsrc = opc.op[0].reg;
378
379 reg[MSP430_SP_REGNUM] = pv_add_constant (reg[MSP430_SP_REGNUM], -2);
380 stack.store (reg[MSP430_SP_REGNUM], 2, reg[rsrc]);
381 after_last_frame_setup_insn = next_pc;
382 }
383 else if (opc.id == MSO_push /* PUSHM */
384 && opc.op[0].type == MSP430_Operand_None
385 && opc.op[1].type == MSP430_Operand_Register)
386 {
387 int rsrc = opc.op[1].reg;
388 int count = opc.repeats + 1;
389 int size = opc.size == 16 ? 2 : 4;
390
391 while (count > 0)
392 {
393 reg[MSP430_SP_REGNUM]
394 = pv_add_constant (reg[MSP430_SP_REGNUM], -size);
395 stack.store (reg[MSP430_SP_REGNUM], size, reg[rsrc]);
396 rsrc--;
397 count--;
398 }
399 after_last_frame_setup_insn = next_pc;
400 }
401 else if (opc.id == MSO_sub
402 && opc.op[0].type == MSP430_Operand_Register
403 && opc.op[0].reg == MSR_SP
404 && opc.op[1].type == MSP430_Operand_Immediate)
405 {
406 int addend = opc.op[1].addend;
407
408 reg[MSP430_SP_REGNUM] = pv_add_constant (reg[MSP430_SP_REGNUM],
409 -addend);
410 after_last_frame_setup_insn = next_pc;
411 }
412 else if (opc.id == MSO_mov
413 && opc.op[0].type == MSP430_Operand_Immediate
414 && 12 <= opc.op[0].reg && opc.op[0].reg <= 15)
415 after_last_frame_setup_insn = next_pc;
416 else
417 {
418 /* Terminate the prologue scan. */
419 break;
420 }
421
422 pc = next_pc;
423 }
424
425 /* Is the frame size (offset, really) a known constant? */
426 if (pv_is_register (reg[MSP430_SP_REGNUM], MSP430_SP_REGNUM))
427 result->frame_size = reg[MSP430_SP_REGNUM].k;
428
429 /* Record where all the registers were saved. */
430 stack.scan (check_for_saved, result);
431
432 result->prologue_end = after_last_frame_setup_insn;
433 }
434
435 /* Implement the "skip_prologue" gdbarch method. */
436
437 static CORE_ADDR
438 msp430_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
439 {
440 const char *name;
441 CORE_ADDR func_addr, func_end;
442 struct msp430_prologue p;
443
444 /* Try to find the extent of the function that contains PC. */
445 if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
446 return pc;
447
448 msp430_analyze_prologue (gdbarch, pc, func_end, &p);
449 return p.prologue_end;
450 }
451
452 /* Given a frame described by THIS_FRAME, decode the prologue of its
453 associated function if there is not cache entry as specified by
454 THIS_PROLOGUE_CACHE. Save the decoded prologue in the cache and
455 return that struct as the value of this function. */
456
457 static struct msp430_prologue *
458 msp430_analyze_frame_prologue (struct frame_info *this_frame,
459 void **this_prologue_cache)
460 {
461 if (!*this_prologue_cache)
462 {
463 CORE_ADDR func_start, stop_addr;
464
465 *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct msp430_prologue);
466
467 func_start = get_frame_func (this_frame);
468 stop_addr = get_frame_pc (this_frame);
469
470 /* If we couldn't find any function containing the PC, then
471 just initialize the prologue cache, but don't do anything. */
472 if (!func_start)
473 stop_addr = func_start;
474
475 msp430_analyze_prologue (get_frame_arch (this_frame), func_start,
476 stop_addr,
477 (struct msp430_prologue *) *this_prologue_cache);
478 }
479
480 return (struct msp430_prologue *) *this_prologue_cache;
481 }
482
483 /* Given a frame and a prologue cache, return this frame's base. */
484
485 static CORE_ADDR
486 msp430_frame_base (struct frame_info *this_frame, void **this_prologue_cache)
487 {
488 struct msp430_prologue *p
489 = msp430_analyze_frame_prologue (this_frame, this_prologue_cache);
490 CORE_ADDR sp = get_frame_register_unsigned (this_frame, MSP430_SP_REGNUM);
491
492 return sp - p->frame_size;
493 }
494
495 /* Implement the "frame_this_id" method for unwinding frames. */
496
497 static void
498 msp430_this_id (struct frame_info *this_frame,
499 void **this_prologue_cache, struct frame_id *this_id)
500 {
501 *this_id = frame_id_build (msp430_frame_base (this_frame,
502 this_prologue_cache),
503 get_frame_func (this_frame));
504 }
505
506 /* Implement the "frame_prev_register" method for unwinding frames. */
507
508 static struct value *
509 msp430_prev_register (struct frame_info *this_frame,
510 void **this_prologue_cache, int regnum)
511 {
512 struct msp430_prologue *p
513 = msp430_analyze_frame_prologue (this_frame, this_prologue_cache);
514 CORE_ADDR frame_base = msp430_frame_base (this_frame, this_prologue_cache);
515
516 if (regnum == MSP430_SP_REGNUM)
517 return frame_unwind_got_constant (this_frame, regnum, frame_base);
518
519 /* If prologue analysis says we saved this register somewhere,
520 return a description of the stack slot holding it. */
521 else if (p->reg_offset[regnum] != 1)
522 {
523 struct value *rv = frame_unwind_got_memory (this_frame, regnum,
524 frame_base +
525 p->reg_offset[regnum]);
526
527 if (regnum == MSP430_PC_REGNUM)
528 {
529 ULONGEST pc = value_as_long (rv);
530
531 return frame_unwind_got_constant (this_frame, regnum, pc);
532 }
533 return rv;
534 }
535
536 /* Otherwise, presume we haven't changed the value of this
537 register, and get it from the next frame. */
538 else
539 return frame_unwind_got_register (this_frame, regnum, regnum);
540 }
541
542 static const struct frame_unwind msp430_unwind = {
543 "msp430 prologue",
544 NORMAL_FRAME,
545 default_frame_unwind_stop_reason,
546 msp430_this_id,
547 msp430_prev_register,
548 NULL,
549 default_frame_sniffer
550 };
551
552 /* Implement the "dwarf2_reg_to_regnum" gdbarch method. */
553
554 static int
555 msp430_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int reg)
556 {
557 if (reg >= 0 && reg < MSP430_NUM_REGS)
558 return reg + MSP430_NUM_REGS;
559 return -1;
560 }
561
562 /* Implement the "return_value" gdbarch method. */
563
564 static enum return_value_convention
565 msp430_return_value (struct gdbarch *gdbarch,
566 struct value *function,
567 struct type *valtype,
568 struct regcache *regcache,
569 gdb_byte *readbuf, const gdb_byte *writebuf)
570 {
571 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
572 LONGEST valtype_len = TYPE_LENGTH (valtype);
573 msp430_gdbarch_tdep *tdep = (msp430_gdbarch_tdep *) gdbarch_tdep (gdbarch);
574 int code_model = tdep->code_model;
575
576 if (TYPE_LENGTH (valtype) > 8
577 || valtype->code () == TYPE_CODE_STRUCT
578 || valtype->code () == TYPE_CODE_UNION)
579 return RETURN_VALUE_STRUCT_CONVENTION;
580
581 if (readbuf)
582 {
583 ULONGEST u;
584 int argreg = MSP430_R12_REGNUM;
585 int offset = 0;
586
587 while (valtype_len > 0)
588 {
589 int size = 2;
590
591 if (code_model == MSP_LARGE_CODE_MODEL
592 && valtype->code () == TYPE_CODE_PTR)
593 {
594 size = 4;
595 }
596
597 regcache_cooked_read_unsigned (regcache, argreg, &u);
598 store_unsigned_integer (readbuf + offset, size, byte_order, u);
599 valtype_len -= size;
600 offset += size;
601 argreg++;
602 }
603 }
604
605 if (writebuf)
606 {
607 ULONGEST u;
608 int argreg = MSP430_R12_REGNUM;
609 int offset = 0;
610
611 while (valtype_len > 0)
612 {
613 int size = 2;
614
615 if (code_model == MSP_LARGE_CODE_MODEL
616 && valtype->code () == TYPE_CODE_PTR)
617 {
618 size = 4;
619 }
620
621 u = extract_unsigned_integer (writebuf + offset, size, byte_order);
622 regcache_cooked_write_unsigned (regcache, argreg, u);
623 valtype_len -= size;
624 offset += size;
625 argreg++;
626 }
627 }
628
629 return RETURN_VALUE_REGISTER_CONVENTION;
630 }
631
632
633 /* Implement the "frame_align" gdbarch method. */
634
635 static CORE_ADDR
636 msp430_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
637 {
638 return align_down (sp, 2);
639 }
640
641 /* Implement the "push_dummy_call" gdbarch method. */
642
643 static CORE_ADDR
644 msp430_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
645 struct regcache *regcache, CORE_ADDR bp_addr,
646 int nargs, struct value **args, CORE_ADDR sp,
647 function_call_return_method return_method,
648 CORE_ADDR struct_addr)
649 {
650 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
651 int write_pass;
652 int sp_off = 0;
653 CORE_ADDR cfa;
654 msp430_gdbarch_tdep *tdep = (msp430_gdbarch_tdep *) gdbarch_tdep (gdbarch);
655 int code_model = tdep->code_model;
656
657 struct type *func_type = value_type (function);
658
659 /* Dereference function pointer types. */
660 while (func_type->code () == TYPE_CODE_PTR)
661 func_type = TYPE_TARGET_TYPE (func_type);
662
663 /* The end result had better be a function or a method. */
664 gdb_assert (func_type->code () == TYPE_CODE_FUNC
665 || func_type->code () == TYPE_CODE_METHOD);
666
667 /* We make two passes; the first does the stack allocation,
668 the second actually stores the arguments. */
669 for (write_pass = 0; write_pass <= 1; write_pass++)
670 {
671 int i;
672 int arg_reg = MSP430_R12_REGNUM;
673 int args_on_stack = 0;
674
675 if (write_pass)
676 sp = align_down (sp - sp_off, 4);
677 sp_off = 0;
678
679 if (return_method == return_method_struct)
680 {
681 if (write_pass)
682 regcache_cooked_write_unsigned (regcache, arg_reg, struct_addr);
683 arg_reg++;
684 }
685
686 /* Push the arguments. */
687 for (i = 0; i < nargs; i++)
688 {
689 struct value *arg = args[i];
690 const gdb_byte *arg_bits = value_contents_all (arg).data ();
691 struct type *arg_type = check_typedef (value_type (arg));
692 ULONGEST arg_size = TYPE_LENGTH (arg_type);
693 int offset;
694 int current_arg_on_stack;
695 gdb_byte struct_addr_buf[4];
696
697 current_arg_on_stack = 0;
698
699 if (arg_type->code () == TYPE_CODE_STRUCT
700 || arg_type->code () == TYPE_CODE_UNION)
701 {
702 /* Aggregates of any size are passed by reference. */
703 store_unsigned_integer (struct_addr_buf, 4, byte_order,
704 value_address (arg));
705 arg_bits = struct_addr_buf;
706 arg_size = (code_model == MSP_LARGE_CODE_MODEL) ? 4 : 2;
707 }
708 else
709 {
710 /* Scalars bigger than 8 bytes such as complex doubles are passed
711 on the stack. */
712 if (arg_size > 8)
713 current_arg_on_stack = 1;
714 }
715
716
717 for (offset = 0; offset < arg_size; offset += 2)
718 {
719 /* The condition below prevents 8 byte scalars from being split
720 between registers and memory (stack). It also prevents other
721 splits once the stack has been written to. */
722 if (!current_arg_on_stack
723 && (arg_reg
724 + ((arg_size == 8 || args_on_stack)
725 ? ((arg_size - offset) / 2 - 1)
726 : 0) <= MSP430_R15_REGNUM))
727 {
728 int size = 2;
729
730 if (code_model == MSP_LARGE_CODE_MODEL
731 && (arg_type->code () == TYPE_CODE_PTR
732 || TYPE_IS_REFERENCE (arg_type)
733 || arg_type->code () == TYPE_CODE_STRUCT
734 || arg_type->code () == TYPE_CODE_UNION))
735 {
736 /* When using the large memory model, pointer,
737 reference, struct, and union arguments are
738 passed using the entire register. (As noted
739 earlier, aggregates are always passed by
740 reference.) */
741 if (offset != 0)
742 continue;
743 size = 4;
744 }
745
746 if (write_pass)
747 regcache_cooked_write_unsigned (regcache, arg_reg,
748 extract_unsigned_integer
749 (arg_bits + offset, size,
750 byte_order));
751
752 arg_reg++;
753 }
754 else
755 {
756 if (write_pass)
757 write_memory (sp + sp_off, arg_bits + offset, 2);
758
759 sp_off += 2;
760 args_on_stack = 1;
761 current_arg_on_stack = 1;
762 }
763 }
764 }
765 }
766
767 /* Keep track of the stack address prior to pushing the return address.
768 This is the value that we'll return. */
769 cfa = sp;
770
771 /* Push the return address. */
772 {
773 int sz = tdep->code_model == MSP_SMALL_CODE_MODEL ? 2 : 4;
774 sp = sp - sz;
775 write_memory_unsigned_integer (sp, sz, byte_order, bp_addr);
776 }
777
778 /* Update the stack pointer. */
779 regcache_cooked_write_unsigned (regcache, MSP430_SP_REGNUM, sp);
780
781 return cfa;
782 }
783
784 /* In order to keep code size small, the compiler may create epilogue
785 code through which more than one function epilogue is routed. I.e.
786 the epilogue and return may just be a branch to some common piece of
787 code which is responsible for tearing down the frame and performing
788 the return. These epilog (label) names will have the common prefix
789 defined here. */
790
791 static const char msp430_epilog_name_prefix[] = "__mspabi_func_epilog_";
792
793 /* Implement the "in_return_stub" gdbarch method. */
794
795 static int
796 msp430_in_return_stub (struct gdbarch *gdbarch, CORE_ADDR pc,
797 const char *name)
798 {
799 return (name != NULL
800 && startswith (name, msp430_epilog_name_prefix));
801 }
802
803 /* Implement the "skip_trampoline_code" gdbarch method. */
804 static CORE_ADDR
805 msp430_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
806 {
807 struct bound_minimal_symbol bms;
808 const char *stub_name;
809 struct gdbarch *gdbarch = get_frame_arch (frame);
810
811 bms = lookup_minimal_symbol_by_pc (pc);
812 if (!bms.minsym)
813 return pc;
814
815 stub_name = bms.minsym->linkage_name ();
816
817 msp430_gdbarch_tdep *tdep = (msp430_gdbarch_tdep *) gdbarch_tdep (gdbarch);
818 if (tdep->code_model == MSP_SMALL_CODE_MODEL
819 && msp430_in_return_stub (gdbarch, pc, stub_name))
820 {
821 CORE_ADDR sp = get_frame_register_unsigned (frame, MSP430_SP_REGNUM);
822
823 return read_memory_integer
824 (sp + 2 * (stub_name[strlen (msp430_epilog_name_prefix)] - '0'),
825 2, gdbarch_byte_order (gdbarch));
826 }
827
828 return pc;
829 }
830
831 /* Allocate and initialize a gdbarch object. */
832
833 static struct gdbarch *
834 msp430_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
835 {
836 struct gdbarch *gdbarch;
837 int elf_flags, isa, code_model;
838
839 /* Extract the elf_flags if available. */
840 if (info.abfd != NULL
841 && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
842 elf_flags = elf_elfheader (info.abfd)->e_flags;
843 else
844 elf_flags = 0;
845
846 if (info.abfd != NULL)
847 switch (bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
848 OFBA_MSPABI_Tag_ISA))
849 {
850 case 1:
851 isa = MSP_ISA_MSP430;
852 code_model = MSP_SMALL_CODE_MODEL;
853 break;
854 case 2:
855 isa = MSP_ISA_MSP430X;
856 switch (bfd_elf_get_obj_attr_int (info.abfd, OBJ_ATTR_PROC,
857 OFBA_MSPABI_Tag_Code_Model))
858 {
859 case 1:
860 code_model = MSP_SMALL_CODE_MODEL;
861 break;
862 case 2:
863 code_model = MSP_LARGE_CODE_MODEL;
864 break;
865 default:
866 internal_error (__FILE__, __LINE__,
867 _("Unknown msp430x code memory model"));
868 break;
869 }
870 break;
871 case 0:
872 /* This can happen when loading a previously dumped data structure.
873 Use the ISA and code model from the current architecture, provided
874 it's compatible. */
875 {
876 struct gdbarch *ca = get_current_arch ();
877 if (ca && gdbarch_bfd_arch_info (ca)->arch == bfd_arch_msp430)
878 {
879 msp430_gdbarch_tdep *ca_tdep
880 = (msp430_gdbarch_tdep *) gdbarch_tdep (ca);
881
882 elf_flags = ca_tdep->elf_flags;
883 isa = ca_tdep->isa;
884 code_model = ca_tdep->code_model;
885 break;
886 }
887 }
888 /* Fall through. */
889 default:
890 error (_("Unknown msp430 isa"));
891 break;
892 }
893 else
894 {
895 isa = MSP_ISA_MSP430;
896 code_model = MSP_SMALL_CODE_MODEL;
897 }
898
899
900 /* Try to find the architecture in the list of already defined
901 architectures. */
902 for (arches = gdbarch_list_lookup_by_info (arches, &info);
903 arches != NULL;
904 arches = gdbarch_list_lookup_by_info (arches->next, &info))
905 {
906 msp430_gdbarch_tdep *candidate_tdep
907 = (msp430_gdbarch_tdep *) gdbarch_tdep (arches->gdbarch);
908
909 if (candidate_tdep->elf_flags != elf_flags
910 || candidate_tdep->isa != isa
911 || candidate_tdep->code_model != code_model)
912 continue;
913
914 return arches->gdbarch;
915 }
916
917 /* None found, create a new architecture from the information
918 provided. */
919 msp430_gdbarch_tdep *tdep = new msp430_gdbarch_tdep;
920 gdbarch = gdbarch_alloc (&info, tdep);
921 tdep->elf_flags = elf_flags;
922 tdep->isa = isa;
923 tdep->code_model = code_model;
924
925 /* Registers. */
926 set_gdbarch_num_regs (gdbarch, MSP430_NUM_REGS);
927 set_gdbarch_num_pseudo_regs (gdbarch, MSP430_NUM_PSEUDO_REGS);
928 set_gdbarch_register_name (gdbarch, msp430_register_name);
929 if (isa == MSP_ISA_MSP430)
930 set_gdbarch_register_type (gdbarch, msp430_register_type);
931 else
932 set_gdbarch_register_type (gdbarch, msp430x_register_type);
933 set_gdbarch_pc_regnum (gdbarch, MSP430_PC_REGNUM);
934 set_gdbarch_sp_regnum (gdbarch, MSP430_SP_REGNUM);
935 set_gdbarch_register_reggroup_p (gdbarch, msp430_register_reggroup_p);
936 set_gdbarch_pseudo_register_read (gdbarch, msp430_pseudo_register_read);
937 set_gdbarch_pseudo_register_write (gdbarch, msp430_pseudo_register_write);
938 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, msp430_dwarf2_reg_to_regnum);
939 set_gdbarch_register_sim_regno (gdbarch, msp430_register_sim_regno);
940
941 /* Data types. */
942 set_gdbarch_char_signed (gdbarch, 0);
943 set_gdbarch_short_bit (gdbarch, 16);
944 set_gdbarch_int_bit (gdbarch, 16);
945 set_gdbarch_long_bit (gdbarch, 32);
946 set_gdbarch_long_long_bit (gdbarch, 64);
947 if (code_model == MSP_SMALL_CODE_MODEL)
948 {
949 set_gdbarch_ptr_bit (gdbarch, 16);
950 set_gdbarch_addr_bit (gdbarch, 16);
951 }
952 else /* MSP_LARGE_CODE_MODEL */
953 {
954 set_gdbarch_ptr_bit (gdbarch, 32);
955 set_gdbarch_addr_bit (gdbarch, 32);
956 }
957 set_gdbarch_dwarf2_addr_size (gdbarch, 4);
958 set_gdbarch_float_bit (gdbarch, 32);
959 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
960 set_gdbarch_double_bit (gdbarch, 64);
961 set_gdbarch_long_double_bit (gdbarch, 64);
962 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
963 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
964
965 /* Breakpoints. */
966 set_gdbarch_breakpoint_kind_from_pc (gdbarch,
967 msp430_breakpoint::kind_from_pc);
968 set_gdbarch_sw_breakpoint_from_kind (gdbarch,
969 msp430_breakpoint::bp_from_kind);
970 set_gdbarch_decr_pc_after_break (gdbarch, 1);
971
972 /* Frames, prologues, etc. */
973 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
974 set_gdbarch_skip_prologue (gdbarch, msp430_skip_prologue);
975 set_gdbarch_frame_align (gdbarch, msp430_frame_align);
976 dwarf2_append_unwinders (gdbarch);
977 frame_unwind_append_unwinder (gdbarch, &msp430_unwind);
978
979 /* Dummy frames, return values. */
980 set_gdbarch_push_dummy_call (gdbarch, msp430_push_dummy_call);
981 set_gdbarch_return_value (gdbarch, msp430_return_value);
982
983 /* Trampolines. */
984 set_gdbarch_in_solib_return_trampoline (gdbarch, msp430_in_return_stub);
985 set_gdbarch_skip_trampoline_code (gdbarch, msp430_skip_trampoline_code);
986
987 /* Virtual tables. */
988 set_gdbarch_vbit_in_delta (gdbarch, 0);
989
990 return gdbarch;
991 }
992
993 /* Register the initialization routine. */
994
995 void _initialize_msp430_tdep ();
996 void
997 _initialize_msp430_tdep ()
998 {
999 register_gdbarch_init (bfd_arch_msp430, msp430_gdbarch_init);
1000 }