1 /* Target-dependent code for the TI TMS320C80 (MVP) for GDB, the GNU debugger.
2 Copyright 1996, Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
27 #include "gdb_string.h"
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 */
37 tic80_frame_find_saved_regs (fi
, regaddr
)
38 struct frame_info
*fi
;
39 struct frame_saved_regs
*regaddr
;
41 memcpy (regaddr
, &fi
->fsr
, sizeof (struct frame_saved_regs
));
44 /* Function: skip_prologue
45 Find end of function prologue. */
48 tic80_skip_prologue (pc
)
51 CORE_ADDR func_addr
, func_end
;
52 struct symtab_and_line sal
;
54 /* See what the symbol table says */
56 if (find_pc_partial_function (pc
, NULL
, &func_addr
, &func_end
))
58 sal
= find_pc_line (func_addr
, 0);
60 if (sal
.line
!= 0 && sal
.end
< func_end
)
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
69 /* We can't find the start of this function, so there's nothing we can do. */
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
79 This information is stored in the "extra" fields of the frame_info. */
82 tic80_scan_prologue (fi
)
83 struct frame_info
*fi
;
85 struct symtab_and_line sal
;
86 CORE_ADDR prologue_start
, prologue_end
, current_pc
;
88 /* Assume there is no frame until proven otherwise. */
89 fi
->framereg
= SP_REGNUM
;
93 /* this code essentially duplicates skip_prologue,
94 but we need the start address below. */
96 if (find_pc_partial_function (fi
->pc
, NULL
, &prologue_start
, &prologue_end
))
98 sal
= find_pc_line (prologue_start
, 0);
100 if (sal
.line
== 0) /* no line info, use current PC */
101 if (prologue_start
!= entry_point_address ())
102 prologue_end
= fi
->pc
;
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) */
110 prologue_end
= prologue_start
+ 40; /* We're in the boondocks: allow for */
111 /* 16 pushes, an add, and "mv fp,sp" */
113 prologue_end
= min (prologue_end
, fi
->pc
);
115 /* Now search the prologue looking for instructions that set up the
116 frame pointer, adjust the stack pointer, and save registers. */
118 for (current_pc
= prologue_start
; current_pc
< prologue_end
; current_pc
+= 4)
124 insn
= read_memory_unsigned_integer (current_pc
, 4);
126 if ((insn
& 0x301000) == 0x301000) /* Long immediate? */
127 /* FIXME - set offset for long immediate instructions */
131 offset
= insn
& 0x7fff; /* extract 15-bit offset */
132 if (offset
& 0x4000) /* if negative, sign-extend */
133 offset
= -(0x8000 - offset
);
136 if ((insn
& 0x7fd0000) == 0x590000) /* st.{w,d} reg, xx(r1) */
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;
143 else if ((insn
& 0xffff8000) == 0x086c8000) /* addu xx, r1, r1 */
144 fi
->framesize
= -offset
;
145 else if ((insn
& 0xffff8000) == 0xf06c8000) /* addu xx, r1, r30 */
147 fi
->framereg
= FP_REGNUM
; /* fp is now valid */
148 fi
->frameoffset
= offset
;
149 break; /* end of stack adjustments */
151 else if (insn
== 0xf03b2001) /* addu r1, r0, r30 */
153 fi
->framereg
= FP_REGNUM
; /* fp is now valid */
155 break; /* end of stack adjustments */
158 /* FIXME - handle long immediate instructions */
159 break; /* anything else isn't prologue */
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. */
171 tic80_init_extra_frame_info (fi
)
172 struct frame_info
*fi
;
177 fi
->pc
= FRAME_SAVED_PC (fi
->next
);
179 memset (fi
->fsr
.regs
, '\000', sizeof fi
->fsr
.regs
);
181 if (PC_IN_CALL_DUMMY (fi
->pc
, fi
->frame
, fi
->frame
))
183 /* We need to setup fi->frame here because run_stack_dummy gets it wrong
184 by assuming it's always FP. */
185 fi
->frame
= generic_read_register_dummy (fi
->pc
, fi
->frame
, SP_REGNUM
);
192 tic80_scan_prologue (fi
);
194 if (!fi
->next
) /* this is the innermost frame? */
195 fi
->frame
= read_register (fi
->framereg
);
196 else /* not the innermost frame */
197 if (fi
->framereg
== FP_REGNUM
) /* we have an FP */
198 if (fi
->next
->fsr
.regs
[FP_REGNUM
] != 0) /* caller saved our FP */
199 fi
->frame
= read_memory_integer (fi
->next
->fsr
.regs
[FP_REGNUM
], 4);
200 for (reg
= 0; reg
< NUM_REGS
; reg
++)
201 if (fi
->fsr
.regs
[reg
] != 0)
202 fi
->fsr
.regs
[reg
] += fi
->frame
- fi
->frameoffset
;
206 /* Function: find_callers_reg
207 Find REGNUM on the stack. Otherwise, it's in an active register. One thing
208 we might want to do here is to check REGNUM against the clobber mask, and
209 somehow flag it as invalid if it isn't saved on the stack somewhere. This
210 would provide a graceful failure mode when trying to get the value of
211 caller-saves registers for an inner frame. */
214 tic80_find_callers_reg (fi
, regnum
)
215 struct frame_info
*fi
;
218 for (; fi
; fi
= fi
->next
)
219 if (PC_IN_CALL_DUMMY (fi
->pc
, fi
->frame
, fi
->frame
))
220 return generic_read_register_dummy (fi
->pc
, fi
->frame
, regnum
);
221 else if (fi
->fsr
.regs
[regnum
] != 0)
222 return read_memory_integer (fi
->fsr
.regs
[regnum
],
223 REGISTER_RAW_SIZE(regnum
));
224 return read_register (regnum
);
227 /* Function: frame_chain
228 Given a GDB frame, determine the address of the calling function's frame.
229 This will be used to create a new GDB frame struct, and then
230 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
231 For c80, we save the frame size when we initialize the frame_info. */
234 tic80_frame_chain (fi
)
235 struct frame_info
*fi
;
237 CORE_ADDR fn_start
, callers_pc
, fp
;
239 /* is this a dummy frame? */
240 if (PC_IN_CALL_DUMMY(fi
->pc
, fi
->frame
, fi
->frame
))
241 return fi
->frame
; /* dummy frame same as caller's frame */
243 /* is caller-of-this a dummy frame? */
244 callers_pc
= FRAME_SAVED_PC(fi
); /* find out who called us: */
245 fp
= tic80_find_callers_reg (fi
, FP_REGNUM
);
246 if (PC_IN_CALL_DUMMY(callers_pc
, fp
, fp
))
247 return fp
; /* dummy frame's frame may bear no relation to ours */
249 if (find_pc_partial_function (fi
->pc
, 0, &fn_start
, 0))
250 if (fn_start
== entry_point_address ())
251 return 0; /* in _start fn, don't chain further */
253 if (fi
->framereg
== FP_REGNUM
)
254 return tic80_find_callers_reg (fi
, FP_REGNUM
);
256 return fi
->frame
+ fi
->framesize
;
259 /* Function: pop_frame
260 Discard from the stack the innermost frame,
261 restoring all saved registers. */
264 tic80_pop_frame (frame
)
265 struct frame_info
*frame
;
269 if (PC_IN_CALL_DUMMY (frame
->pc
, frame
->frame
, frame
->frame
))
270 generic_pop_dummy_frame ();
273 for (regnum
= 0; regnum
< NUM_REGS
; regnum
++)
274 if (frame
->fsr
.regs
[regnum
] != 0)
275 write_register (regnum
,
276 read_memory_integer (frame
->fsr
.regs
[regnum
], 4));
278 write_register (PC_REGNUM
, FRAME_SAVED_PC (frame
));
279 write_register (SP_REGNUM
, read_register (FP_REGNUM
));
281 if (read_register (PSW_REGNUM
) & 0x80)
282 write_register (SPU_REGNUM
, read_register (SP_REGNUM
));
284 write_register (SPI_REGNUM
, read_register (SP_REGNUM
));
287 flush_cached_frames ();
291 /* Function: frame_saved_pc
292 Find the caller of this frame. We do this by seeing if LR_REGNUM is saved
293 in the stack anywhere, otherwise we get it from the registers. */
296 tic80_frame_saved_pc (fi
)
297 struct frame_info
*fi
;
299 if (PC_IN_CALL_DUMMY(fi
->pc
, fi
->frame
, fi
->frame
))
300 return generic_read_register_dummy(fi
->pc
, fi
->frame
, PC_REGNUM
);
302 return tic80_find_callers_reg (fi
, LR_REGNUM
);
305 /* Function: tic80_push_return_address (pc, sp)
306 Set up the return address for the inferior function call.
307 Necessary for targets that don't actually execute a JSR/BSR instruction
308 (ie. when using an empty CALL_DUMMY) */
311 tic80_push_return_address (pc
, sp
)
315 write_register (LR_REGNUM
, CALL_DUMMY_ADDRESS ());
320 /* Function: push_arguments
321 Setup the function arguments for calling a function in the inferior.
323 On the TI C80 architecture, there are six register pairs (R2/R3 to R12/13)
324 which are dedicated for passing function arguments. Up to the first six
325 arguments (depending on size) may go into these registers.
326 The rest go on the stack.
328 Arguments that are smaller than 4 bytes will still take up a whole
329 register or a whole 32-bit word on the stack, and will be
330 right-justified in the register or the stack word. This includes
331 chars, shorts, and small aggregate types.
333 Arguments that are four bytes or less in size are placed in the
334 even-numbered register of a register pair, and the odd-numbered
335 register is not used.
337 Arguments of 8 bytes size (such as floating point doubles) are placed
338 in a register pair. The least significant 32-bit word is placed in
339 the even-numbered register, and the most significant word in the
340 odd-numbered register.
342 Aggregate types with sizes between 4 and 8 bytes are passed
343 entirely on the stack, and are left-justified within the
344 double-word (as opposed to aggregates smaller than 4 bytes
345 which are right-justified).
347 Aggregates of greater than 8 bytes are first copied onto the stack,
348 and then a pointer to the copy is passed in the place of the normal
349 argument (either in a register if available, or on the stack).
351 Functions that must return an aggregate type can return it in the
352 normal return value registers (R2 and R3) if its size is 8 bytes or
353 less. For larger return values, the caller must allocate space for
354 the callee to copy the return value to. A pointer to this space is
355 passed as an implicit first argument, always in R0. */
358 tic80_push_arguments (nargs
, args
, sp
, struct_return
, struct_addr
)
362 unsigned char struct_return
;
363 CORE_ADDR struct_addr
;
365 int stack_offset
, stack_alloc
;
373 int odd_sized_struct
;
375 /* first force sp to a 4-byte alignment */
378 argreg
= ARG0_REGNUM
;
379 /* The "struct return pointer" pseudo-argument goes in R0 */
381 write_register (argreg
++, struct_addr
);
383 /* Now make sure there's space on the stack */
384 for (argnum
= 0, stack_alloc
= 0;
385 argnum
< nargs
; argnum
++)
386 stack_alloc
+= ((TYPE_LENGTH(VALUE_TYPE(args
[argnum
])) + 3) & ~3);
387 sp
-= stack_alloc
; /* make room on stack for args */
390 /* Now load as many as possible of the first arguments into
391 registers, and push the rest onto the stack. There are 16 bytes
392 in four registers available. Loop thru args from first to last. */
394 argreg
= ARG0_REGNUM
;
395 for (argnum
= 0, stack_offset
= 0; argnum
< nargs
; argnum
++)
397 type
= VALUE_TYPE (args
[argnum
]);
398 len
= TYPE_LENGTH (type
);
399 memset(valbuf
, 0, sizeof(valbuf
));
400 val
= (char *) VALUE_CONTENTS (args
[argnum
]);
402 { /* value gets right-justified in the register or stack word */
403 memcpy(valbuf
+ (4 - len
), val
, len
);
407 /* FIXME -- tic80 can take doubleword arguments in register pairs */
408 if (len
> 4 && (len
& 3) != 0)
409 odd_sized_struct
= 1; /* such structs go entirely on stack */
411 odd_sized_struct
= 0;
414 if (argreg
> ARGLAST_REGNUM
|| odd_sized_struct
)
415 { /* must go on the stack */
416 write_memory (sp
+ stack_offset
, val
, 4);
419 /* NOTE WELL!!!!! This is not an "else if" clause!!!
420 That's because some *&^%$ things get passed on the stack
421 AND in the registers! */
422 if (argreg
<= ARGLAST_REGNUM
)
423 { /* there's room in a register */
424 regval
= extract_address (val
, REGISTER_RAW_SIZE(argreg
));
425 write_register (argreg
, regval
);
426 argreg
+= 2; /* FIXME -- what about doubleword args? */
428 /* Store the value 4 bytes at a time. This means that things
429 larger than 4 bytes may go partly in registers and partly
431 len
-= REGISTER_RAW_SIZE(argreg
);
432 val
+= REGISTER_RAW_SIZE(argreg
);
438 /* Function: get_saved_register
439 Just call the generic_get_saved_register function. */
442 get_saved_register (raw_buffer
, optimized
, addrp
, frame
, regnum
, lval
)
446 struct frame_info
*frame
;
448 enum lval_type
*lval
;
450 generic_get_saved_register (raw_buffer
, optimized
, addrp
,
451 frame
, regnum
, lval
);
454 /* Function: tic80_write_sp
455 Because SP is really a read-only register that mirrors either SPU or SPI,
456 we must actually write one of those two as well, depending on PSW. */
463 unsigned long psw
= read_register (PSW_REGNUM
);
465 if (psw
& 0x80) /* stack mode: user or interrupt */
466 write_register (SPU_REGNUM
, val
);
468 write_register (SPI_REGNUM
, val
);
470 write_register (SP_REGNUM
, val
);
474 _initialize_tic80_tdep ()
476 tm_print_insn
= print_insn_tic80
;