* mn10300.igen (OP_F0F4): Need to load contents of register AN0
[platform/upstream/binutils.git] / gdb / tic80-tdep.c
1 /* Target-dependent code for the TI TMS320C80 (MVP) for GDB, the GNU debugger.
2    Copyright 1996, 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 "value.h"
22 #include "frame.h"
23 #include "inferior.h"
24 #include "obstack.h"
25 #include "target.h"
26 #include "bfd.h"
27 #include "gdb_string.h"
28 #include "gdbcore.h"
29 #include "symfile.h"
30
31 /* Function: frame_find_saved_regs
32    Return the frame_saved_regs structure for the frame.
33    Doesn't really work for dummy frames, but it does pass back
34    an empty frame_saved_regs, so I guess that's better than total failure */
35
36 void 
37 tic80_frame_find_saved_regs (fi, regaddr)
38      struct frame_info *fi;
39      struct frame_saved_regs *regaddr;
40 {
41   memcpy (regaddr, &fi->fsr, sizeof (struct frame_saved_regs));
42 }
43
44 /* Function: skip_prologue
45    Find end of function prologue.  */
46
47 CORE_ADDR
48 tic80_skip_prologue (pc)
49      CORE_ADDR pc;
50 {
51   CORE_ADDR func_addr, func_end;
52   struct symtab_and_line sal;
53
54   /* See what the symbol table says */
55
56   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
57     {
58       sal = find_pc_line (func_addr, 0);
59
60       if (sal.line != 0 && sal.end < func_end)
61         return sal.end;
62       else
63         /* Either there's no line info, or the line after the prologue is after
64            the end of the function.  In this case, there probably isn't a
65            prologue.  */
66         return pc;
67     }
68
69   /* We can't find the start of this function, so there's nothing we can do. */
70   return pc;
71 }
72
73 /* Function: tic80_scan_prologue
74    This function decodes the target function prologue to determine:
75      1) the size of the stack frame
76      2) which registers are saved on it
77      3) the offsets of saved regs
78      4) the frame size
79    This information is stored in the "extra" fields of the frame_info.  */
80
81 static void
82 tic80_scan_prologue (fi)
83      struct frame_info *fi;
84 {
85   struct symtab_and_line sal;
86   CORE_ADDR prologue_start, prologue_end, current_pc;
87
88   /* Assume there is no frame until proven otherwise.  */
89   fi->framereg = SP_REGNUM;
90   fi->framesize = 0;
91   fi->frameoffset = 0;
92
93   /* this code essentially duplicates skip_prologue, 
94      but we need the start address below.  */
95
96   if (find_pc_partial_function (fi->pc, NULL, &prologue_start, &prologue_end))
97     {
98       sal = find_pc_line (prologue_start, 0);
99
100       if (sal.line == 0)                /* no line info, use current PC */
101         if (prologue_start != entry_point_address ())
102           prologue_end = fi->pc;
103         else
104           return;                       /* _start has no frame or prologue */
105       else if (sal.end < prologue_end)  /* next line begins after fn end */
106         prologue_end = sal.end;         /* (probably means no prologue)  */
107     }
108   else
109 /* FIXME */
110     prologue_end = prologue_start + 40; /* We're in the boondocks: allow for */
111                                         /* 16 pushes, an add, and "mv fp,sp" */
112
113   prologue_end = min (prologue_end, fi->pc);
114
115   /* Now search the prologue looking for instructions that set up the
116      frame pointer, adjust the stack pointer, and save registers.  */
117
118   for (current_pc = prologue_start; current_pc < prologue_end; current_pc += 4)
119     {
120       unsigned int insn;
121       int regno;
122       int offset = 0;
123
124       insn = read_memory_unsigned_integer (current_pc, 4);
125
126       if ((insn & 0x301000) == 0x301000)        /* Long immediate? */
127 /* FIXME - set offset for long immediate instructions */
128         current_pc += 4;
129       else
130         {
131           offset = insn & 0x7fff;               /* extract 15-bit offset */
132           if (offset & 0x4000)                  /* if negative, sign-extend */
133             offset = -(0x8000 - offset);
134         }
135
136       if ((insn & 0x7fd0000) == 0x590000)       /* st.{w,d} reg, xx(r1) */
137         {
138           regno = ((insn >> 27) & 0x1f);
139           fi->fsr.regs[regno] = offset;
140           if (insn & 0x8000)                    /* 64-bit store (st.d)? */
141             fi->fsr.regs[regno+1] = offset+4;
142         }
143       else if ((insn & 0xffff8000) == 0x086c8000)   /* addu xx, r1, r1 */
144         fi->framesize = -offset;
145       else if ((insn & 0xffff8000) == 0xf06c8000)  /* addu xx, r1, r30 */
146         {
147           fi->framereg = FP_REGNUM;             /* fp is now valid */
148           fi->frameoffset = offset;
149           break;                                /* end of stack adjustments */
150         }
151       else if (insn == 0xf03b2001)              /* addu r1, r0, r30 */
152         {
153           fi->framereg = FP_REGNUM;             /* fp is now valid */
154           fi->frameoffset = 0;
155           break;                                /* end of stack adjustments */
156         }
157       else
158 /* FIXME - handle long immediate instructions */
159         break;                          /* anything else isn't prologue */
160     }
161 }
162
163 /* Function: init_extra_frame_info
164    This function actually figures out the frame address for a given pc and
165    sp.  This is tricky on the c80 because we sometimes don't use an explicit
166    frame pointer, and the previous stack pointer isn't necessarily recorded
167    on the stack.  The only reliable way to get this info is to
168    examine the prologue.  */
169
170 void
171 tic80_init_extra_frame_info (fi)
172      struct frame_info *fi;
173 {
174   int reg;
175
176   if (fi->next)
177     fi->pc = FRAME_SAVED_PC (fi->next);
178
179   /* Because zero is a valid register offset relative to SP, we initialize
180      the offsets to -1 to indicate unused entries.  */
181   for (reg = 0; reg < NUM_REGS; reg++)
182     fi->fsr.regs[reg] = -1;
183
184   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
185     {
186       /* We need to setup fi->frame here because run_stack_dummy gets it wrong
187          by assuming it's always FP.  */
188       fi->frame = generic_read_register_dummy (fi->pc, fi->frame, SP_REGNUM);
189       fi->framesize = 0;
190       fi->frameoffset = 0;
191       return;
192     }
193   else 
194     {
195       tic80_scan_prologue (fi);
196
197       if (!fi->next)                    /* this is the innermost frame? */
198         fi->frame = read_register (fi->framereg);
199       else                              /* not the innermost frame */
200         /* If this function uses FP as the frame register, and the function
201            it called saved the FP, get the saved FP.  */
202         if (fi->framereg == FP_REGNUM &&
203             fi->next->fsr.regs[FP_REGNUM] != (unsigned) -1)
204           fi->frame = read_memory_integer (fi->next->fsr.regs[FP_REGNUM], 4);
205
206       /* Convert SP-relative offsets of saved registers to real addresses.  */
207       for (reg = 0; reg < NUM_REGS; reg++)
208         if (fi->fsr.regs[reg] == (unsigned) -1)
209             fi->fsr.regs[reg] = 0;      /* unused entry */
210           else
211             fi->fsr.regs[reg] += fi->frame - fi->frameoffset;
212     }
213 }
214
215 /* Function: find_callers_reg
216    Find REGNUM on the stack.  Otherwise, it's in an active register.  One thing
217    we might want to do here is to check REGNUM against the clobber mask, and
218    somehow flag it as invalid if it isn't saved on the stack somewhere.  This
219    would provide a graceful failure mode when trying to get the value of
220    caller-saves registers for an inner frame.  */
221
222 CORE_ADDR
223 tic80_find_callers_reg (fi, regnum)
224      struct frame_info *fi;
225      int regnum;
226 {
227   for (; fi; fi = fi->next)
228     if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
229       return generic_read_register_dummy (fi->pc, fi->frame, regnum);
230     else if (fi->fsr.regs[regnum] != 0)
231       return read_memory_integer (fi->fsr.regs[regnum], 
232                                   REGISTER_RAW_SIZE(regnum));
233   return read_register (regnum);
234 }
235
236 /* Function: frame_chain
237    Given a GDB frame, determine the address of the calling function's frame.
238    This will be used to create a new GDB frame struct, and then
239    INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
240    For c80, we save the frame size when we initialize the frame_info.  */
241
242 CORE_ADDR
243 tic80_frame_chain (fi)
244      struct frame_info *fi;
245 {
246   CORE_ADDR fn_start, callers_pc, fp;
247
248   /* is this a dummy frame? */
249   if (PC_IN_CALL_DUMMY(fi->pc, fi->frame, fi->frame))
250     return fi->frame;   /* dummy frame same as caller's frame */
251
252   /* is caller-of-this a dummy frame? */
253   callers_pc = FRAME_SAVED_PC(fi);  /* find out who called us: */
254   fp = tic80_find_callers_reg (fi, FP_REGNUM);
255   if (PC_IN_CALL_DUMMY(callers_pc, fp, fp))     
256     return fp;          /* dummy frame's frame may bear no relation to ours */
257
258   if (find_pc_partial_function (fi->pc, 0, &fn_start, 0))
259     if (fn_start == entry_point_address ())
260       return 0;         /* in _start fn, don't chain further */
261
262   if (fi->framereg == FP_REGNUM)
263     return tic80_find_callers_reg (fi, FP_REGNUM);
264   else
265     return fi->frame + fi->framesize;
266 }
267
268 /* Function: pop_frame
269    Discard from the stack the innermost frame,
270    restoring all saved registers.  */
271
272 struct frame_info *
273 tic80_pop_frame (frame)
274      struct frame_info *frame;
275 {
276   int regnum;
277
278   if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
279     generic_pop_dummy_frame ();
280   else
281     {
282       for (regnum = 0; regnum < NUM_REGS; regnum++)
283         if (frame->fsr.regs[regnum] != 0)
284           write_register (regnum, 
285                           read_memory_integer (frame->fsr.regs[regnum], 4));
286
287       write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
288       write_register (SP_REGNUM, read_register (FP_REGNUM));
289 #if 0
290       if (read_register (PSW_REGNUM) & 0x80)
291         write_register (SPU_REGNUM, read_register (SP_REGNUM));
292       else
293         write_register (SPI_REGNUM, read_register (SP_REGNUM));
294 #endif
295     }
296   flush_cached_frames ();
297   return NULL;
298 }
299
300 /* Function: frame_saved_pc
301    Find the caller of this frame.  We do this by seeing if LR_REGNUM is saved
302    in the stack anywhere, otherwise we get it from the registers. */
303
304 CORE_ADDR
305 tic80_frame_saved_pc (fi)
306      struct frame_info *fi;
307 {
308   if (PC_IN_CALL_DUMMY(fi->pc, fi->frame, fi->frame))
309     return generic_read_register_dummy (fi->pc, fi->frame, PC_REGNUM);
310   else
311     return tic80_find_callers_reg (fi, LR_REGNUM);
312 }
313
314 /* Function: tic80_push_return_address (pc, sp)
315    Set up the return address for the inferior function call.
316    Necessary for targets that don't actually execute a JSR/BSR instruction 
317    (ie. when using an empty CALL_DUMMY) */
318
319 CORE_ADDR
320 tic80_push_return_address (pc, sp)
321      CORE_ADDR pc;
322      CORE_ADDR sp;
323 {
324   write_register (LR_REGNUM, CALL_DUMMY_ADDRESS ());
325   return sp;
326 }
327
328
329 /* Function: push_arguments
330    Setup the function arguments for calling a function in the inferior.
331
332    On the TI C80 architecture, there are six register pairs (R2/R3 to R12/13)
333    which are dedicated for passing function arguments.  Up to the first six
334    arguments (depending on size) may go into these registers.
335    The rest go on the stack.
336
337    Arguments that are smaller than 4 bytes will still take up a whole
338    register or a whole 32-bit word on the stack, and will be
339    right-justified in the register or the stack word.  This includes
340    chars, shorts, and small aggregate types.
341  
342    Arguments that are four bytes or less in size are placed in the
343    even-numbered register of a register pair, and the odd-numbered
344    register is not used.
345
346    Arguments of 8 bytes size (such as floating point doubles) are placed
347    in a register pair.  The least significant 32-bit word is placed in
348    the even-numbered register, and the most significant word in the
349    odd-numbered register.
350
351    Aggregate types with sizes between 4 and 8 bytes are passed
352    entirely on the stack, and are left-justified within the
353    double-word (as opposed to aggregates smaller than 4 bytes
354    which are right-justified).
355
356    Aggregates of greater than 8 bytes are first copied onto the stack, 
357    and then a pointer to the copy is passed in the place of the normal
358    argument (either in a register if available, or on the stack).
359
360    Functions that must return an aggregate type can return it in the 
361    normal return value registers (R2 and R3) if its size is 8 bytes or
362    less.  For larger return values, the caller must allocate space for 
363    the callee to copy the return value to.  A pointer to this space is
364    passed as an implicit first argument, always in R0. */
365
366 CORE_ADDR
367 tic80_push_arguments (nargs, args, sp, struct_return, struct_addr)
368      int nargs;
369      value_ptr *args;
370      CORE_ADDR sp;
371      unsigned char struct_return;
372      CORE_ADDR struct_addr;
373 {
374   int stack_offset, stack_alloc;
375   int argreg;
376   int argnum;
377   struct type *type;
378   CORE_ADDR regval;
379   char *val;
380   char valbuf[4];
381   int len;
382   int odd_sized_struct;
383   int is_struct;
384
385   /* first force sp to a 4-byte alignment */
386   sp = sp & ~3;
387
388   argreg = ARG0_REGNUM;  
389   /* The "struct return pointer" pseudo-argument goes in R0 */
390   if (struct_return)
391       write_register (argreg++, struct_addr);
392  
393   /* Now make sure there's space on the stack */
394   for (argnum = 0, stack_alloc = 0;
395        argnum < nargs; argnum++)
396     stack_alloc += ((TYPE_LENGTH(VALUE_TYPE(args[argnum])) + 3) & ~3);
397   sp -= stack_alloc;    /* make room on stack for args */
398  
399  
400   /* Now load as many as possible of the first arguments into
401      registers, and push the rest onto the stack.  There are 16 bytes
402      in four registers available.  Loop thru args from first to last.  */
403  
404   argreg = ARG0_REGNUM;
405   for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++)
406     {
407       type = VALUE_TYPE (args[argnum]);
408       len  = TYPE_LENGTH (type);
409       memset (valbuf, 0, sizeof (valbuf));
410       val = (char *) VALUE_CONTENTS (args[argnum]);
411  
412 /* FIXME -- tic80 can take doubleword arguments in register pairs */
413       is_struct = (type->code == TYPE_CODE_STRUCT);
414       odd_sized_struct = 0;
415
416       if (! is_struct)
417         {
418           if (len < 4)
419             { /* value gets right-justified in the register or stack word */
420               memcpy (valbuf + (4 - len), val, len);
421               val = valbuf;
422             }
423           if (len > 4 && (len & 3) != 0)
424             odd_sized_struct = 1;     /* such structs go entirely on stack */
425         }
426       else
427         {
428           /* Structs are always passed by reference. */
429           write_register (argreg, sp + stack_offset);
430           argreg ++;
431         }
432
433       while (len > 0)
434         {
435           if (is_struct || argreg > ARGLAST_REGNUM || odd_sized_struct)
436             {                           /* must go on the stack */
437               write_memory (sp + stack_offset, val, 4);
438               stack_offset += 4;
439             }
440           /* NOTE WELL!!!!!  This is not an "else if" clause!!!
441              That's because some things get passed on the stack
442              AND in the registers!   */
443           if (!is_struct && argreg <= ARGLAST_REGNUM)
444             {                           /* there's room in a register */
445               regval = extract_address (val, REGISTER_RAW_SIZE(argreg));
446               write_register (argreg, regval);
447               argreg += 2;      /* FIXME -- what about doubleword args? */
448             }
449           /* Store the value 4 bytes at a time.  This means that things
450              larger than 4 bytes may go partly in registers and partly
451              on the stack.  */
452           len -= REGISTER_RAW_SIZE(argreg);
453           val += REGISTER_RAW_SIZE(argreg);
454         }
455     }
456   return sp;
457 }
458
459 /* Function: get_saved_register
460    Just call the generic_get_saved_register function.  */
461
462 void
463 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
464      char *raw_buffer;
465      int *optimized;
466      CORE_ADDR *addrp;
467      struct frame_info *frame;
468      int regnum;
469      enum lval_type *lval;
470 {
471   generic_get_saved_register (raw_buffer, optimized, addrp, 
472                               frame, regnum, lval);
473 }
474
475 /* Function: tic80_write_sp
476    Because SP is really a read-only register that mirrors either SPU or SPI,
477    we must actually write one of those two as well, depending on PSW. */
478
479 void
480 tic80_write_sp (val)
481      CORE_ADDR val;
482 {
483 #if 0
484   unsigned long psw = read_register (PSW_REGNUM);
485
486   if (psw & 0x80)       /* stack mode: user or interrupt */
487     write_register (SPU_REGNUM, val);
488   else
489     write_register (SPI_REGNUM, val);
490 #endif
491   write_register (SP_REGNUM, val);
492 }
493
494 void
495 _initialize_tic80_tdep ()
496 {
497   tm_print_insn = print_insn_tic80;
498 }
499