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