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