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