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