Tue Jan 21 17:03:26 1997 Geoffrey Noer <noer@cygnus.com>
[platform/upstream/binutils.git] / gdb / mn10300-tdep.c
1 /* Target-dependent code for the Matsushita MN10300 for GDB, the GNU debugger.
2    Copyright 1996, 1997 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "obstack.h"
24 #include "target.h"
25 #include "value.h"
26 #include "bfd.h"
27 #include "gdb_string.h"
28 #include "gdbcore.h"
29 #include "symfile.h"
30
31 /* Info gleaned from scanning a function's prologue.  */
32
33 struct pifsr                    /* Info about one saved reg */
34 {
35   int framereg;                 /* Frame reg (SP or FP) */
36   int offset;                   /* Offset from framereg */
37   int reg;                      /* Saved register number */
38 };
39
40 struct prologue_info
41 {
42   int framereg;
43   int frameoffset;
44   int start_function;
45   struct pifsr *pifsrs;
46 };
47
48 /* Function: frame_chain
49    Figure out and return the caller's frame pointer given current
50    frame_info struct.
51
52    We start out knowing the current pc, current sp, current fp.
53    We want to determine the caller's fp and caller's pc.  To do this
54    correctly, we have to be able to handle the case where we are in the
55    middle of the prologue which involves scanning the prologue.
56
57    We don't handle dummy frames yet but we would probably just return the
58    stack pointer that was in use at the time the function call was made?
59 */
60
61 CORE_ADDR
62 mn10300_frame_chain (fi)
63      struct frame_info *fi;
64 {
65   struct prologue_info pi;
66   CORE_ADDR callers_pc, callers_fp, curr_sp;
67   CORE_ADDR past_prologue_addr;
68   int past_prologue = 1; /* default to being past prologue */
69   int n_movm_args = 4;
70
71   struct pifsr *pifsr, *pifsr_tmp;
72
73   /* current pc is fi->pc */
74   /* current fp is fi->frame */  
75
76   /* current sp is: */
77   curr_sp = read_register (SP_REGNUM);
78
79 /*
80   printf("curr pc = 0x%x ; curr fp = 0x%x ; curr sp = 0x%x\n",
81          fi->pc, fi->frame, curr_sp);
82 */
83
84   /* first inst after prologue is: */
85   past_prologue_addr = mn10300_skip_prologue (fi->pc);
86
87   /* Are we in the prologue? */
88   /* Yes if mn10300_skip_prologue returns an address after the
89      current pc in which case we have to scan prologue */
90   if (fi->pc < mn10300_skip_prologue (fi->pc))
91       past_prologue = 0;
92
93   /* scan prologue if we're not past it */
94   if (!past_prologue)
95     {
96         /* printf("scanning prologue\n"); */
97         /* FIXME -- fill out this case later */
98         return 0x666; /* bogus value */
99     }
100
101   if (past_prologue) /* if we don't need to scan the prologue */
102     {
103 /*    printf("we're past the prologue\n"); */
104       callers_pc = fi->frame - REGISTER_SIZE;
105       callers_fp = fi->frame - ((n_movm_args + 1) * REGISTER_SIZE);
106 /*
107       printf("callers_pc = 0x%x ; callers_fp = 0x%x\n",
108              callers_pc, callers_fp);
109
110       printf("*callers_pc = 0x%x ; *callers_fp = 0x%x\n",
111              read_memory_integer(callers_pc, REGISTER_SIZE),
112              read_memory_integer(callers_fp, REGISTER_SIZE));
113 */
114       return read_memory_integer(callers_fp, REGISTER_SIZE);
115     }
116
117   /* we don't get here */
118 }
119
120 /* Function: find_callers_reg
121    Find REGNUM on the stack.  Otherwise, it's in an active register.
122    One thing we might want to do here is to check REGNUM against the
123    clobber mask, and somehow flag it as invalid if it isn't saved on
124    the stack somewhere.  This would provide a graceful failure mode
125    when trying to get the value of caller-saves registers for an inner
126    frame.  */
127
128 CORE_ADDR
129 mn10300_find_callers_reg (fi, regnum)
130      struct frame_info *fi;
131      int regnum;
132 {
133 /*  printf("mn10300_find_callers_reg\n"); */
134
135   for (; fi; fi = fi->next)
136     if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
137       return generic_read_register_dummy (fi->pc, fi->frame, regnum);
138     else if (fi->fsr.regs[regnum] != 0)
139       return read_memory_unsigned_integer (fi->fsr.regs[regnum], 
140                                            REGISTER_RAW_SIZE(regnum));
141
142   return read_register (regnum);
143 }
144
145 /* Function: skip_prologue
146    Return the address of the first inst past the prologue of the function.
147 */
148
149 CORE_ADDR
150 mn10300_skip_prologue (pc)
151      CORE_ADDR pc;
152 {
153   CORE_ADDR func_addr, func_end;
154
155 /*  printf("mn10300_skip_prologue\n"); */
156
157   /* See what the symbol table says */
158
159   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
160     {
161       struct symtab_and_line sal;
162
163       sal = find_pc_line (func_addr, 0);
164
165       if (sal.line != 0 && sal.end < func_end)
166         return sal.end;
167       else
168         /* Either there's no line info, or the line after the prologue is after
169            the end of the function.  In this case, there probably isn't a
170            prologue.  */
171         return pc;
172     }
173
174 /* We can't find the start of this function, so there's nothing we can do. */
175   return pc;
176 }
177
178 /* Function: pop_frame
179    This routine gets called when either the user uses the `return'
180    command, or the call dummy breakpoint gets hit.  */
181
182 void
183 mn10300_pop_frame (frame)
184      struct frame_info *frame;
185 {
186   int regnum;
187
188 /*  printf("mn10300_pop_frame start\n"); */
189
190   if (PC_IN_CALL_DUMMY(frame->pc, frame->frame, frame->frame))
191     generic_pop_dummy_frame ();
192   else
193     {
194       write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
195
196       for (regnum = 0; regnum < NUM_REGS; regnum++)
197         if (frame->fsr.regs[regnum] != 0)
198           write_register (regnum,
199                           read_memory_unsigned_integer (frame->fsr.regs[regnum],
200                                                         REGISTER_RAW_SIZE(regnum)));
201
202       write_register (SP_REGNUM, FRAME_FP (frame));
203     }
204
205   flush_cached_frames ();
206
207 /*  printf("mn10300_pop_frame end\n"); */
208 }
209
210 /* Function: push_arguments
211    Setup arguments for a call to the target.  Arguments go in
212    order on the stack.
213 */
214
215 CORE_ADDR
216 mn10300_push_arguments (nargs, args, sp, struct_return, struct_addr)
217      int nargs;
218      value_ptr *args;
219      CORE_ADDR sp;
220      unsigned char struct_return;
221      CORE_ADDR struct_addr;
222 {
223   int argnum = 0;
224   int len = 0;
225   int stack_offset = 0;  /* copy args to this offset onto stack */
226
227 /*  printf("mn10300_push_arguments start\n"); */
228
229   /* First, just for safety, make sure stack is aligned */
230   sp &= ~3;
231
232   /* Now make space on the stack for the args. */
233   for (argnum = 0; argnum < nargs; argnum++)
234     len += ((TYPE_LENGTH(VALUE_TYPE(args[argnum])) + 3) & ~3);
235
236   sp -= len;
237
238   /* Push all arguments onto the stack. */
239   for (argnum = 0; argnum < nargs; argnum++)
240     {
241       int len;
242       char *val;
243
244       if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
245           && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
246         {
247           /* for now, pretend structs aren't special */
248           len = TYPE_LENGTH (VALUE_TYPE (*args));
249           val = (char *)VALUE_CONTENTS (*args);
250         }
251       else
252         {
253           len = TYPE_LENGTH (VALUE_TYPE (*args));
254           val = (char *)VALUE_CONTENTS (*args);
255         }
256
257       while (len > 0)
258         {
259           write_memory (sp + stack_offset, val, 4);
260
261           len -= 4;
262           val += 4;
263           stack_offset += 4;
264         }
265       args++;
266     }
267
268 /*  printf"mn10300_push_arguments end\n"); */
269
270   return sp;
271 }
272
273 /* Function: push_return_address (pc)
274    Set up the return address for the inferior function call.
275    Needed for targets where we don't actually execute a JSR/BSR instruction */
276  
277 CORE_ADDR
278 mn10300_push_return_address (pc, sp)
279      CORE_ADDR pc;
280      CORE_ADDR sp;
281 {
282 /*  printf("mn10300_push_return_address\n"); */
283
284   /* write_register (RP_REGNUM, CALL_DUMMY_ADDRESS ()); */
285   return sp;
286 }
287  
288 /* Function: frame_saved_pc 
289    Find the caller of this frame.  We do this by seeing if RP_REGNUM
290    is saved in the stack anywhere, otherwise we get it from the
291    registers.  If the inner frame is a dummy frame, return its PC
292    instead of RP, because that's where "caller" of the dummy-frame
293    will be found.  */
294
295 CORE_ADDR
296 mn10300_frame_saved_pc (fi)
297      struct frame_info *fi;
298 {
299 /*  printf("mn10300_frame_saved_pc\n"); */
300
301   return (read_memory_integer(fi->frame - REGISTER_SIZE, REGISTER_SIZE));
302 }
303
304 void
305 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
306      char *raw_buffer;
307      int *optimized;
308      CORE_ADDR *addrp;
309      struct frame_info *frame;
310      int regnum;
311      enum lval_type *lval;
312 {
313 /*  printf("get_saved_register\n"); */
314
315   generic_get_saved_register (raw_buffer, optimized, addrp, 
316                               frame, regnum, lval);
317 }
318
319 /* Function: init_extra_frame_info
320    Setup the frame's frame pointer, pc, and frame addresses for saved
321    registers.  Most of the work is done in frame_chain().
322
323    Note that when we are called for the last frame (currently active frame),
324    that fi->pc and fi->frame will already be setup.  However, fi->frame will
325    be valid only if this routine uses FP.  For previous frames, fi-frame will
326    always be correct (since that is derived from v850_frame_chain ()).
327
328    We can be called with the PC in the call dummy under two circumstances.
329    First, during normal backtracing, second, while figuring out the frame
330    pointer just prior to calling the target function (see run_stack_dummy).
331 */
332
333 void
334 mn10300_init_extra_frame_info (fi)
335      struct frame_info *fi;
336 {
337   struct prologue_info pi;
338   struct pifsr pifsrs[NUM_REGS + 1], *pifsr;
339   int reg;
340
341   if (fi->next)
342     fi->pc = FRAME_SAVED_PC (fi->next);
343
344   memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
345
346   /* The call dummy doesn't save any registers on the stack, so we can return
347      now.  */
348 /*
349   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
350       return;
351
352   pi.pifsrs = pifsrs;
353 */
354
355   /* v850_scan_prologue (fi->pc, &pi); */
356 /*
357   if (!fi->next && pi.framereg == SP_REGNUM)
358     fi->frame = read_register (pi.framereg) - pi.frameoffset;
359
360   for (pifsr = pifsrs; pifsr->framereg; pifsr++)
361     {
362       fi->fsr.regs[pifsr->reg] = pifsr->offset + fi->frame;
363
364       if (pifsr->framereg == SP_REGNUM)
365         fi->fsr.regs[pifsr->reg] += pi.frameoffset;
366     }
367 */
368 /*   printf("init_extra_frame_info\n"); */
369 }
370
371 void
372 _initialize_mn10300_tdep ()
373 {
374 /*  printf("_initialize_mn10300_tdep\n"); */
375
376   tm_print_insn = print_insn_mn10300;
377 }
378