2003-05-03 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / d10v-tdep.c
CommitLineData
c906108c 1/* Target-dependent code for Mitsubishi D10V, for GDB.
349c5d5f 2
51603483 3 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003 Free Software
349c5d5f 4 Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
c906108c 12
c5aa993b
JM
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
c906108c 17
c5aa993b
JM
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
c906108c
SS
22
23/* Contributed by Martin Hunt, hunt@cygnus.com */
24
25#include "defs.h"
26#include "frame.h"
7f6104a9 27#include "frame-unwind.h"
270cb5d6 28#include "frame-base.h"
c906108c
SS
29#include "symtab.h"
30#include "gdbtypes.h"
31#include "gdbcmd.h"
32#include "gdbcore.h"
33#include "gdb_string.h"
34#include "value.h"
35#include "inferior.h"
c5aa993b 36#include "dis-asm.h"
c906108c
SS
37#include "symfile.h"
38#include "objfiles.h"
104c1213 39#include "language.h"
28d069e6 40#include "arch-utils.h"
4e052eda 41#include "regcache.h"
e8933a55 42#include "remote.h"
f0d4cc9e 43#include "floatformat.h"
b91b96f4 44#include "gdb/sim-d10v.h"
8238d0bf 45#include "sim-regno.h"
92bf2b80 46#include "disasm.h"
4ce44c66 47
fa1fd571
AC
48#include "gdb_assert.h"
49
4ce44c66
JM
50struct gdbarch_tdep
51 {
52 int a0_regnum;
53 int nr_dmap_regs;
54 unsigned long (*dmap_register) (int nr);
55 unsigned long (*imap_register) (int nr);
4ce44c66
JM
56 };
57
58/* These are the addresses the D10V-EVA board maps data and
59 instruction memory to. */
cce74817 60
78eac43e
MS
61enum memspace {
62 DMEM_START = 0x2000000,
63 IMEM_START = 0x1000000,
64 STACK_START = 0x200bffe
65};
cce74817 66
4ce44c66
JM
67/* d10v register names. */
68
69enum
70 {
71 R0_REGNUM = 0,
78eac43e 72 R3_REGNUM = 3,
6c2b5168 73 D10V_FP_REGNUM = 11,
4ce44c66 74 LR_REGNUM = 13,
f75493ed 75 D10V_SP_REGNUM = 15,
4ce44c66 76 PSW_REGNUM = 16,
27d94c49 77 D10V_PC_REGNUM = 18,
4ce44c66 78 NR_IMAP_REGS = 2,
78eac43e
MS
79 NR_A_REGS = 2,
80 TS2_NUM_REGS = 37,
81 TS3_NUM_REGS = 42,
82 /* d10v calling convention. */
83 ARG1_REGNUM = R0_REGNUM,
84 ARGN_REGNUM = R3_REGNUM,
85 RET1_REGNUM = R0_REGNUM,
4ce44c66 86 };
78eac43e 87
f75493ed
AC
88int
89nr_dmap_regs (struct gdbarch *gdbarch)
90{
91 return gdbarch_tdep (gdbarch)->nr_dmap_regs;
92}
93
94int
95a0_regnum (struct gdbarch *gdbarch)
96{
97 return gdbarch_tdep (gdbarch)->a0_regnum;
98}
4ce44c66 99
392a587b
JM
100/* Local functions */
101
a14ed312 102extern void _initialize_d10v_tdep (void);
392a587b 103
095a4c96
EZ
104static CORE_ADDR d10v_read_sp (void);
105
a14ed312 106static void d10v_eva_prepare_to_trace (void);
392a587b 107
a14ed312 108static void d10v_eva_get_trace_data (void);
c906108c 109
23964bcd 110static CORE_ADDR
6ae2f580 111d10v_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
489137c0 112{
6ae2f580
AC
113 /* Align to the size of an instruction (so that they can safely be
114 pushed onto the stack. */
115 return sp & ~3;
489137c0 116}
c906108c
SS
117
118/* Should we use EXTRACT_STRUCT_VALUE_ADDRESS instead of
119 EXTRACT_RETURN_VALUE? GCC_P is true if compiled with gcc
120 and TYPE is the type (which is known to be struct, union or array).
121
122 The d10v returns anything less than 8 bytes in size in
123 registers. */
124
f5e1cf12 125static int
fba45db2 126d10v_use_struct_convention (int gcc_p, struct type *type)
c906108c 127{
02da6206
JSC
128 long alignment;
129 int i;
130 /* The d10v only passes a struct in a register when that structure
131 has an alignment that matches the size of a register. */
132 /* If the structure doesn't fit in 4 registers, put it on the
133 stack. */
134 if (TYPE_LENGTH (type) > 8)
135 return 1;
136 /* If the struct contains only one field, don't put it on the stack
137 - gcc can fit it in one or more registers. */
138 if (TYPE_NFIELDS (type) == 1)
139 return 0;
140 alignment = TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0));
141 for (i = 1; i < TYPE_NFIELDS (type); i++)
142 {
143 /* If the alignment changes, just assume it goes on the
144 stack. */
145 if (TYPE_LENGTH (TYPE_FIELD_TYPE (type, i)) != alignment)
146 return 1;
147 }
148 /* If the alignment is suitable for the d10v's 16 bit registers,
149 don't put it on the stack. */
150 if (alignment == 2 || alignment == 4)
151 return 0;
152 return 1;
c906108c
SS
153}
154
155
f4f9705a 156static const unsigned char *
fba45db2 157d10v_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
392a587b 158{
c5aa993b
JM
159 static unsigned char breakpoint[] =
160 {0x2f, 0x90, 0x5e, 0x00};
392a587b
JM
161 *lenptr = sizeof (breakpoint);
162 return breakpoint;
163}
164
4ce44c66
JM
165/* Map the REG_NR onto an ascii name. Return NULL or an empty string
166 when the reg_nr isn't valid. */
167
168enum ts2_regnums
169 {
170 TS2_IMAP0_REGNUM = 32,
171 TS2_DMAP_REGNUM = 34,
172 TS2_NR_DMAP_REGS = 1,
173 TS2_A0_REGNUM = 35
174 };
175
fa88f677 176static const char *
4ce44c66 177d10v_ts2_register_name (int reg_nr)
392a587b 178{
c5aa993b
JM
179 static char *register_names[] =
180 {
181 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
182 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
183 "psw", "bpsw", "pc", "bpc", "cr4", "cr5", "cr6", "rpt_c",
184 "rpt_s", "rpt_e", "mod_s", "mod_e", "cr12", "cr13", "iba", "cr15",
185 "imap0", "imap1", "dmap", "a0", "a1"
392a587b
JM
186 };
187 if (reg_nr < 0)
188 return NULL;
189 if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
190 return NULL;
c5aa993b 191 return register_names[reg_nr];
392a587b
JM
192}
193
4ce44c66
JM
194enum ts3_regnums
195 {
196 TS3_IMAP0_REGNUM = 36,
197 TS3_DMAP0_REGNUM = 38,
198 TS3_NR_DMAP_REGS = 4,
199 TS3_A0_REGNUM = 32
200 };
201
fa88f677 202static const char *
4ce44c66
JM
203d10v_ts3_register_name (int reg_nr)
204{
205 static char *register_names[] =
206 {
207 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
208 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
209 "psw", "bpsw", "pc", "bpc", "cr4", "cr5", "cr6", "rpt_c",
210 "rpt_s", "rpt_e", "mod_s", "mod_e", "cr12", "cr13", "iba", "cr15",
211 "a0", "a1",
212 "spi", "spu",
213 "imap0", "imap1",
214 "dmap0", "dmap1", "dmap2", "dmap3"
215 };
216 if (reg_nr < 0)
217 return NULL;
218 if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
219 return NULL;
220 return register_names[reg_nr];
221}
222
bf93dfed
JB
223/* Access the DMAP/IMAP registers in a target independent way.
224
225 Divide the D10V's 64k data space into four 16k segments:
226 0x0000 -- 0x3fff, 0x4000 -- 0x7fff, 0x8000 -- 0xbfff, and
227 0xc000 -- 0xffff.
228
229 On the TS2, the first two segments (0x0000 -- 0x3fff, 0x4000 --
230 0x7fff) always map to the on-chip data RAM, and the fourth always
231 maps to I/O space. The third (0x8000 - 0xbfff) can be mapped into
232 unified memory or instruction memory, under the control of the
233 single DMAP register.
234
235 On the TS3, there are four DMAP registers, each of which controls
236 one of the segments. */
4ce44c66
JM
237
238static unsigned long
239d10v_ts2_dmap_register (int reg_nr)
240{
241 switch (reg_nr)
242 {
243 case 0:
244 case 1:
245 return 0x2000;
246 case 2:
247 return read_register (TS2_DMAP_REGNUM);
248 default:
249 return 0;
250 }
251}
252
253static unsigned long
254d10v_ts3_dmap_register (int reg_nr)
255{
256 return read_register (TS3_DMAP0_REGNUM + reg_nr);
257}
258
259static unsigned long
260d10v_dmap_register (int reg_nr)
261{
262 return gdbarch_tdep (current_gdbarch)->dmap_register (reg_nr);
263}
264
265static unsigned long
266d10v_ts2_imap_register (int reg_nr)
267{
268 return read_register (TS2_IMAP0_REGNUM + reg_nr);
269}
270
271static unsigned long
272d10v_ts3_imap_register (int reg_nr)
273{
274 return read_register (TS3_IMAP0_REGNUM + reg_nr);
275}
276
277static unsigned long
278d10v_imap_register (int reg_nr)
279{
280 return gdbarch_tdep (current_gdbarch)->imap_register (reg_nr);
281}
282
283/* MAP GDB's internal register numbering (determined by the layout fo
284 the REGISTER_BYTE array) onto the simulator's register
285 numbering. */
286
287static int
288d10v_ts2_register_sim_regno (int nr)
289{
e8933a55
AC
290 /* Only makes sense to supply raw registers. */
291 gdb_assert (nr >= 0 && nr < NUM_REGS);
4ce44c66
JM
292 if (nr >= TS2_IMAP0_REGNUM
293 && nr < TS2_IMAP0_REGNUM + NR_IMAP_REGS)
294 return nr - TS2_IMAP0_REGNUM + SIM_D10V_IMAP0_REGNUM;
295 if (nr == TS2_DMAP_REGNUM)
296 return nr - TS2_DMAP_REGNUM + SIM_D10V_TS2_DMAP_REGNUM;
297 if (nr >= TS2_A0_REGNUM
298 && nr < TS2_A0_REGNUM + NR_A_REGS)
299 return nr - TS2_A0_REGNUM + SIM_D10V_A0_REGNUM;
300 return nr;
301}
302
303static int
304d10v_ts3_register_sim_regno (int nr)
305{
e8933a55
AC
306 /* Only makes sense to supply raw registers. */
307 gdb_assert (nr >= 0 && nr < NUM_REGS);
4ce44c66
JM
308 if (nr >= TS3_IMAP0_REGNUM
309 && nr < TS3_IMAP0_REGNUM + NR_IMAP_REGS)
310 return nr - TS3_IMAP0_REGNUM + SIM_D10V_IMAP0_REGNUM;
311 if (nr >= TS3_DMAP0_REGNUM
312 && nr < TS3_DMAP0_REGNUM + TS3_NR_DMAP_REGS)
313 return nr - TS3_DMAP0_REGNUM + SIM_D10V_DMAP0_REGNUM;
314 if (nr >= TS3_A0_REGNUM
315 && nr < TS3_A0_REGNUM + NR_A_REGS)
316 return nr - TS3_A0_REGNUM + SIM_D10V_A0_REGNUM;
317 return nr;
318}
319
392a587b
JM
320/* Return the GDB type object for the "standard" data type
321 of data in register N. */
322
f5e1cf12 323static struct type *
35cac7cf 324d10v_register_type (struct gdbarch *gdbarch, int reg_nr)
392a587b 325{
27d94c49 326 if (reg_nr == D10V_PC_REGNUM)
75af7f68 327 return builtin_type_void_func_ptr;
f75493ed 328 if (reg_nr == D10V_SP_REGNUM || reg_nr == D10V_FP_REGNUM)
095a4c96 329 return builtin_type_void_data_ptr;
f75493ed
AC
330 else if (reg_nr >= a0_regnum (gdbarch)
331 && reg_nr < (a0_regnum (gdbarch) + NR_A_REGS))
4ce44c66 332 return builtin_type_int64;
392a587b 333 else
4ce44c66 334 return builtin_type_int16;
392a587b
JM
335}
336
f5e1cf12 337static int
fba45db2 338d10v_daddr_p (CORE_ADDR x)
392a587b
JM
339{
340 return (((x) & 0x3000000) == DMEM_START);
341}
342
f5e1cf12 343static int
fba45db2 344d10v_iaddr_p (CORE_ADDR x)
392a587b
JM
345{
346 return (((x) & 0x3000000) == IMEM_START);
347}
348
169a7369
MS
349static CORE_ADDR
350d10v_make_daddr (CORE_ADDR x)
351{
352 return ((x) | DMEM_START);
353}
354
355static CORE_ADDR
356d10v_make_iaddr (CORE_ADDR x)
357{
358 if (d10v_iaddr_p (x))
359 return x; /* Idempotency -- x is already in the IMEM space. */
360 else
361 return (((x) << 2) | IMEM_START);
362}
392a587b 363
f5e1cf12 364static CORE_ADDR
fba45db2 365d10v_convert_iaddr_to_raw (CORE_ADDR x)
392a587b
JM
366{
367 return (((x) >> 2) & 0xffff);
368}
369
f5e1cf12 370static CORE_ADDR
fba45db2 371d10v_convert_daddr_to_raw (CORE_ADDR x)
392a587b
JM
372{
373 return ((x) & 0xffff);
374}
375
75af7f68
JB
376static void
377d10v_address_to_pointer (struct type *type, void *buf, CORE_ADDR addr)
378{
379 /* Is it a code address? */
380 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC
381 || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD)
382 {
75af7f68
JB
383 store_unsigned_integer (buf, TYPE_LENGTH (type),
384 d10v_convert_iaddr_to_raw (addr));
385 }
386 else
387 {
388 /* Strip off any upper segment bits. */
389 store_unsigned_integer (buf, TYPE_LENGTH (type),
390 d10v_convert_daddr_to_raw (addr));
391 }
392}
393
394static CORE_ADDR
66140c26 395d10v_pointer_to_address (struct type *type, const void *buf)
75af7f68 396{
f75493ed 397 CORE_ADDR addr = extract_unsigned_integer (buf, TYPE_LENGTH (type));
75af7f68
JB
398 /* Is it a code address? */
399 if (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_FUNC
74a9bb82
FF
400 || TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_METHOD
401 || TYPE_CODE_SPACE (TYPE_TARGET_TYPE (type)))
75af7f68
JB
402 return d10v_make_iaddr (addr);
403 else
404 return d10v_make_daddr (addr);
405}
406
095a4c96
EZ
407/* Don't do anything if we have an integer, this way users can type 'x
408 <addr>' w/o having gdb outsmart them. The internal gdb conversions
409 to the correct space are taken care of in the pointer_to_address
410 function. If we don't do this, 'x $fp' wouldn't work. */
fc0c74b1
AC
411static CORE_ADDR
412d10v_integer_to_address (struct type *type, void *buf)
413{
414 LONGEST val;
415 val = unpack_long (type, buf);
095a4c96 416 return val;
fc0c74b1 417}
75af7f68 418
392a587b
JM
419/* Write into appropriate registers a function return value
420 of type TYPE, given in virtual format.
421
422 Things always get returned in RET1_REGNUM, RET2_REGNUM, ... */
423
f5e1cf12 424static void
fa1fd571
AC
425d10v_store_return_value (struct type *type, struct regcache *regcache,
426 const void *valbuf)
392a587b 427{
fa1fd571
AC
428 /* Only char return values need to be shifted right within the first
429 regnum. */
3d79a47c
MS
430 if (TYPE_LENGTH (type) == 1
431 && TYPE_CODE (type) == TYPE_CODE_INT)
432 {
fa1fd571
AC
433 bfd_byte tmp[2];
434 tmp[1] = *(bfd_byte *)valbuf;
435 regcache_cooked_write (regcache, RET1_REGNUM, tmp);
3d79a47c
MS
436 }
437 else
fa1fd571
AC
438 {
439 int reg;
440 /* A structure is never more than 8 bytes long. See
441 use_struct_convention(). */
442 gdb_assert (TYPE_LENGTH (type) <= 8);
443 /* Write out most registers, stop loop before trying to write
444 out any dangling byte at the end of the buffer. */
445 for (reg = 0; (reg * 2) + 1 < TYPE_LENGTH (type); reg++)
446 {
447 regcache_cooked_write (regcache, RET1_REGNUM + reg,
448 (bfd_byte *) valbuf + reg * 2);
449 }
450 /* Write out any dangling byte at the end of the buffer. */
451 if ((reg * 2) + 1 == TYPE_LENGTH (type))
452 regcache_cooked_write_part (regcache, reg, 0, 1,
453 (bfd_byte *) valbuf + reg * 2);
454 }
392a587b
JM
455}
456
457/* Extract from an array REGBUF containing the (raw) register state
458 the address in which a function should return its structure value,
459 as a CORE_ADDR (or an expression that can be used as one). */
460
f5e1cf12 461static CORE_ADDR
fa1fd571 462d10v_extract_struct_value_address (struct regcache *regcache)
392a587b 463{
fa1fd571
AC
464 ULONGEST addr;
465 regcache_cooked_read_unsigned (regcache, ARG1_REGNUM, &addr);
466 return (addr | DMEM_START);
392a587b
JM
467}
468
c5aa993b 469static int
fba45db2 470check_prologue (unsigned short op)
c906108c
SS
471{
472 /* st rn, @-sp */
473 if ((op & 0x7E1F) == 0x6C1F)
474 return 1;
475
476 /* st2w rn, @-sp */
477 if ((op & 0x7E3F) == 0x6E1F)
478 return 1;
479
480 /* subi sp, n */
481 if ((op & 0x7FE1) == 0x01E1)
482 return 1;
483
484 /* mv r11, sp */
485 if (op == 0x417E)
486 return 1;
487
488 /* nop */
489 if (op == 0x5E00)
490 return 1;
491
492 /* st rn, @sp */
493 if ((op & 0x7E1F) == 0x681E)
494 return 1;
495
496 /* st2w rn, @sp */
c5aa993b
JM
497 if ((op & 0x7E3F) == 0x3A1E)
498 return 1;
c906108c
SS
499
500 return 0;
501}
502
f5e1cf12 503static CORE_ADDR
fba45db2 504d10v_skip_prologue (CORE_ADDR pc)
c906108c
SS
505{
506 unsigned long op;
507 unsigned short op1, op2;
508 CORE_ADDR func_addr, func_end;
509 struct symtab_and_line sal;
510
511 /* If we have line debugging information, then the end of the */
512 /* prologue should the first assembly instruction of the first source line */
513 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
514 {
515 sal = find_pc_line (func_addr, 0);
c5aa993b 516 if (sal.end && sal.end < func_end)
c906108c
SS
517 return sal.end;
518 }
c5aa993b
JM
519
520 if (target_read_memory (pc, (char *) &op, 4))
c906108c
SS
521 return pc; /* Can't access it -- assume no prologue. */
522
523 while (1)
524 {
c5aa993b 525 op = (unsigned long) read_memory_integer (pc, 4);
c906108c
SS
526 if ((op & 0xC0000000) == 0xC0000000)
527 {
528 /* long instruction */
c5aa993b
JM
529 if (((op & 0x3FFF0000) != 0x01FF0000) && /* add3 sp,sp,n */
530 ((op & 0x3F0F0000) != 0x340F0000) && /* st rn, @(offset,sp) */
531 ((op & 0x3F1F0000) != 0x350F0000)) /* st2w rn, @(offset,sp) */
c906108c
SS
532 break;
533 }
534 else
535 {
536 /* short instructions */
537 if ((op & 0xC0000000) == 0x80000000)
538 {
539 op2 = (op & 0x3FFF8000) >> 15;
540 op1 = op & 0x7FFF;
c5aa993b
JM
541 }
542 else
c906108c
SS
543 {
544 op1 = (op & 0x3FFF8000) >> 15;
545 op2 = op & 0x7FFF;
546 }
c5aa993b 547 if (check_prologue (op1))
c906108c 548 {
c5aa993b 549 if (!check_prologue (op2))
c906108c
SS
550 {
551 /* if the previous opcode was really part of the prologue */
552 /* and not just a NOP, then we want to break after both instructions */
553 if (op1 != 0x5E00)
554 pc += 4;
555 break;
556 }
557 }
558 else
559 break;
560 }
561 pc += 4;
562 }
563 return pc;
564}
565
7f6104a9 566struct d10v_unwind_cache
c906108c 567{
7f6104a9 568 CORE_ADDR return_pc;
270cb5d6
AC
569 /* The previous frame's inner most stack address. Used as this
570 frame ID's stack_addr. */
571 CORE_ADDR prev_sp;
572 /* The frame's base, optionally used by the high-level debug info. */
ceea5145 573 CORE_ADDR base;
7f6104a9
AC
574 int size;
575 CORE_ADDR *saved_regs;
0d843116
AC
576 /* How far the SP and r11 (FP) have been offset from the start of
577 the stack frame (as defined by the previous frame's stack
578 pointer). */
579 LONGEST sp_offset;
580 LONGEST r11_offset;
7f6104a9
AC
581 int uses_frame;
582 void **regs;
583};
c906108c 584
c5aa993b 585static int
7f6104a9
AC
586prologue_find_regs (struct d10v_unwind_cache *info, unsigned short op,
587 CORE_ADDR addr)
c906108c
SS
588{
589 int n;
590
591 /* st rn, @-sp */
592 if ((op & 0x7E1F) == 0x6C1F)
593 {
594 n = (op & 0x1E0) >> 5;
0d843116
AC
595 info->sp_offset -= 2;
596 info->saved_regs[n] = info->sp_offset;
c906108c
SS
597 return 1;
598 }
599
600 /* st2w rn, @-sp */
601 else if ((op & 0x7E3F) == 0x6E1F)
602 {
603 n = (op & 0x1E0) >> 5;
0d843116
AC
604 info->sp_offset -= 4;
605 info->saved_regs[n] = info->sp_offset;
606 info->saved_regs[n + 1] = info->sp_offset + 2;
c906108c
SS
607 return 1;
608 }
609
610 /* subi sp, n */
611 if ((op & 0x7FE1) == 0x01E1)
612 {
613 n = (op & 0x1E) >> 1;
614 if (n == 0)
615 n = 16;
0d843116 616 info->sp_offset -= n;
c906108c
SS
617 return 1;
618 }
619
620 /* mv r11, sp */
621 if (op == 0x417E)
622 {
7f6104a9 623 info->uses_frame = 1;
0d843116
AC
624 info->r11_offset = info->sp_offset;
625 return 1;
626 }
627
628 /* st rn, @r11 */
629 if ((op & 0x7E1F) == 0x6816)
630 {
631 n = (op & 0x1E0) >> 5;
632 info->saved_regs[n] = info->r11_offset;
c906108c
SS
633 return 1;
634 }
635
636 /* nop */
637 if (op == 0x5E00)
638 return 1;
639
640 /* st rn, @sp */
641 if ((op & 0x7E1F) == 0x681E)
642 {
643 n = (op & 0x1E0) >> 5;
0d843116 644 info->saved_regs[n] = info->sp_offset;
c906108c
SS
645 return 1;
646 }
647
648 /* st2w rn, @sp */
649 if ((op & 0x7E3F) == 0x3A1E)
650 {
651 n = (op & 0x1E0) >> 5;
0d843116
AC
652 info->saved_regs[n] = info->sp_offset;
653 info->saved_regs[n + 1] = info->sp_offset + 2;
c906108c
SS
654 return 1;
655 }
656
657 return 0;
658}
659
cce74817
JM
660/* Put here the code to store, into fi->saved_regs, the addresses of
661 the saved registers of frame described by FRAME_INFO. This
662 includes special registers such as pc and fp saved in special ways
663 in the stack frame. sp is even more special: the address we return
664 for it IS the sp for the next frame. */
665
7f6104a9 666struct d10v_unwind_cache *
6dc42492
AC
667d10v_frame_unwind_cache (struct frame_info *next_frame,
668 void **this_prologue_cache)
c906108c 669{
ceea5145
AC
670 CORE_ADDR pc;
671 ULONGEST prev_sp;
672 ULONGEST this_base;
c906108c
SS
673 unsigned long op;
674 unsigned short op1, op2;
675 int i;
7f6104a9
AC
676 struct d10v_unwind_cache *info;
677
6dc42492
AC
678 if ((*this_prologue_cache))
679 return (*this_prologue_cache);
7f6104a9
AC
680
681 info = FRAME_OBSTACK_ZALLOC (struct d10v_unwind_cache);
6dc42492 682 (*this_prologue_cache) = info;
f75493ed 683 info->saved_regs = FRAME_OBSTACK_CALLOC (NUM_REGS, CORE_ADDR);
7f6104a9 684
7f6104a9
AC
685 info->size = 0;
686 info->return_pc = 0;
0d843116 687 info->sp_offset = 0;
c906108c 688
7f6104a9 689 info->uses_frame = 0;
be41e9f4 690 for (pc = frame_func_unwind (next_frame);
81f8a206 691 pc > 0 && pc < frame_pc_unwind (next_frame);
270cb5d6 692 pc += 4)
c906108c 693 {
c5aa993b 694 op = (unsigned long) read_memory_integer (pc, 4);
c906108c
SS
695 if ((op & 0xC0000000) == 0xC0000000)
696 {
697 /* long instruction */
698 if ((op & 0x3FFF0000) == 0x01FF0000)
699 {
700 /* add3 sp,sp,n */
701 short n = op & 0xFFFF;
0d843116 702 info->sp_offset += n;
c906108c
SS
703 }
704 else if ((op & 0x3F0F0000) == 0x340F0000)
705 {
706 /* st rn, @(offset,sp) */
707 short offset = op & 0xFFFF;
708 short n = (op >> 20) & 0xF;
0d843116 709 info->saved_regs[n] = info->sp_offset + offset;
c906108c
SS
710 }
711 else if ((op & 0x3F1F0000) == 0x350F0000)
712 {
713 /* st2w rn, @(offset,sp) */
714 short offset = op & 0xFFFF;
715 short n = (op >> 20) & 0xF;
0d843116
AC
716 info->saved_regs[n] = info->sp_offset + offset;
717 info->saved_regs[n + 1] = info->sp_offset + offset + 2;
c906108c
SS
718 }
719 else
720 break;
721 }
722 else
723 {
724 /* short instructions */
725 if ((op & 0xC0000000) == 0x80000000)
726 {
727 op2 = (op & 0x3FFF8000) >> 15;
728 op1 = op & 0x7FFF;
c5aa993b
JM
729 }
730 else
c906108c
SS
731 {
732 op1 = (op & 0x3FFF8000) >> 15;
733 op2 = op & 0x7FFF;
734 }
7f6104a9
AC
735 if (!prologue_find_regs (info, op1, pc)
736 || !prologue_find_regs (info, op2, pc))
c906108c
SS
737 break;
738 }
c906108c 739 }
c5aa993b 740
0d843116 741 info->size = -info->sp_offset;
c906108c 742
ceea5145
AC
743 /* Compute the frame's base, and the previous frame's SP. */
744 if (info->uses_frame)
745 {
746 /* The SP was moved to the FP. This indicates that a new frame
747 was created. Get THIS frame's FP value by unwinding it from
748 the next frame. */
6c2b5168 749 frame_unwind_unsigned_register (next_frame, D10V_FP_REGNUM, &this_base);
ceea5145
AC
750 /* The FP points at the last saved register. Adjust the FP back
751 to before the first saved register giving the SP. */
752 prev_sp = this_base + info->size;
753 }
f75493ed 754 else if (info->saved_regs[D10V_SP_REGNUM])
ceea5145
AC
755 {
756 /* The SP was saved (which is very unusual), the frame base is
757 just the PREV's frame's TOP-OF-STACK. */
f75493ed 758 this_base = read_memory_unsigned_integer (info->saved_regs[D10V_SP_REGNUM],
ceea5145 759 register_size (current_gdbarch,
f75493ed 760 D10V_SP_REGNUM));
ceea5145
AC
761 prev_sp = this_base;
762 }
763 else
764 {
765 /* Assume that the FP is this frame's SP but with that pushed
766 stack space added back. */
f75493ed 767 frame_unwind_unsigned_register (next_frame, D10V_SP_REGNUM, &this_base);
ceea5145
AC
768 prev_sp = this_base + info->size;
769 }
770
771 info->base = d10v_make_daddr (this_base);
270cb5d6 772 info->prev_sp = d10v_make_daddr (prev_sp);
c906108c 773
ceea5145
AC
774 /* Adjust all the saved registers so that they contain addresses and
775 not offsets. */
c5aa993b 776 for (i = 0; i < NUM_REGS - 1; i++)
7f6104a9 777 if (info->saved_regs[i])
c906108c 778 {
270cb5d6 779 info->saved_regs[i] = (info->prev_sp + info->saved_regs[i]);
c906108c
SS
780 }
781
7f6104a9 782 if (info->saved_regs[LR_REGNUM])
c906108c 783 {
78eac43e 784 CORE_ADDR return_pc
7f6104a9 785 = read_memory_unsigned_integer (info->saved_regs[LR_REGNUM],
08a617da 786 register_size (current_gdbarch, LR_REGNUM));
7f6104a9 787 info->return_pc = d10v_make_iaddr (return_pc);
c906108c
SS
788 }
789 else
790 {
7f6104a9 791 ULONGEST return_pc;
6dc42492 792 frame_unwind_unsigned_register (next_frame, LR_REGNUM, &return_pc);
7f6104a9 793 info->return_pc = d10v_make_iaddr (return_pc);
c906108c 794 }
c5aa993b 795
f75493ed 796 /* The D10V_SP_REGNUM is special. Instead of the address of the SP, the
ceea5145 797 previous frame's SP value is saved. */
f75493ed 798 info->saved_regs[D10V_SP_REGNUM] = info->prev_sp;
c906108c 799
7f6104a9 800 return info;
c906108c
SS
801}
802
803static void
5f601589
AC
804d10v_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
805 struct frame_info *frame, int regnum, int all)
c906108c 806{
5f601589 807 if (regnum >= 0)
4ce44c66 808 {
5f601589
AC
809 default_print_registers_info (gdbarch, file, frame, regnum, all);
810 return;
4ce44c66 811 }
5f601589
AC
812
813 {
814 ULONGEST pc, psw, rpt_s, rpt_e, rpt_c;
27d94c49 815 frame_read_unsigned_register (frame, D10V_PC_REGNUM, &pc);
5f601589
AC
816 frame_read_unsigned_register (frame, PSW_REGNUM, &psw);
817 frame_read_unsigned_register (frame, frame_map_name_to_regnum ("rpt_s", -1), &rpt_s);
818 frame_read_unsigned_register (frame, frame_map_name_to_regnum ("rpt_e", -1), &rpt_e);
819 frame_read_unsigned_register (frame, frame_map_name_to_regnum ("rpt_c", -1), &rpt_c);
820 fprintf_filtered (file, "PC=%04lx (0x%lx) PSW=%04lx RPT_S=%04lx RPT_E=%04lx RPT_C=%04lx\n",
821 (long) pc, (long) d10v_make_iaddr (pc), (long) psw,
822 (long) rpt_s, (long) rpt_e, (long) rpt_c);
823 }
824
825 {
826 int group;
827 for (group = 0; group < 16; group += 8)
828 {
829 int r;
830 fprintf_filtered (file, "R%d-R%-2d", group, group + 7);
831 for (r = group; r < group + 8; r++)
832 {
833 ULONGEST tmp;
834 frame_read_unsigned_register (frame, r, &tmp);
835 fprintf_filtered (file, " %04lx", (long) tmp);
836 }
837 fprintf_filtered (file, "\n");
838 }
839 }
840
841 /* Note: The IMAP/DMAP registers don't participate in function
842 calls. Don't bother trying to unwind them. */
843
6789195b 844 {
5f601589
AC
845 int a;
846 for (a = 0; a < NR_IMAP_REGS; a++)
847 {
848 if (a > 0)
849 fprintf_filtered (file, " ");
850 fprintf_filtered (file, "IMAP%d %04lx", a, d10v_imap_register (a));
851 }
f75493ed 852 if (nr_dmap_regs (gdbarch) == 1)
5f601589
AC
853 /* Registers DMAP0 and DMAP1 are constant. Just return dmap2. */
854 fprintf_filtered (file, " DMAP %04lx\n", d10v_dmap_register (2));
855 else
856 {
f75493ed 857 for (a = 0; a < nr_dmap_regs (gdbarch); a++)
5f601589
AC
858 {
859 fprintf_filtered (file, " DMAP%d %04lx", a, d10v_dmap_register (a));
860 }
861 fprintf_filtered (file, "\n");
862 }
863 }
864
865 {
866 char *num = alloca (max_register_size (gdbarch));
867 int a;
868 fprintf_filtered (file, "A0-A%d", NR_A_REGS - 1);
f75493ed 869 for (a = a0_regnum (gdbarch); a < a0_regnum (gdbarch) + NR_A_REGS; a++)
6789195b
AC
870 {
871 int i;
5f601589 872 fprintf_filtered (file, " ");
f75493ed
AC
873 frame_read_register (frame, a, num);
874 for (i = 0; i < register_size (current_gdbarch, a); i++)
6789195b 875 {
5f601589 876 fprintf_filtered (file, "%02x", (num[i] & 0xff));
6789195b
AC
877 }
878 }
879 }
5f601589
AC
880 fprintf_filtered (file, "\n");
881}
882
883static void
884show_regs (char *args, int from_tty)
885{
886 d10v_print_registers_info (current_gdbarch, gdb_stdout,
887 get_current_frame (), -1, 1);
c906108c
SS
888}
889
f5e1cf12 890static CORE_ADDR
39f77062 891d10v_read_pc (ptid_t ptid)
c906108c 892{
39f77062 893 ptid_t save_ptid;
c906108c
SS
894 CORE_ADDR pc;
895 CORE_ADDR retval;
896
39f77062
KB
897 save_ptid = inferior_ptid;
898 inferior_ptid = ptid;
27d94c49 899 pc = (int) read_register (D10V_PC_REGNUM);
39f77062 900 inferior_ptid = save_ptid;
7b570125 901 retval = d10v_make_iaddr (pc);
c906108c
SS
902 return retval;
903}
904
f5e1cf12 905static void
39f77062 906d10v_write_pc (CORE_ADDR val, ptid_t ptid)
c906108c 907{
39f77062 908 ptid_t save_ptid;
c906108c 909
39f77062
KB
910 save_ptid = inferior_ptid;
911 inferior_ptid = ptid;
27d94c49 912 write_register (D10V_PC_REGNUM, d10v_convert_iaddr_to_raw (val));
39f77062 913 inferior_ptid = save_ptid;
c906108c
SS
914}
915
f5e1cf12 916static CORE_ADDR
fba45db2 917d10v_read_sp (void)
c906108c 918{
f75493ed 919 return (d10v_make_daddr (read_register (D10V_SP_REGNUM)));
c906108c
SS
920}
921
7a292a7a
SS
922/* When arguments must be pushed onto the stack, they go on in reverse
923 order. The below implements a FILO (stack) to do this. */
924
925struct stack_item
926{
927 int len;
928 struct stack_item *prev;
929 void *data;
930};
931
a14ed312
KB
932static struct stack_item *push_stack_item (struct stack_item *prev,
933 void *contents, int len);
7a292a7a 934static struct stack_item *
fba45db2 935push_stack_item (struct stack_item *prev, void *contents, int len)
7a292a7a
SS
936{
937 struct stack_item *si;
938 si = xmalloc (sizeof (struct stack_item));
939 si->data = xmalloc (len);
940 si->len = len;
941 si->prev = prev;
942 memcpy (si->data, contents, len);
943 return si;
944}
945
a14ed312 946static struct stack_item *pop_stack_item (struct stack_item *si);
7a292a7a 947static struct stack_item *
fba45db2 948pop_stack_item (struct stack_item *si)
7a292a7a
SS
949{
950 struct stack_item *dead = si;
951 si = si->prev;
b8c9b27d
KB
952 xfree (dead->data);
953 xfree (dead);
7a292a7a
SS
954 return si;
955}
956
957
7043d8dc
AC
958static CORE_ADDR
959d10v_push_dummy_code (struct gdbarch *gdbarch,
960 CORE_ADDR sp, CORE_ADDR funaddr, int using_gcc,
961 struct value **args, int nargs,
962 struct type *value_type,
963 CORE_ADDR *real_pc, CORE_ADDR *bp_addr)
964{
965 /* Allocate space sufficient for a breakpoint. */
966 sp = (sp - 4) & ~3;
967 /* Store the address of that breakpoint taking care to first convert
968 it into a code (IADDR) address from a stack (DADDR) address.
969 This of course assumes that the two virtual addresses map onto
970 the same real address. */
971 (*bp_addr) = d10v_make_iaddr (d10v_convert_iaddr_to_raw (sp));
972 /* d10v always starts the call at the callee's entry point. */
973 (*real_pc) = funaddr;
974 return sp;
975}
976
f5e1cf12 977static CORE_ADDR
5873a88d
AC
978d10v_push_dummy_call (struct gdbarch *gdbarch, struct regcache *regcache,
979 CORE_ADDR dummy_addr, int nargs, struct value **args,
980 CORE_ADDR sp, int struct_return, CORE_ADDR struct_addr)
c906108c
SS
981{
982 int i;
983 int regnum = ARG1_REGNUM;
7a292a7a 984 struct stack_item *si = NULL;
7bd91a28
MS
985 long val;
986
5873a88d
AC
987 /* Set the return address. For the d10v, the return breakpoint is
988 always at DUMMY_ADDR. */
989 regcache_cooked_write_unsigned (regcache, LR_REGNUM,
990 d10v_convert_iaddr_to_raw (dummy_addr));
991
4183d812
AC
992 /* If STRUCT_RETURN is true, then the struct return address (in
993 STRUCT_ADDR) will consume the first argument-passing register.
994 Both adjust the register count and store that value. */
7bd91a28 995 if (struct_return)
4183d812 996 {
5873a88d 997 regcache_cooked_write_unsigned (regcache, regnum, struct_addr);
4183d812
AC
998 regnum++;
999 }
c5aa993b 1000
c906108c
SS
1001 /* Fill in registers and arg lists */
1002 for (i = 0; i < nargs; i++)
1003 {
ea7c478f 1004 struct value *arg = args[i];
c906108c
SS
1005 struct type *type = check_typedef (VALUE_TYPE (arg));
1006 char *contents = VALUE_CONTENTS (arg);
1007 int len = TYPE_LENGTH (type);
7bd91a28
MS
1008 int aligned_regnum = (regnum + 1) & ~1;
1009
8b279e7a 1010 /* printf ("push: type=%d len=%d\n", TYPE_CODE (type), len); */
7bd91a28
MS
1011 if (len <= 2 && regnum <= ARGN_REGNUM)
1012 /* fits in a single register, do not align */
1013 {
1014 val = extract_unsigned_integer (contents, len);
5873a88d 1015 regcache_cooked_write_unsigned (regcache, regnum++, val);
7bd91a28
MS
1016 }
1017 else if (len <= (ARGN_REGNUM - aligned_regnum + 1) * 2)
1018 /* value fits in remaining registers, store keeping left
1019 aligned */
c906108c 1020 {
7bd91a28
MS
1021 int b;
1022 regnum = aligned_regnum;
1023 for (b = 0; b < (len & ~1); b += 2)
c906108c 1024 {
7bd91a28 1025 val = extract_unsigned_integer (&contents[b], 2);
5873a88d 1026 regcache_cooked_write_unsigned (regcache, regnum++, val);
c906108c 1027 }
7bd91a28 1028 if (b < len)
c906108c 1029 {
7bd91a28 1030 val = extract_unsigned_integer (&contents[b], 1);
5873a88d 1031 regcache_cooked_write_unsigned (regcache, regnum++, (val << 8));
c906108c
SS
1032 }
1033 }
7bd91a28
MS
1034 else
1035 {
1036 /* arg will go onto stack */
1037 regnum = ARGN_REGNUM + 1;
1038 si = push_stack_item (si, contents, len);
1039 }
c906108c 1040 }
7a292a7a
SS
1041
1042 while (si)
1043 {
1044 sp = (sp - si->len) & ~1;
1045 write_memory (sp, si->data, si->len);
1046 si = pop_stack_item (si);
1047 }
c5aa993b 1048
5873a88d 1049 /* Finally, update the SP register. */
f75493ed 1050 regcache_cooked_write_unsigned (regcache, D10V_SP_REGNUM,
5873a88d
AC
1051 d10v_convert_daddr_to_raw (sp));
1052
c906108c
SS
1053 return sp;
1054}
1055
1056
1057/* Given a return value in `regbuf' with a type `valtype',
1058 extract and copy its value into `valbuf'. */
1059
f5e1cf12 1060static void
fa1fd571
AC
1061d10v_extract_return_value (struct type *type, struct regcache *regcache,
1062 void *valbuf)
c906108c
SS
1063{
1064 int len;
fa1fd571 1065 if (TYPE_LENGTH (type) == 1)
c906108c 1066 {
fa1fd571
AC
1067 ULONGEST c;
1068 regcache_cooked_read_unsigned (regcache, RET1_REGNUM, &c);
3d79a47c
MS
1069 store_unsigned_integer (valbuf, 1, c);
1070 }
3d79a47c
MS
1071 else
1072 {
1073 /* For return values of odd size, the first byte is in the
1074 least significant part of the first register. The
fa1fd571
AC
1075 remaining bytes in remaining registers. Interestingly, when
1076 such values are passed in, the last byte is in the most
1077 significant byte of that same register - wierd. */
1078 int reg = RET1_REGNUM;
1079 int off = 0;
1080 if (TYPE_LENGTH (type) & 1)
1081 {
1082 regcache_cooked_read_part (regcache, RET1_REGNUM, 1, 1,
1083 (bfd_byte *)valbuf + off);
1084 off++;
1085 reg++;
1086 }
1087 /* Transfer the remaining registers. */
1088 for (; off < TYPE_LENGTH (type); reg++, off += 2)
1089 {
1090 regcache_cooked_read (regcache, RET1_REGNUM + reg,
1091 (bfd_byte *) valbuf + off);
1092 }
c906108c
SS
1093 }
1094}
1095
c2c6d25f
JM
1096/* Translate a GDB virtual ADDR/LEN into a format the remote target
1097 understands. Returns number of bytes that can be transfered
4ce44c66
JM
1098 starting at TARG_ADDR. Return ZERO if no bytes can be transfered
1099 (segmentation fault). Since the simulator knows all about how the
1100 VM system works, we just call that to do the translation. */
c2c6d25f 1101
4ce44c66 1102static void
c2c6d25f
JM
1103remote_d10v_translate_xfer_address (CORE_ADDR memaddr, int nr_bytes,
1104 CORE_ADDR *targ_addr, int *targ_len)
1105{
4ce44c66
JM
1106 long out_addr;
1107 long out_len;
1108 out_len = sim_d10v_translate_addr (memaddr, nr_bytes,
1109 &out_addr,
1110 d10v_dmap_register,
1111 d10v_imap_register);
1112 *targ_addr = out_addr;
1113 *targ_len = out_len;
c2c6d25f
JM
1114}
1115
4ce44c66 1116
c906108c
SS
1117/* The following code implements access to, and display of, the D10V's
1118 instruction trace buffer. The buffer consists of 64K or more
1119 4-byte words of data, of which each words includes an 8-bit count,
1120 an 8-bit segment number, and a 16-bit instruction address.
1121
1122 In theory, the trace buffer is continuously capturing instruction
1123 data that the CPU presents on its "debug bus", but in practice, the
1124 ROMified GDB stub only enables tracing when it continues or steps
1125 the program, and stops tracing when the program stops; so it
1126 actually works for GDB to read the buffer counter out of memory and
1127 then read each trace word. The counter records where the tracing
1128 stops, but there is no record of where it started, so we remember
1129 the PC when we resumed and then search backwards in the trace
1130 buffer for a word that includes that address. This is not perfect,
1131 because you will miss trace data if the resumption PC is the target
1132 of a branch. (The value of the buffer counter is semi-random, any
1133 trace data from a previous program stop is gone.) */
1134
1135/* The address of the last word recorded in the trace buffer. */
1136
1137#define DBBC_ADDR (0xd80000)
1138
1139/* The base of the trace buffer, at least for the "Board_0". */
1140
1141#define TRACE_BUFFER_BASE (0xf40000)
1142
a14ed312 1143static void trace_command (char *, int);
c906108c 1144
a14ed312 1145static void untrace_command (char *, int);
c906108c 1146
a14ed312 1147static void trace_info (char *, int);
c906108c 1148
a14ed312 1149static void tdisassemble_command (char *, int);
c906108c 1150
a14ed312 1151static void display_trace (int, int);
c906108c
SS
1152
1153/* True when instruction traces are being collected. */
1154
1155static int tracing;
1156
1157/* Remembered PC. */
1158
1159static CORE_ADDR last_pc;
1160
1161/* True when trace output should be displayed whenever program stops. */
1162
1163static int trace_display;
1164
1165/* True when trace listing should include source lines. */
1166
1167static int default_trace_show_source = 1;
1168
c5aa993b
JM
1169struct trace_buffer
1170 {
1171 int size;
1172 short *counts;
1173 CORE_ADDR *addrs;
1174 }
1175trace_data;
c906108c
SS
1176
1177static void
fba45db2 1178trace_command (char *args, int from_tty)
c906108c
SS
1179{
1180 /* Clear the host-side trace buffer, allocating space if needed. */
1181 trace_data.size = 0;
1182 if (trace_data.counts == NULL)
f75493ed 1183 trace_data.counts = XCALLOC (65536, short);
c906108c 1184 if (trace_data.addrs == NULL)
f75493ed 1185 trace_data.addrs = XCALLOC (65536, CORE_ADDR);
c906108c
SS
1186
1187 tracing = 1;
1188
1189 printf_filtered ("Tracing is now on.\n");
1190}
1191
1192static void
fba45db2 1193untrace_command (char *args, int from_tty)
c906108c
SS
1194{
1195 tracing = 0;
1196
1197 printf_filtered ("Tracing is now off.\n");
1198}
1199
1200static void
fba45db2 1201trace_info (char *args, int from_tty)
c906108c
SS
1202{
1203 int i;
1204
1205 if (trace_data.size)
1206 {
1207 printf_filtered ("%d entries in trace buffer:\n", trace_data.size);
1208
1209 for (i = 0; i < trace_data.size; ++i)
1210 {
d4f3574e
SS
1211 printf_filtered ("%d: %d instruction%s at 0x%s\n",
1212 i,
1213 trace_data.counts[i],
c906108c 1214 (trace_data.counts[i] == 1 ? "" : "s"),
d4f3574e 1215 paddr_nz (trace_data.addrs[i]));
c906108c
SS
1216 }
1217 }
1218 else
1219 printf_filtered ("No entries in trace buffer.\n");
1220
1221 printf_filtered ("Tracing is currently %s.\n", (tracing ? "on" : "off"));
1222}
1223
392a587b 1224static void
fba45db2 1225d10v_eva_prepare_to_trace (void)
c906108c
SS
1226{
1227 if (!tracing)
1228 return;
1229
27d94c49 1230 last_pc = read_register (D10V_PC_REGNUM);
c906108c
SS
1231}
1232
1233/* Collect trace data from the target board and format it into a form
1234 more useful for display. */
1235
392a587b 1236static void
fba45db2 1237d10v_eva_get_trace_data (void)
c906108c
SS
1238{
1239 int count, i, j, oldsize;
1240 int trace_addr, trace_seg, trace_cnt, next_cnt;
1241 unsigned int last_trace, trace_word, next_word;
1242 unsigned int *tmpspace;
1243
1244 if (!tracing)
1245 return;
1246
c5aa993b 1247 tmpspace = xmalloc (65536 * sizeof (unsigned int));
c906108c
SS
1248
1249 last_trace = read_memory_unsigned_integer (DBBC_ADDR, 2) << 2;
1250
1251 /* Collect buffer contents from the target, stopping when we reach
1252 the word recorded when execution resumed. */
1253
1254 count = 0;
1255 while (last_trace > 0)
1256 {
1257 QUIT;
1258 trace_word =
1259 read_memory_unsigned_integer (TRACE_BUFFER_BASE + last_trace, 4);
1260 trace_addr = trace_word & 0xffff;
1261 last_trace -= 4;
1262 /* Ignore an apparently nonsensical entry. */
1263 if (trace_addr == 0xffd5)
1264 continue;
1265 tmpspace[count++] = trace_word;
1266 if (trace_addr == last_pc)
1267 break;
1268 if (count > 65535)
1269 break;
1270 }
1271
1272 /* Move the data to the host-side trace buffer, adjusting counts to
1273 include the last instruction executed and transforming the address
1274 into something that GDB likes. */
1275
1276 for (i = 0; i < count; ++i)
1277 {
1278 trace_word = tmpspace[i];
1279 next_word = ((i == 0) ? 0 : tmpspace[i - 1]);
1280 trace_addr = trace_word & 0xffff;
1281 next_cnt = (next_word >> 24) & 0xff;
1282 j = trace_data.size + count - i - 1;
1283 trace_data.addrs[j] = (trace_addr << 2) + 0x1000000;
1284 trace_data.counts[j] = next_cnt + 1;
1285 }
1286
1287 oldsize = trace_data.size;
1288 trace_data.size += count;
1289
b8c9b27d 1290 xfree (tmpspace);
c906108c
SS
1291
1292 if (trace_display)
1293 display_trace (oldsize, trace_data.size);
1294}
1295
1296static void
fba45db2 1297tdisassemble_command (char *arg, int from_tty)
c906108c
SS
1298{
1299 int i, count;
1300 CORE_ADDR low, high;
c906108c
SS
1301
1302 if (!arg)
1303 {
1304 low = 0;
1305 high = trace_data.size;
1306 }
c906108c 1307 else
e8933a55
AC
1308 {
1309 char *space_index = strchr (arg, ' ');
1310 if (space_index == NULL)
1311 {
1312 low = parse_and_eval_address (arg);
1313 high = low + 5;
1314 }
1315 else
1316 {
1317 /* Two arguments. */
1318 *space_index = '\0';
1319 low = parse_and_eval_address (arg);
1320 high = parse_and_eval_address (space_index + 1);
1321 if (high < low)
1322 high = low;
1323 }
c906108c
SS
1324 }
1325
d4f3574e 1326 printf_filtered ("Dump of trace from %s to %s:\n", paddr_u (low), paddr_u (high));
c906108c
SS
1327
1328 display_trace (low, high);
1329
1330 printf_filtered ("End of trace dump.\n");
1331 gdb_flush (gdb_stdout);
1332}
1333
1334static void
fba45db2 1335display_trace (int low, int high)
c906108c
SS
1336{
1337 int i, count, trace_show_source, first, suppress;
1338 CORE_ADDR next_address;
1339
1340 trace_show_source = default_trace_show_source;
c5aa993b 1341 if (!have_full_symbols () && !have_partial_symbols ())
c906108c
SS
1342 {
1343 trace_show_source = 0;
1344 printf_filtered ("No symbol table is loaded. Use the \"file\" command.\n");
1345 printf_filtered ("Trace will not display any source.\n");
1346 }
1347
1348 first = 1;
1349 suppress = 0;
1350 for (i = low; i < high; ++i)
1351 {
1352 next_address = trace_data.addrs[i];
c5aa993b 1353 count = trace_data.counts[i];
c906108c
SS
1354 while (count-- > 0)
1355 {
1356 QUIT;
1357 if (trace_show_source)
1358 {
1359 struct symtab_and_line sal, sal_prev;
1360
1361 sal_prev = find_pc_line (next_address - 4, 0);
1362 sal = find_pc_line (next_address, 0);
1363
1364 if (sal.symtab)
1365 {
1366 if (first || sal.line != sal_prev.line)
1367 print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
1368 suppress = 0;
1369 }
1370 else
1371 {
1372 if (!suppress)
1373 /* FIXME-32x64--assumes sal.pc fits in long. */
1374 printf_filtered ("No source file for address %s.\n",
c5aa993b 1375 local_hex_string ((unsigned long) sal.pc));
c906108c
SS
1376 suppress = 1;
1377 }
1378 }
1379 first = 0;
1380 print_address (next_address, gdb_stdout);
1381 printf_filtered (":");
1382 printf_filtered ("\t");
1383 wrap_here (" ");
92bf2b80 1384 next_address += gdb_print_insn (next_address, gdb_stdout);
c906108c
SS
1385 printf_filtered ("\n");
1386 gdb_flush (gdb_stdout);
1387 }
1388 }
1389}
1390
7f6104a9 1391static CORE_ADDR
12cc2063 1392d10v_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
7f6104a9 1393{
12cc2063 1394 ULONGEST pc;
27d94c49 1395 frame_unwind_unsigned_register (next_frame, D10V_PC_REGNUM, &pc);
12cc2063 1396 return d10v_make_iaddr (pc);
7f6104a9
AC
1397}
1398
1399/* Given a GDB frame, determine the address of the calling function's
1400 frame. This will be used to create a new GDB frame struct. */
1401
1402static void
6dc42492
AC
1403d10v_frame_this_id (struct frame_info *next_frame,
1404 void **this_prologue_cache,
1405 struct frame_id *this_id)
7f6104a9 1406{
6dc42492
AC
1407 struct d10v_unwind_cache *info
1408 = d10v_frame_unwind_cache (next_frame, this_prologue_cache);
1409 CORE_ADDR base;
81f8a206
AC
1410 CORE_ADDR func;
1411 struct frame_id id;
7f6104a9 1412
81f8a206
AC
1413 /* The FUNC is easy. */
1414 func = frame_func_unwind (next_frame);
7f6104a9 1415
6dc42492
AC
1416 /* This is meant to halt the backtrace at "_start". Make sure we
1417 don't halt it at a generic dummy frame. */
81f8a206 1418 if (func <= IMEM_START || inside_entry_file (func))
6dc42492 1419 return;
7f6104a9 1420
6dc42492
AC
1421 /* Hopefully the prologue analysis either correctly determined the
1422 frame's base (which is the SP from the previous frame), or set
1423 that base to "NULL". */
270cb5d6 1424 base = info->prev_sp;
6dc42492
AC
1425 if (base == STACK_START || base == 0)
1426 return;
7f6104a9 1427
81f8a206
AC
1428 id = frame_id_build (base, func);
1429
6dc42492
AC
1430 /* Check that we're not going round in circles with the same frame
1431 ID (but avoid applying the test to sentinel frames which do go
1432 round in circles). Can't use frame_id_eq() as that doesn't yet
1433 compare the frame's PC value. */
1434 if (frame_relative_level (next_frame) >= 0
1435 && get_frame_type (next_frame) != DUMMY_FRAME
81f8a206 1436 && frame_id_eq (get_frame_id (next_frame), id))
7f6104a9
AC
1437 return;
1438
81f8a206 1439 (*this_id) = id;
7f6104a9
AC
1440}
1441
1442static void
6dc42492
AC
1443saved_regs_unwinder (struct frame_info *next_frame,
1444 CORE_ADDR *this_saved_regs,
7f6104a9
AC
1445 int regnum, int *optimizedp,
1446 enum lval_type *lvalp, CORE_ADDR *addrp,
1447 int *realnump, void *bufferp)
1448{
6dc42492 1449 if (this_saved_regs[regnum] != 0)
7f6104a9 1450 {
f75493ed 1451 if (regnum == D10V_SP_REGNUM)
7f6104a9
AC
1452 {
1453 /* SP register treated specially. */
1454 *optimizedp = 0;
1455 *lvalp = not_lval;
1456 *addrp = 0;
1457 *realnump = -1;
1458 if (bufferp != NULL)
f75493ed
AC
1459 store_unsigned_integer (bufferp,
1460 register_size (current_gdbarch, regnum),
1461 this_saved_regs[regnum]);
7f6104a9
AC
1462 }
1463 else
1464 {
1465 /* Any other register is saved in memory, fetch it but cache
1466 a local copy of its value. */
1467 *optimizedp = 0;
1468 *lvalp = lval_memory;
6dc42492 1469 *addrp = this_saved_regs[regnum];
7f6104a9
AC
1470 *realnump = -1;
1471 if (bufferp != NULL)
1472 {
1473 /* Read the value in from memory. */
6dc42492 1474 read_memory (this_saved_regs[regnum], bufferp,
08a617da 1475 register_size (current_gdbarch, regnum));
7f6104a9
AC
1476 }
1477 }
1478 return;
1479 }
1480
1481 /* No luck, assume this and the next frame have the same register
1482 value. If a value is needed, pass the request on down the chain;
1483 otherwise just return an indication that the value is in the same
1484 register as the next frame. */
6dc42492
AC
1485 frame_register_unwind (next_frame, regnum, optimizedp, lvalp, addrp,
1486 realnump, bufferp);
7f6104a9
AC
1487}
1488
1489
1490static void
6dc42492
AC
1491d10v_frame_prev_register (struct frame_info *next_frame,
1492 void **this_prologue_cache,
1493 int regnum, int *optimizedp,
1494 enum lval_type *lvalp, CORE_ADDR *addrp,
1495 int *realnump, void *bufferp)
7f6104a9 1496{
6dc42492
AC
1497 struct d10v_unwind_cache *info
1498 = d10v_frame_unwind_cache (next_frame, this_prologue_cache);
27d94c49 1499 if (regnum == D10V_PC_REGNUM)
ef840a37
AC
1500 {
1501 /* The call instruction saves the caller's PC in LR. The
1502 function prologue of the callee may then save the LR on the
1503 stack. Find that possibly saved LR value and return it. */
6dc42492 1504 saved_regs_unwinder (next_frame, info->saved_regs, LR_REGNUM, optimizedp,
ef840a37
AC
1505 lvalp, addrp, realnump, bufferp);
1506 }
1507 else
1508 {
6dc42492 1509 saved_regs_unwinder (next_frame, info->saved_regs, regnum, optimizedp,
ef840a37
AC
1510 lvalp, addrp, realnump, bufferp);
1511 }
7f6104a9
AC
1512}
1513
270cb5d6 1514static const struct frame_unwind d10v_frame_unwind = {
7df05f2b 1515 NORMAL_FRAME,
6dc42492
AC
1516 d10v_frame_this_id,
1517 d10v_frame_prev_register
7f6104a9
AC
1518};
1519
1520const struct frame_unwind *
1521d10v_frame_p (CORE_ADDR pc)
1522{
1523 return &d10v_frame_unwind;
1524}
1525
270cb5d6
AC
1526static CORE_ADDR
1527d10v_frame_base_address (struct frame_info *next_frame, void **this_cache)
1528{
1529 struct d10v_unwind_cache *info
1530 = d10v_frame_unwind_cache (next_frame, this_cache);
1531 return info->base;
1532}
1533
1534static const struct frame_base d10v_frame_base = {
1535 &d10v_frame_unwind,
1536 d10v_frame_base_address,
1537 d10v_frame_base_address,
1538 d10v_frame_base_address
1539};
1540
6314f104
AC
1541/* Assuming NEXT_FRAME->prev is a dummy, return the frame ID of that
1542 dummy frame. The frame ID's base needs to match the TOS value
1543 saved by save_dummy_frame_tos(), and the PC match the dummy frame's
1544 breakpoint. */
1545
1546static struct frame_id
1547d10v_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
1548{
1549 ULONGEST base;
f75493ed 1550 frame_unwind_unsigned_register (next_frame, D10V_SP_REGNUM, &base);
11889732 1551 return frame_id_build (d10v_make_daddr (base), frame_pc_unwind (next_frame));
6314f104
AC
1552}
1553
0f71a2f6 1554static gdbarch_init_ftype d10v_gdbarch_init;
4ce44c66 1555
0f71a2f6 1556static struct gdbarch *
fba45db2 1557d10v_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
0f71a2f6 1558{
0f71a2f6 1559 struct gdbarch *gdbarch;
4ce44c66
JM
1560 int d10v_num_regs;
1561 struct gdbarch_tdep *tdep;
1562 gdbarch_register_name_ftype *d10v_register_name;
7c7651b2 1563 gdbarch_register_sim_regno_ftype *d10v_register_sim_regno;
0f71a2f6 1564
4ce44c66
JM
1565 /* Find a candidate among the list of pre-declared architectures. */
1566 arches = gdbarch_list_lookup_by_info (arches, &info);
0f71a2f6
JM
1567 if (arches != NULL)
1568 return arches->gdbarch;
4ce44c66
JM
1569
1570 /* None found, create a new architecture from the information
1571 provided. */
1572 tdep = XMALLOC (struct gdbarch_tdep);
1573 gdbarch = gdbarch_alloc (&info, tdep);
1574
1575 switch (info.bfd_arch_info->mach)
1576 {
1577 case bfd_mach_d10v_ts2:
1578 d10v_num_regs = 37;
1579 d10v_register_name = d10v_ts2_register_name;
7c7651b2 1580 d10v_register_sim_regno = d10v_ts2_register_sim_regno;
4ce44c66
JM
1581 tdep->a0_regnum = TS2_A0_REGNUM;
1582 tdep->nr_dmap_regs = TS2_NR_DMAP_REGS;
4ce44c66
JM
1583 tdep->dmap_register = d10v_ts2_dmap_register;
1584 tdep->imap_register = d10v_ts2_imap_register;
1585 break;
1586 default:
1587 case bfd_mach_d10v_ts3:
1588 d10v_num_regs = 42;
1589 d10v_register_name = d10v_ts3_register_name;
7c7651b2 1590 d10v_register_sim_regno = d10v_ts3_register_sim_regno;
4ce44c66
JM
1591 tdep->a0_regnum = TS3_A0_REGNUM;
1592 tdep->nr_dmap_regs = TS3_NR_DMAP_REGS;
4ce44c66
JM
1593 tdep->dmap_register = d10v_ts3_dmap_register;
1594 tdep->imap_register = d10v_ts3_imap_register;
1595 break;
1596 }
0f71a2f6
JM
1597
1598 set_gdbarch_read_pc (gdbarch, d10v_read_pc);
1599 set_gdbarch_write_pc (gdbarch, d10v_write_pc);
0f71a2f6 1600 set_gdbarch_read_sp (gdbarch, d10v_read_sp);
0f71a2f6
JM
1601
1602 set_gdbarch_num_regs (gdbarch, d10v_num_regs);
f75493ed 1603 set_gdbarch_sp_regnum (gdbarch, D10V_SP_REGNUM);
0f71a2f6 1604 set_gdbarch_register_name (gdbarch, d10v_register_name);
35cac7cf 1605 set_gdbarch_register_type (gdbarch, d10v_register_type);
0f71a2f6 1606
75af7f68
JB
1607 set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1608 set_gdbarch_addr_bit (gdbarch, 32);
1609 set_gdbarch_address_to_pointer (gdbarch, d10v_address_to_pointer);
1610 set_gdbarch_pointer_to_address (gdbarch, d10v_pointer_to_address);
fc0c74b1 1611 set_gdbarch_integer_to_address (gdbarch, d10v_integer_to_address);
0f71a2f6
JM
1612 set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1613 set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1614 set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
02da6206 1615 set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
f0d4cc9e
AC
1616 /* NOTE: The d10v as a 32 bit ``float'' and ``double''. ``long
1617 double'' is 64 bits. */
0f71a2f6
JM
1618 set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1619 set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1620 set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
f0d4cc9e
AC
1621 switch (info.byte_order)
1622 {
d7449b42 1623 case BFD_ENDIAN_BIG:
f0d4cc9e
AC
1624 set_gdbarch_float_format (gdbarch, &floatformat_ieee_single_big);
1625 set_gdbarch_double_format (gdbarch, &floatformat_ieee_single_big);
1626 set_gdbarch_long_double_format (gdbarch, &floatformat_ieee_double_big);
1627 break;
778eb05e 1628 case BFD_ENDIAN_LITTLE:
f0d4cc9e
AC
1629 set_gdbarch_float_format (gdbarch, &floatformat_ieee_single_little);
1630 set_gdbarch_double_format (gdbarch, &floatformat_ieee_single_little);
1631 set_gdbarch_long_double_format (gdbarch, &floatformat_ieee_double_little);
1632 break;
1633 default:
8e65ff28
AC
1634 internal_error (__FILE__, __LINE__,
1635 "d10v_gdbarch_init: bad byte order for float format");
f0d4cc9e 1636 }
0f71a2f6 1637
fa1fd571 1638 set_gdbarch_extract_return_value (gdbarch, d10v_extract_return_value);
7043d8dc 1639 set_gdbarch_push_dummy_code (gdbarch, d10v_push_dummy_code);
5873a88d 1640 set_gdbarch_push_dummy_call (gdbarch, d10v_push_dummy_call);
fa1fd571
AC
1641 set_gdbarch_store_return_value (gdbarch, d10v_store_return_value);
1642 set_gdbarch_extract_struct_value_address (gdbarch, d10v_extract_struct_value_address);
0f71a2f6
JM
1643 set_gdbarch_use_struct_convention (gdbarch, d10v_use_struct_convention);
1644
0f71a2f6
JM
1645 set_gdbarch_skip_prologue (gdbarch, d10v_skip_prologue);
1646 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1647 set_gdbarch_decr_pc_after_break (gdbarch, 4);
1648 set_gdbarch_function_start_offset (gdbarch, 0);
1649 set_gdbarch_breakpoint_from_pc (gdbarch, d10v_breakpoint_from_pc);
1650
1651 set_gdbarch_remote_translate_xfer_address (gdbarch, remote_d10v_translate_xfer_address);
1652
1653 set_gdbarch_frame_args_skip (gdbarch, 0);
1654 set_gdbarch_frameless_function_invocation (gdbarch, frameless_look_for_prologue);
f4ded5b1 1655
0f71a2f6 1656 set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
6ae2f580 1657 set_gdbarch_frame_align (gdbarch, d10v_frame_align);
0f71a2f6 1658
7c7651b2
AC
1659 set_gdbarch_register_sim_regno (gdbarch, d10v_register_sim_regno);
1660
5f601589
AC
1661 set_gdbarch_print_registers_info (gdbarch, d10v_print_registers_info);
1662
7f6104a9 1663 frame_unwind_append_predicate (gdbarch, d10v_frame_p);
270cb5d6 1664 frame_base_set_default (gdbarch, &d10v_frame_base);
7f6104a9 1665
6314f104
AC
1666 /* Methods for saving / extracting a dummy frame's ID. */
1667 set_gdbarch_unwind_dummy_id (gdbarch, d10v_unwind_dummy_id);
1668 set_gdbarch_save_dummy_frame_tos (gdbarch, generic_save_dummy_frame_tos);
1669
12cc2063
AC
1670 /* Return the unwound PC value. */
1671 set_gdbarch_unwind_pc (gdbarch, d10v_unwind_pc);
1672
0ef21242
AC
1673 set_gdbarch_print_insn (gdbarch, print_insn_d10v);
1674
0f71a2f6
JM
1675 return gdbarch;
1676}
1677
c906108c 1678void
fba45db2 1679_initialize_d10v_tdep (void)
c906108c 1680{
0f71a2f6
JM
1681 register_gdbarch_init (bfd_arch_d10v, d10v_gdbarch_init);
1682
c906108c
SS
1683 target_resume_hook = d10v_eva_prepare_to_trace;
1684 target_wait_loop_hook = d10v_eva_get_trace_data;
1685
5f601589
AC
1686 deprecate_cmd (add_com ("regs", class_vars, show_regs, "Print all registers"),
1687 "info registers");
c906108c 1688
cff3e48b 1689 add_com ("itrace", class_support, trace_command,
c906108c
SS
1690 "Enable tracing of instruction execution.");
1691
cff3e48b 1692 add_com ("iuntrace", class_support, untrace_command,
c906108c
SS
1693 "Disable tracing of instruction execution.");
1694
cff3e48b 1695 add_com ("itdisassemble", class_vars, tdisassemble_command,
c906108c
SS
1696 "Disassemble the trace buffer.\n\
1697Two optional arguments specify a range of trace buffer entries\n\
1698as reported by info trace (NOT addresses!).");
1699
cff3e48b 1700 add_info ("itrace", trace_info,
c906108c
SS
1701 "Display info about the trace data buffer.");
1702
f75493ed
AC
1703 add_setshow_boolean_cmd ("itracedisplay", no_class, &trace_display,
1704 "Set automatic display of trace.\n",
1705 "Show automatic display of trace.\n",
1706 NULL, NULL, &setlist, &showlist);
1707 add_setshow_boolean_cmd ("itracesource", no_class,
1708 &default_trace_show_source,
1709 "Set display of source code with trace.\n",
1710 "Show display of source code with trace.\n",
1711 NULL, NULL, &setlist, &showlist);
c5aa993b 1712}
This page took 0.341071 seconds and 4 git commands to generate.