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