2005-03-04 Michael Snyder <msnyder@redhat.com>
[external/binutils.git] / gdb / mn10300-prologue.c
1 /* Target-dependent code for the Matsushita MN10300 for GDB, the GNU debugger.
2    Prologue analysis module, extracted from mn10300-tdep.c, Oct. 1, 2004.
3
4    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free
5    Software Foundation, Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23
24 #include "defs.h"
25 #include "symtab.h"
26 #include "inferior.h"
27 #include "gdbcore.h"
28 #include "gdb_string.h"
29 #include "trad-frame.h"
30 #include "mn10300-tdep.h"
31
32 enum movm_register_bits {
33   movm_exother_bit = 0x01,
34   movm_exreg1_bit  = 0x02,
35   movm_exreg0_bit  = 0x04,
36   movm_other_bit   = 0x08,
37   movm_a3_bit      = 0x10,
38   movm_a2_bit      = 0x20,
39   movm_d3_bit      = 0x40,
40   movm_d2_bit      = 0x80
41 };
42
43 /* Values for frame_info.status */
44
45 enum frame_kind {
46   MY_FRAME_IN_SP = 0x1,
47   MY_FRAME_IN_FP = 0x2,
48   NO_MORE_FRAMES = 0x4
49 };
50
51 /* 
52  * Frame Extra Info:
53  *
54  *   status -- actually frame type (SP, FP, or last frame)
55  *   stack size -- offset to the next frame
56  * 
57  * The former might ultimately be stored in the frame_base.
58  * Seems like there'd be a way to store the later too.
59  *
60  * Temporarily supply empty stub functions as place holders.
61  */
62
63 static void
64 my_frame_is_in_sp (struct frame_info *fi, void **this_cache)
65 {
66   struct trad_frame_cache *cache = mn10300_frame_unwind_cache (fi, this_cache);
67   trad_frame_set_this_base (cache, 
68                             frame_unwind_register_unsigned (fi, 
69                                                             E_SP_REGNUM));
70 }
71
72 static void
73 my_frame_is_in_fp (struct frame_info *fi, void **this_cache)
74 {
75   struct trad_frame_cache *cache = mn10300_frame_unwind_cache (fi, this_cache);
76   trad_frame_set_this_base (cache, 
77                             frame_unwind_register_unsigned (fi, 
78                                                             E_A3_REGNUM));
79 }
80
81 static void
82 my_frame_is_last (struct frame_info *fi)
83 {
84 }
85
86 static int
87 is_my_frame_in_sp (struct frame_info *fi)
88 {
89   return 0;
90 }
91
92 static int
93 is_my_frame_in_fp (struct frame_info *fi)
94 {
95   return 0;
96 }
97
98 static int
99 is_my_frame_last (struct frame_info *fi)
100 {
101   return 0;
102 }
103
104 static void
105 set_my_stack_size (struct frame_info *fi, CORE_ADDR size)
106 {
107 }
108
109
110 /* Fix fi->frame if it's bogus at this point.  This is a helper
111    function for mn10300_analyze_prologue.
112
113    MVS: This later became frame_base_hack, and probably now
114    could just be trad_frame_set_this_base.
115 */
116
117 static void
118 fix_frame_pointer (struct frame_info *fi, int stack_size)
119 {
120 #if 0
121   if (fi && get_next_frame (fi) == NULL)
122     {
123       if (is_my_frame_in_sp (fi))
124         deprecated_update_frame_base_hack (fi, read_sp () - stack_size);
125       else if (is_my_frame_in_fp (fi))
126         deprecated_update_frame_base_hack (fi, read_register (E_A3_REGNUM));
127     }
128 #endif
129 }
130
131
132 /* Set offsets of registers saved by movm instruction.
133    This is a helper function for mn10300_analyze_prologue.  */
134
135 static void
136 set_movm_offsets (struct frame_info *fi, 
137                   void **this_cache, 
138                   int movm_args)
139 {
140   struct trad_frame_cache *cache;
141   int offset = 0;
142   CORE_ADDR base;
143
144   if (cache == NULL || fi == NULL || movm_args == 0)
145     return;
146
147   cache = mn10300_frame_unwind_cache (fi, this_cache);
148   base = trad_frame_get_this_base (cache);
149   if (movm_args & movm_other_bit)
150     {
151       /* The `other' bit leaves a blank area of four bytes at the
152          beginning of its block of saved registers, making it 32 bytes
153          long in total.  */
154       trad_frame_set_reg_addr (cache, E_LAR_REGNUM,    base + offset + 4);
155       trad_frame_set_reg_addr (cache, E_LIR_REGNUM,    base + offset + 8);
156       trad_frame_set_reg_addr (cache, E_MDR_REGNUM,    base + offset + 12);
157       trad_frame_set_reg_addr (cache, E_A0_REGNUM + 1, base + offset + 16);
158       trad_frame_set_reg_addr (cache, E_A0_REGNUM,     base + offset + 20);
159       trad_frame_set_reg_addr (cache, E_D0_REGNUM + 1, base + offset + 24);
160       trad_frame_set_reg_addr (cache, E_D0_REGNUM,     base + offset + 28);
161       offset += 32;
162     }
163
164   if (movm_args & movm_a3_bit)
165     {
166       trad_frame_set_reg_addr (cache, E_A3_REGNUM, base + offset);
167       offset += 4;
168     }
169   if (movm_args & movm_a2_bit)
170     {
171       trad_frame_set_reg_addr (cache, E_A2_REGNUM, base + offset);
172       offset += 4;
173     }
174   if (movm_args & movm_d3_bit)
175     {
176       trad_frame_set_reg_addr (cache, E_D3_REGNUM, base + offset);
177       offset += 4;
178     }
179   if (movm_args & movm_d2_bit)
180     {
181       trad_frame_set_reg_addr (cache, E_D2_REGNUM, base + offset);
182       offset += 4;
183     }
184   if (AM33_MODE)
185     {
186       if (movm_args & movm_exother_bit)
187         {
188           trad_frame_set_reg_addr (cache, E_MCVF_REGNUM, base + offset);
189           trad_frame_set_reg_addr (cache, E_MCRL_REGNUM, base + offset + 4);
190           trad_frame_set_reg_addr (cache, E_MCRH_REGNUM, base + offset + 8);
191           trad_frame_set_reg_addr (cache, E_MDRQ_REGNUM, base + offset + 12);
192           trad_frame_set_reg_addr (cache, E_E1_REGNUM,   base + offset + 16);
193           trad_frame_set_reg_addr (cache, E_E0_REGNUM,   base + offset + 20);
194           offset += 24;
195         }
196       if (movm_args & movm_exreg1_bit)
197         {
198           trad_frame_set_reg_addr (cache, E_E7_REGNUM, base + offset);
199           trad_frame_set_reg_addr (cache, E_E6_REGNUM, base + offset + 4);
200           trad_frame_set_reg_addr (cache, E_E5_REGNUM, base + offset + 8);
201           trad_frame_set_reg_addr (cache, E_E4_REGNUM, base + offset + 12);
202           offset += 16;
203         }
204       if (movm_args & movm_exreg0_bit)
205         {
206           trad_frame_set_reg_addr (cache, E_E3_REGNUM, base + offset);
207           trad_frame_set_reg_addr (cache, E_E2_REGNUM, base + offset + 4);
208           offset += 8;
209         }
210     }
211   /* The last (or first) thing on the stack will be the PC.  */
212   trad_frame_set_reg_addr (cache, E_PC_REGNUM, base + offset);
213   /* Save the SP in the 'traditional' way.  
214      This will be the same location where the PC is saved.  */
215   trad_frame_set_reg_value (cache, E_SP_REGNUM, base + offset);
216 }
217
218 /* The main purpose of this file is dealing with prologues to extract
219    information about stack frames and saved registers.
220
221    In gcc/config/mn13000/mn10300.c, the expand_prologue prologue
222    function is pretty readable, and has a nice explanation of how the
223    prologue is generated.  The prologues generated by that code will
224    have the following form (NOTE: the current code doesn't handle all
225    this!):
226
227    + If this is an old-style varargs function, then its arguments
228      need to be flushed back to the stack:
229      
230         mov d0,(4,sp)
231         mov d1,(4,sp)
232
233    + If we use any of the callee-saved registers, save them now.
234      
235         movm [some callee-saved registers],(sp)
236
237    + If we have any floating-point registers to save:
238
239      - Decrement the stack pointer to reserve space for the registers.
240        If the function doesn't need a frame pointer, we may combine
241        this with the adjustment that reserves space for the frame.
242
243         add -SIZE, sp
244
245      - Save the floating-point registers.  We have two possible
246        strategies:
247
248        . Save them at fixed offset from the SP:
249
250         fmov fsN,(OFFSETN,sp)
251         fmov fsM,(OFFSETM,sp)
252         ...
253
254        Note that, if OFFSETN happens to be zero, you'll get the
255        different opcode: fmov fsN,(sp)
256
257        . Or, set a0 to the start of the save area, and then use
258        post-increment addressing to save the FP registers.
259
260         mov sp, a0
261         add SIZE, a0
262         fmov fsN,(a0+)
263         fmov fsM,(a0+)
264         ...
265
266    + If the function needs a frame pointer, we set it here.
267
268         mov sp, a3
269
270    + Now we reserve space for the stack frame proper.  This could be
271      merged into the `add -SIZE, sp' instruction for FP saves up
272      above, unless we needed to set the frame pointer in the previous
273      step, or the frame is so large that allocating the whole thing at
274      once would put the FP register save slots out of reach of the
275      addressing mode (128 bytes).
276       
277         add -SIZE, sp        
278
279    One day we might keep the stack pointer constant, that won't
280    change the code for prologues, but it will make the frame
281    pointerless case much more common.  */
282
283 /* Analyze the prologue to determine where registers are saved,
284    the end of the prologue, etc etc.  Return the end of the prologue
285    scanned.
286
287    We store into FI (if non-null) several tidbits of information:
288
289    * stack_size -- size of this stack frame.  Note that if we stop in
290    certain parts of the prologue/epilogue we may claim the size of the
291    current frame is zero.  This happens when the current frame has
292    not been allocated yet or has already been deallocated.
293
294    * fsr -- Addresses of registers saved in the stack by this frame.
295
296    * status -- A (relatively) generic status indicator.  It's a bitmask
297    with the following bits: 
298
299    MY_FRAME_IN_SP: The base of the current frame is actually in
300    the stack pointer.  This can happen for frame pointerless
301    functions, or cases where we're stopped in the prologue/epilogue
302    itself.  For these cases mn10300_analyze_prologue will need up
303    update fi->frame before returning or analyzing the register
304    save instructions.
305
306    MY_FRAME_IN_FP: The base of the current frame is in the
307    frame pointer register ($a3).
308
309    NO_MORE_FRAMES: Set this if the current frame is "start" or
310    if the first instruction looks like mov <imm>,sp.  This tells
311    frame chain to not bother trying to unwind past this frame.  */
312
313 CORE_ADDR
314 mn10300_analyze_prologue (struct frame_info *fi, 
315                           void **this_cache, 
316                           CORE_ADDR pc)
317 {
318   CORE_ADDR func_addr, func_end, addr, stop;
319   long      stack_size;
320   int imm_size;
321   unsigned char buf[4];
322   int status, movm_args = 0;
323   char *name;
324
325   /* Use the PC in the frame if it's provided to look up the
326      start of this function.
327
328      Note: kevinb/2003-07-16: We used to do the following here:
329         pc = (fi ? get_frame_pc (fi) : pc);
330      But this is (now) badly broken when called from analyze_dummy_frame().
331   */
332   pc = (pc ? pc : get_frame_pc (fi));
333
334   /* Find the start of this function.  */
335   status = find_pc_partial_function (pc, &name, &func_addr, &func_end);
336
337   /* Do nothing if we couldn't find the start of this function or if we're
338      stopped at the first instruction in the prologue.  */
339   if (status == 0)
340     {
341       return pc;
342     }
343
344   /* If we're in start, then give up.  */
345   if (strcmp (name, "start") == 0)
346     {
347       if (fi != NULL)
348         my_frame_is_last (fi);
349       return pc;
350     }
351
352   /* At the start of a function our frame is in the stack pointer.  */
353   if (fi)
354     my_frame_is_in_sp (fi, this_cache);
355
356 #if 0
357   /* Get the next two bytes into buf, we need two because rets is a two
358      byte insn and the first isn't enough to uniquely identify it.  */
359   status = deprecated_read_memory_nobpt (pc, buf, 2);
360   if (status != 0)
361     return pc;
362
363   /* Note: kevinb/2003-07-16: We shouldn't be making these sorts of
364      changes to the frame in prologue examination code.  */
365   /* If we're physically on an "rets" instruction, then our frame has
366      already been deallocated.  Note this can also be true for retf
367      and ret if they specify a size of zero.
368
369      In this case fi->frame is bogus, we need to fix it.  */
370   if (fi && buf[0] == 0xf0 && buf[1] == 0xfc)
371     {
372       if (get_next_frame (fi) == NULL)
373         deprecated_update_frame_base_hack (fi, read_sp ());
374       return get_frame_pc (fi);
375     }
376
377   /* Similarly if we're stopped on the first insn of a prologue as our
378      frame hasn't been allocated yet.  */
379   if (fi && get_frame_pc (fi) == func_addr)
380     {
381       if (get_next_frame (fi) == NULL)
382         deprecated_update_frame_base_hack (fi, read_sp ());
383       return get_frame_pc (fi);
384     }
385 #endif
386
387   /* Figure out where to stop scanning.  */
388   stop = fi ? pc : func_end;
389
390   /* Don't walk off the end of the function.  */
391   stop = stop > func_end ? func_end : stop;
392
393   /* Start scanning on the first instruction of this function.  */
394   addr = func_addr;
395
396   /* Suck in two bytes.  */
397   if (addr + 2 >= stop
398       || (status = deprecated_read_memory_nobpt (addr, buf, 2)) != 0)
399     {
400       fix_frame_pointer (fi, 0);
401       return addr;
402     }
403
404   /* First see if this insn sets the stack pointer from a register; if
405      so, it's probably the initialization of the stack pointer in _start,
406      so mark this as the bottom-most frame.  */
407   if (buf[0] == 0xf2 && (buf[1] & 0xf3) == 0xf0)
408     {
409       if (fi)
410         my_frame_is_last (fi);
411       return addr;
412     }
413
414   /* Now look for movm [regs],sp, which saves the callee saved registers.
415
416      At this time we don't know if fi->frame is valid, so we only note
417      that we encountered a movm instruction.  Later, we'll set the entries
418      in fsr.regs as needed.  */
419   if (buf[0] == 0xcf)
420     {
421       /* Extract the register list for the movm instruction.  */
422       movm_args = buf[1];
423
424       addr += 2;
425
426       /* Quit now if we're beyond the stop point.  */
427       if (addr >= stop)
428         {
429           /* Fix fi->frame since it's bogus at this point.  */
430           if (fi && get_next_frame (fi) == NULL)
431             deprecated_update_frame_base_hack (fi, read_sp ());
432
433           /* Note if/where callee saved registers were saved.  */
434           set_movm_offsets (fi, this_cache, movm_args);
435           return addr;
436         }
437
438       /* Get the next two bytes so the prologue scan can continue.  */
439       status = deprecated_read_memory_nobpt (addr, buf, 2);
440       if (status != 0)
441         {
442           /* Fix fi->frame since it's bogus at this point.  */
443           if (fi && get_next_frame (fi) == NULL)
444             deprecated_update_frame_base_hack (fi, read_sp ());
445
446           /* Note if/where callee saved registers were saved.  */
447           set_movm_offsets (fi, this_cache, movm_args);
448           return addr;
449         }
450     }
451
452   /* Now see if we set up a frame pointer via "mov sp,a3" */
453   if (buf[0] == 0x3f)
454     {
455       addr += 1;
456
457       /* The frame pointer is now valid.  */
458       if (fi)
459         {
460           my_frame_is_in_fp (fi, this_cache);
461         }
462
463       /* Quit now if we're beyond the stop point.  */
464       if (addr >= stop)
465         {
466           /* Fix fi->frame if it's bogus at this point.  */
467           fix_frame_pointer (fi, 0);
468
469           /* Note if/where callee saved registers were saved.  */
470           set_movm_offsets (fi, this_cache, movm_args);
471           return addr;
472         }
473
474       /* Get two more bytes so scanning can continue.  */
475       status = deprecated_read_memory_nobpt (addr, buf, 2);
476       if (status != 0)
477         {
478           /* Fix fi->frame if it's bogus at this point.  */
479           fix_frame_pointer (fi, 0);
480
481           /* Note if/where callee saved registers were saved.  */
482           set_movm_offsets (fi, this_cache, movm_args);
483           return addr;
484         }
485     }
486
487   /* Next we should allocate the local frame.  No more prologue insns
488      are found after allocating the local frame.
489
490      Search for add imm8,sp (0xf8feXX)
491      or add imm16,sp (0xfafeXXXX)
492      or add imm32,sp (0xfcfeXXXXXXXX).
493
494      If none of the above was found, then this prologue has no 
495      additional stack.  */
496
497   imm_size = 0;
498   if (buf[0] == 0xf8 && buf[1] == 0xfe)
499     imm_size = 1;
500   else if (buf[0] == 0xfa && buf[1] == 0xfe)
501     imm_size = 2;
502   else if (buf[0] == 0xfc && buf[1] == 0xfe)
503     imm_size = 4;
504
505   if (imm_size != 0)
506     {
507       /* Suck in imm_size more bytes, they'll hold the size of the
508          current frame.  */
509       status = deprecated_read_memory_nobpt (addr + 2, buf, imm_size);
510       if (status != 0)
511         {
512           /* Fix fi->frame if it's bogus at this point.  */
513           fix_frame_pointer (fi, 0);
514
515           /* Note if/where callee saved registers were saved.  */
516           set_movm_offsets (fi, this_cache, movm_args);
517           return addr;
518         }
519
520       /* Note the size of the stack in the frame info structure.  */
521       stack_size = extract_signed_integer (buf, imm_size);
522       if (fi)
523         set_my_stack_size (fi, stack_size);
524
525       /* We just consumed 2 + imm_size bytes.  */
526       addr += 2 + imm_size;
527
528       /* No more prologue insns follow, so begin preparation to return.  */
529       /* Fix fi->frame if it's bogus at this point.  */
530       fix_frame_pointer (fi, stack_size);
531
532       /* Note if/where callee saved registers were saved.  */
533       set_movm_offsets (fi, this_cache, movm_args);
534       return addr;
535     }
536
537   /* We never found an insn which allocates local stack space, regardless
538      this is the end of the prologue.  */
539   /* Fix fi->frame if it's bogus at this point.  */
540   fix_frame_pointer (fi, 0);
541
542   /* Note if/where callee saved registers were saved.  */
543   set_movm_offsets (fi, this_cache, movm_args);
544   return addr;
545 }
546
547