* thread-db.c (check_event): Don't report an error if we encounter
[deliverable/binutils-gdb.git] / gdb / d30v-tdep.c
1 /* Target-dependent code for Mitsubishi D30V, for GDB.
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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. */
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"
34 #include "dis-asm.h"
35 #include "symfile.h"
36 #include "objfiles.h"
37 #include "regcache.h"
38
39 #include "language.h" /* For local_hex_string() */
40
41 void d30v_frame_find_saved_regs (struct frame_info *fi,
42 struct frame_saved_regs *fsr);
43 void d30v_frame_find_saved_regs_offsets (struct frame_info *fi,
44 struct frame_saved_regs *fsr);
45 static void d30v_pop_dummy_frame (struct frame_info *fi);
46 static void d30v_print_flags (void);
47 static void print_flags_command (char *, int);
48
49 /* the following defines assume:
50 fp is r61, lr is r62, sp is r63, and ?? is r22
51 if that changes, they will need to be updated */
52
53 #define OP_MASK_ALL_BUT_RA 0x0ffc0fff /* throw away Ra, keep the rest */
54
55 #define OP_STW_SPM 0x054c0fc0 /* stw Ra, @(sp-) */
56 #define OP_STW_SP_R0 0x05400fc0 /* stw Ra, @(sp,r0) */
57 #define OP_STW_SP_IMM0 0x05480fc0 /* st Ra, @(sp, 0x0) */
58 #define OP_STW_R22P_R0 0x05440580 /* stw Ra, @(r22+,r0) */
59
60 #define OP_ST2W_SPM 0x056c0fc0 /* st2w Ra, @(sp-) */
61 #define OP_ST2W_SP_R0 0x05600fc0 /* st2w Ra, @(sp, r0) */
62 #define OP_ST2W_SP_IMM0 0x05680fc0 /* st2w Ra, @(sp, 0x0) */
63 #define OP_ST2W_R22P_R0 0x05640580 /* st2w Ra, @(r22+, r0) */
64
65 #define OP_MASK_OPCODE 0x0ffc0000 /* just the opcode, ign operands */
66 #define OP_NOP 0x00f00000 /* nop */
67
68 #define OP_MASK_ALL_BUT_IMM 0x0fffffc0 /* throw away imm, keep the rest */
69 #define OP_SUB_SP_IMM 0x082bffc0 /* sub sp,sp,imm */
70 #define OP_ADD_SP_IMM 0x080bffc0 /* add sp,sp,imm */
71 #define OP_ADD_R22_SP_IMM 0x08096fc0 /* add r22,sp,imm */
72 #define OP_STW_FP_SP_IMM 0x054bdfc0 /* stw fp,@(sp,imm) */
73 #define OP_OR_SP_R0_IMM 0x03abf000 /* or sp,r0,imm */
74
75 /* no mask */
76 #define OP_OR_FP_R0_SP 0x03a3d03f /* or fp,r0,sp */
77 #define OP_OR_FP_SP_R0 0x03a3dfc0 /* or fp,sp,r0 */
78 #define OP_OR_FP_IMM0_SP 0x03abd03f /* or fp,0x0,sp */
79 #define OP_STW_FP_R22P_R0 0x0547d580 /* stw fp,@(r22+,r0) */
80 #define OP_STW_LR_R22P_R0 0x0547e580 /* stw lr,@(r22+,r0) */
81
82 #define OP_MASK_OP_AND_RB 0x0ff80fc0 /* keep op and rb,throw away rest */
83 #define OP_STW_SP_IMM 0x05480fc0 /* stw Ra,@(sp,imm) */
84 #define OP_ST2W_SP_IMM 0x05680fc0 /* st2w Ra,@(sp,imm) */
85 #define OP_STW_FP_IMM 0x05480f40 /* stw Ra,@(fp,imm) */
86 #define OP_STW_FP_R0 0x05400f40 /* stw Ra,@(fp,r0) */
87
88 #define OP_MASK_FM_BIT 0x80000000
89 #define OP_MASK_CC_BITS 0x70000000
90 #define OP_MASK_SUB_INST 0x0fffffff
91
92 #define EXTRACT_RA(op) (((op) >> 12) & 0x3f)
93 #define EXTRACT_RB(op) (((op) >> 6) & 0x3f)
94 #define EXTRACT_RC(op) (((op) & 0x3f)
95 #define EXTRACT_UIMM6(op) ((op) & 0x3f)
96 #define EXTRACT_IMM6(op) ((((int)EXTRACT_UIMM6(op)) << 26) >> 26)
97 #define EXTRACT_IMM26(op) ((((op)&0x0ff00000) >> 2) | ((op)&0x0003ffff))
98 #define EXTRACT_IMM32(opl, opr) ((EXTRACT_UIMM6(opl) << 26)|EXTRACT_IMM26(opr))
99
100
101 int
102 d30v_frame_chain_valid (CORE_ADDR chain, struct frame_info *fi)
103 {
104 #if 0
105 return ((chain) != 0 && (fi) != 0 && (fi)->return_pc != 0);
106 #else
107 return ((chain) != 0 && (fi) != 0 && (fi)->frame <= chain);
108 #endif
109 }
110
111 /* Discard from the stack the innermost frame, restoring all saved
112 registers. */
113
114 void
115 d30v_pop_frame (void)
116 {
117 struct frame_info *frame = get_current_frame ();
118 CORE_ADDR fp;
119 int regnum;
120 struct frame_saved_regs fsr;
121 char raw_buffer[8];
122
123 fp = FRAME_FP (frame);
124 if (frame->dummy)
125 {
126 d30v_pop_dummy_frame (frame);
127 return;
128 }
129
130 /* fill out fsr with the address of where each */
131 /* register was stored in the frame */
132 get_frame_saved_regs (frame, &fsr);
133
134 /* now update the current registers with the old values */
135 for (regnum = A0_REGNUM; regnum < A0_REGNUM + 2; regnum++)
136 {
137 if (fsr.regs[regnum])
138 {
139 read_memory (fsr.regs[regnum], raw_buffer, 8);
140 write_register_bytes (REGISTER_BYTE (regnum), raw_buffer, 8);
141 }
142 }
143 for (regnum = 0; regnum < SP_REGNUM; regnum++)
144 {
145 if (fsr.regs[regnum])
146 {
147 write_register (regnum, read_memory_unsigned_integer (fsr.regs[regnum], 4));
148 }
149 }
150 if (fsr.regs[PSW_REGNUM])
151 {
152 write_register (PSW_REGNUM, read_memory_unsigned_integer (fsr.regs[PSW_REGNUM], 4));
153 }
154
155 write_register (PC_REGNUM, read_register (LR_REGNUM));
156 write_register (SP_REGNUM, fp + frame->size);
157 target_store_registers (-1);
158 flush_cached_frames ();
159 }
160
161 static int
162 check_prologue (unsigned long op)
163 {
164 /* add sp,sp,imm -- observed */
165 if ((op & OP_MASK_ALL_BUT_IMM) == OP_ADD_SP_IMM)
166 return 1;
167
168 /* add r22,sp,imm -- observed */
169 if ((op & OP_MASK_ALL_BUT_IMM) == OP_ADD_R22_SP_IMM)
170 return 1;
171
172 /* or fp,r0,sp -- observed */
173 if (op == OP_OR_FP_R0_SP)
174 return 1;
175
176 /* nop */
177 if ((op & OP_MASK_OPCODE) == OP_NOP)
178 return 1;
179
180 /* stw Ra,@(sp,r0) */
181 if ((op & OP_MASK_ALL_BUT_RA) == OP_STW_SP_R0)
182 return 1;
183
184 /* stw Ra,@(sp,0x0) */
185 if ((op & OP_MASK_ALL_BUT_RA) == OP_STW_SP_IMM0)
186 return 1;
187
188 /* st2w Ra,@(sp,r0) */
189 if ((op & OP_MASK_ALL_BUT_RA) == OP_ST2W_SP_R0)
190 return 1;
191
192 /* st2w Ra,@(sp,0x0) */
193 if ((op & OP_MASK_ALL_BUT_RA) == OP_ST2W_SP_IMM0)
194 return 1;
195
196 /* stw fp, @(r22+,r0) -- observed */
197 if (op == OP_STW_FP_R22P_R0)
198 return 1;
199
200 /* stw r62, @(r22+,r0) -- observed */
201 if (op == OP_STW_LR_R22P_R0)
202 return 1;
203
204 /* stw Ra, @(fp,r0) -- observed */
205 if ((op & OP_MASK_ALL_BUT_RA) == OP_STW_FP_R0)
206 return 1; /* first arg */
207
208 /* stw Ra, @(fp,imm) -- observed */
209 if ((op & OP_MASK_OP_AND_RB) == OP_STW_FP_IMM)
210 return 1; /* second and subsequent args */
211
212 /* stw fp,@(sp,imm) -- observed */
213 if ((op & OP_MASK_ALL_BUT_IMM) == OP_STW_FP_SP_IMM)
214 return 1;
215
216 /* st2w Ra,@(r22+,r0) */
217 if ((op & OP_MASK_ALL_BUT_RA) == OP_ST2W_R22P_R0)
218 return 1;
219
220 /* stw Ra, @(sp-) */
221 if ((op & OP_MASK_ALL_BUT_RA) == OP_STW_SPM)
222 return 1;
223
224 /* st2w Ra, @(sp-) */
225 if ((op & OP_MASK_ALL_BUT_RA) == OP_ST2W_SPM)
226 return 1;
227
228 /* sub.? sp,sp,imm */
229 if ((op & OP_MASK_ALL_BUT_IMM) == OP_SUB_SP_IMM)
230 return 1;
231
232 return 0;
233 }
234
235 CORE_ADDR
236 d30v_skip_prologue (CORE_ADDR pc)
237 {
238 unsigned long op[2];
239 unsigned long opl, opr; /* left / right sub operations */
240 unsigned long fm0, fm1; /* left / right mode bits */
241 unsigned long cc0, cc1;
242 unsigned long op1, op2;
243 CORE_ADDR func_addr, func_end;
244 struct symtab_and_line sal;
245
246 /* If we have line debugging information, then the end of the */
247 /* prologue should the first assembly instruction of the first source line */
248 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
249 {
250 sal = find_pc_line (func_addr, 0);
251 if (sal.end && sal.end < func_end)
252 return sal.end;
253 }
254
255 if (target_read_memory (pc, (char *) &op[0], 8))
256 return pc; /* Can't access it -- assume no prologue. */
257
258 while (1)
259 {
260 opl = (unsigned long) read_memory_integer (pc, 4);
261 opr = (unsigned long) read_memory_integer (pc + 4, 4);
262
263 fm0 = (opl & OP_MASK_FM_BIT);
264 fm1 = (opr & OP_MASK_FM_BIT);
265
266 cc0 = (opl & OP_MASK_CC_BITS);
267 cc1 = (opr & OP_MASK_CC_BITS);
268
269 opl = (opl & OP_MASK_SUB_INST);
270 opr = (opr & OP_MASK_SUB_INST);
271
272 if (fm0 && fm1)
273 {
274 /* long instruction (opl contains the opcode) */
275 if (((opl & OP_MASK_ALL_BUT_IMM) != OP_ADD_SP_IMM) && /* add sp,sp,imm */
276 ((opl & OP_MASK_ALL_BUT_IMM) != OP_ADD_R22_SP_IMM) && /* add r22,sp,imm */
277 ((opl & OP_MASK_OP_AND_RB) != OP_STW_SP_IMM) && /* stw Ra, @(sp,imm) */
278 ((opl & OP_MASK_OP_AND_RB) != OP_ST2W_SP_IMM)) /* st2w Ra, @(sp,imm) */
279 break;
280 }
281 else
282 {
283 /* short instructions */
284 if (fm0 && !fm1)
285 {
286 op1 = opr;
287 op2 = opl;
288 }
289 else
290 {
291 op1 = opl;
292 op2 = opr;
293 }
294 if (check_prologue (op1))
295 {
296 if (!check_prologue (op2))
297 {
298 /* if the previous opcode was really part of the prologue */
299 /* and not just a NOP, then we want to break after both instructions */
300 if ((op1 & OP_MASK_OPCODE) != OP_NOP)
301 pc += 8;
302 break;
303 }
304 }
305 else
306 break;
307 }
308 pc += 8;
309 }
310 return pc;
311 }
312
313 static int end_of_stack;
314
315 /* Given a GDB frame, determine the address of the calling function's frame.
316 This will be used to create a new GDB frame struct, and then
317 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
318 */
319
320 CORE_ADDR
321 d30v_frame_chain (struct frame_info *frame)
322 {
323 struct frame_saved_regs fsr;
324
325 d30v_frame_find_saved_regs (frame, &fsr);
326
327 if (end_of_stack)
328 return (CORE_ADDR) 0;
329
330 if (frame->return_pc == IMEM_START)
331 return (CORE_ADDR) 0;
332
333 if (!fsr.regs[FP_REGNUM])
334 {
335 if (!fsr.regs[SP_REGNUM] || fsr.regs[SP_REGNUM] == STACK_START)
336 return (CORE_ADDR) 0;
337
338 return fsr.regs[SP_REGNUM];
339 }
340
341 if (!read_memory_unsigned_integer (fsr.regs[FP_REGNUM], 4))
342 return (CORE_ADDR) 0;
343
344 return read_memory_unsigned_integer (fsr.regs[FP_REGNUM], 4);
345 }
346
347 static int next_addr, uses_frame;
348 static int frame_size;
349
350 static int
351 prologue_find_regs (unsigned long op, struct frame_saved_regs *fsr,
352 CORE_ADDR addr)
353 {
354 int n;
355 int offset;
356
357 /* add sp,sp,imm -- observed */
358 if ((op & OP_MASK_ALL_BUT_IMM) == OP_ADD_SP_IMM)
359 {
360 offset = EXTRACT_IMM6 (op);
361 /*next_addr += offset; */
362 frame_size += -offset;
363 return 1;
364 }
365
366 /* add r22,sp,imm -- observed */
367 if ((op & OP_MASK_ALL_BUT_IMM) == OP_ADD_R22_SP_IMM)
368 {
369 offset = EXTRACT_IMM6 (op);
370 next_addr = (offset - frame_size);
371 return 1;
372 }
373
374 /* stw Ra, @(fp, offset) -- observed */
375 if ((op & OP_MASK_OP_AND_RB) == OP_STW_FP_IMM)
376 {
377 n = EXTRACT_RA (op);
378 offset = EXTRACT_IMM6 (op);
379 fsr->regs[n] = (offset - frame_size);
380 return 1;
381 }
382
383 /* stw Ra, @(fp, r0) -- observed */
384 if ((op & OP_MASK_ALL_BUT_RA) == OP_STW_FP_R0)
385 {
386 n = EXTRACT_RA (op);
387 fsr->regs[n] = (-frame_size);
388 return 1;
389 }
390
391 /* or fp,0,sp -- observed */
392 if ((op == OP_OR_FP_R0_SP) ||
393 (op == OP_OR_FP_SP_R0) ||
394 (op == OP_OR_FP_IMM0_SP))
395 {
396 uses_frame = 1;
397 return 1;
398 }
399
400 /* nop */
401 if ((op & OP_MASK_OPCODE) == OP_NOP)
402 return 1;
403
404 /* stw Ra,@(r22+,r0) -- observed */
405 if ((op & OP_MASK_ALL_BUT_RA) == OP_STW_R22P_R0)
406 {
407 n = EXTRACT_RA (op);
408 fsr->regs[n] = next_addr;
409 next_addr += 4;
410 return 1;
411 }
412 #if 0 /* subsumed in pattern above */
413 /* stw fp,@(r22+,r0) -- observed */
414 if (op == OP_STW_FP_R22P_R0)
415 {
416 fsr->regs[FP_REGNUM] = next_addr; /* XXX */
417 next_addr += 4;
418 return 1;
419 }
420
421 /* stw r62,@(r22+,r0) -- observed */
422 if (op == OP_STW_LR_R22P_R0)
423 {
424 fsr->regs[LR_REGNUM] = next_addr;
425 next_addr += 4;
426 return 1;
427 }
428 #endif
429 /* st2w Ra,@(r22+,r0) -- observed */
430 if ((op & OP_MASK_ALL_BUT_RA) == OP_ST2W_R22P_R0)
431 {
432 n = EXTRACT_RA (op);
433 fsr->regs[n] = next_addr;
434 fsr->regs[n + 1] = next_addr + 4;
435 next_addr += 8;
436 return 1;
437 }
438
439 /* stw rn, @(sp-) */
440 if ((op & OP_MASK_ALL_BUT_RA) == OP_STW_SPM)
441 {
442 n = EXTRACT_RA (op);
443 fsr->regs[n] = next_addr;
444 next_addr -= 4;
445 return 1;
446 }
447
448 /* st2w Ra, @(sp-) */
449 else if ((op & OP_MASK_ALL_BUT_RA) == OP_ST2W_SPM)
450 {
451 n = EXTRACT_RA (op);
452 fsr->regs[n] = next_addr;
453 fsr->regs[n + 1] = next_addr + 4;
454 next_addr -= 8;
455 return 1;
456 }
457
458 /* sub sp,sp,imm */
459 if ((op & OP_MASK_ALL_BUT_IMM) == OP_SUB_SP_IMM)
460 {
461 offset = EXTRACT_IMM6 (op);
462 frame_size += -offset;
463 return 1;
464 }
465
466 /* st rn, @(sp,0) -- observed */
467 if (((op & OP_MASK_ALL_BUT_RA) == OP_STW_SP_R0) ||
468 ((op & OP_MASK_ALL_BUT_RA) == OP_STW_SP_IMM0))
469 {
470 n = EXTRACT_RA (op);
471 fsr->regs[n] = (-frame_size);
472 return 1;
473 }
474
475 /* st2w rn, @(sp,0) */
476 if (((op & OP_MASK_ALL_BUT_RA) == OP_ST2W_SP_R0) ||
477 ((op & OP_MASK_ALL_BUT_RA) == OP_ST2W_SP_IMM0))
478 {
479 n = EXTRACT_RA (op);
480 fsr->regs[n] = (-frame_size);
481 fsr->regs[n + 1] = (-frame_size) + 4;
482 return 1;
483 }
484
485 /* stw fp,@(sp,imm) -- observed */
486 if ((op & OP_MASK_ALL_BUT_IMM) == OP_STW_FP_SP_IMM)
487 {
488 offset = EXTRACT_IMM6 (op);
489 fsr->regs[FP_REGNUM] = (offset - frame_size);
490 return 1;
491 }
492 return 0;
493 }
494
495 /* Put here the code to store, into a struct frame_saved_regs, the
496 addresses of the saved registers of frame described by FRAME_INFO.
497 This includes special registers such as pc and fp saved in special
498 ways in the stack frame. sp is even more special: the address we
499 return for it IS the sp for the next frame. */
500 void
501 d30v_frame_find_saved_regs (struct frame_info *fi, struct frame_saved_regs *fsr)
502 {
503 CORE_ADDR fp, pc;
504 unsigned long opl, opr;
505 unsigned long op1, op2;
506 unsigned long fm0, fm1;
507 int i;
508
509 fp = fi->frame;
510 memset (fsr, 0, sizeof (*fsr));
511 next_addr = 0;
512 frame_size = 0;
513 end_of_stack = 0;
514
515 uses_frame = 0;
516
517 d30v_frame_find_saved_regs_offsets (fi, fsr);
518
519 fi->size = frame_size;
520
521 if (!fp)
522 fp = read_register (SP_REGNUM);
523
524 for (i = 0; i < NUM_REGS - 1; i++)
525 if (fsr->regs[i])
526 {
527 fsr->regs[i] = fsr->regs[i] + fp + frame_size;
528 }
529
530 if (fsr->regs[LR_REGNUM])
531 fi->return_pc = read_memory_unsigned_integer (fsr->regs[LR_REGNUM], 4);
532 else
533 fi->return_pc = read_register (LR_REGNUM);
534
535 /* the SP is not normally (ever?) saved, but check anyway */
536 if (!fsr->regs[SP_REGNUM])
537 {
538 /* if the FP was saved, that means the current FP is valid, */
539 /* otherwise, it isn't being used, so we use the SP instead */
540 if (uses_frame)
541 fsr->regs[SP_REGNUM] = read_register (FP_REGNUM) + fi->size;
542 else
543 {
544 fsr->regs[SP_REGNUM] = fp + fi->size;
545 fi->frameless = 1;
546 fsr->regs[FP_REGNUM] = 0;
547 }
548 }
549 }
550
551 void
552 d30v_frame_find_saved_regs_offsets (struct frame_info *fi,
553 struct frame_saved_regs *fsr)
554 {
555 CORE_ADDR fp, pc;
556 unsigned long opl, opr;
557 unsigned long op1, op2;
558 unsigned long fm0, fm1;
559 int i;
560
561 fp = fi->frame;
562 memset (fsr, 0, sizeof (*fsr));
563 next_addr = 0;
564 frame_size = 0;
565 end_of_stack = 0;
566
567 pc = get_pc_function_start (fi->pc);
568
569 uses_frame = 0;
570 while (pc < fi->pc)
571 {
572 opl = (unsigned long) read_memory_integer (pc, 4);
573 opr = (unsigned long) read_memory_integer (pc + 4, 4);
574
575 fm0 = (opl & OP_MASK_FM_BIT);
576 fm1 = (opr & OP_MASK_FM_BIT);
577
578 opl = (opl & OP_MASK_SUB_INST);
579 opr = (opr & OP_MASK_SUB_INST);
580
581 if (fm0 && fm1)
582 {
583 /* long instruction */
584 if ((opl & OP_MASK_ALL_BUT_IMM) == OP_ADD_SP_IMM)
585 {
586 /* add sp,sp,n */
587 long offset = EXTRACT_IMM32 (opl, opr);
588 frame_size += -offset;
589 }
590 else if ((opl & OP_MASK_ALL_BUT_IMM) == OP_ADD_R22_SP_IMM)
591 {
592 /* add r22,sp,offset */
593 long offset = EXTRACT_IMM32 (opl, opr);
594 next_addr = (offset - frame_size);
595 }
596 else if ((opl & OP_MASK_OP_AND_RB) == OP_STW_SP_IMM)
597 {
598 /* st Ra, @(sp,imm) */
599 long offset = EXTRACT_IMM32 (opl, opr);
600 short n = EXTRACT_RA (opl);
601 fsr->regs[n] = (offset - frame_size);
602 }
603 else if ((opl & OP_MASK_OP_AND_RB) == OP_ST2W_SP_IMM)
604 {
605 /* st2w Ra, @(sp,offset) */
606 long offset = EXTRACT_IMM32 (opl, opr);
607 short n = EXTRACT_RA (opl);
608 fsr->regs[n] = (offset - frame_size);
609 fsr->regs[n + 1] = (offset - frame_size) + 4;
610 }
611 else if ((opl & OP_MASK_ALL_BUT_IMM) == OP_OR_SP_R0_IMM)
612 {
613 end_of_stack = 1;
614 }
615 else
616 break;
617 }
618 else
619 {
620 /* short instructions */
621 if (fm0 && !fm1)
622 {
623 op2 = opl;
624 op1 = opr;
625 }
626 else
627 {
628 op1 = opl;
629 op2 = opr;
630 }
631 if (!prologue_find_regs (op1, fsr, pc) || !prologue_find_regs (op2, fsr, pc))
632 break;
633 }
634 pc += 8;
635 }
636
637 #if 0
638 fi->size = frame_size;
639
640 if (!fp)
641 fp = read_register (SP_REGNUM);
642
643 for (i = 0; i < NUM_REGS - 1; i++)
644 if (fsr->regs[i])
645 {
646 fsr->regs[i] = fsr->regs[i] + fp + frame_size;
647 }
648
649 if (fsr->regs[LR_REGNUM])
650 fi->return_pc = read_memory_unsigned_integer (fsr->regs[LR_REGNUM], 4);
651 else
652 fi->return_pc = read_register (LR_REGNUM);
653
654 /* the SP is not normally (ever?) saved, but check anyway */
655 if (!fsr->regs[SP_REGNUM])
656 {
657 /* if the FP was saved, that means the current FP is valid, */
658 /* otherwise, it isn't being used, so we use the SP instead */
659 if (uses_frame)
660 fsr->regs[SP_REGNUM] = read_register (FP_REGNUM) + fi->size;
661 else
662 {
663 fsr->regs[SP_REGNUM] = fp + fi->size;
664 fi->frameless = 1;
665 fsr->regs[FP_REGNUM] = 0;
666 }
667 }
668 #endif
669 }
670
671 void
672 d30v_init_extra_frame_info (int fromleaf, struct frame_info *fi)
673 {
674 struct frame_saved_regs dummy;
675
676 if (fi->next && (fi->pc == 0))
677 fi->pc = fi->next->return_pc;
678
679 d30v_frame_find_saved_regs_offsets (fi, &dummy);
680
681 if (uses_frame == 0)
682 fi->frameless = 1;
683 else
684 fi->frameless = 0;
685
686 if ((fi->next == 0) && (uses_frame == 0))
687 /* innermost frame and it's "frameless",
688 so the fi->frame field is wrong, fix it! */
689 fi->frame = read_sp ();
690
691 if (dummy.regs[LR_REGNUM])
692 {
693 /* it was saved, grab it! */
694 dummy.regs[LR_REGNUM] += (fi->frame + frame_size);
695 fi->return_pc = read_memory_unsigned_integer (dummy.regs[LR_REGNUM], 4);
696 }
697 else
698 fi->return_pc = read_register (LR_REGNUM);
699 }
700
701 void
702 d30v_init_frame_pc (int fromleaf, struct frame_info *prev)
703 {
704 /* default value, put here so we can breakpoint on it and
705 see if the default value is really the right thing to use */
706 prev->pc = (fromleaf ? SAVED_PC_AFTER_CALL (prev->next) : \
707 prev->next ? FRAME_SAVED_PC (prev->next) : read_pc ());
708 }
709
710 static void d30v_print_register (int regnum, int tabular);
711
712 static void
713 d30v_print_register (int regnum, int tabular)
714 {
715 if (regnum < A0_REGNUM)
716 {
717 if (tabular)
718 printf_filtered ("%08lx", (long) read_register (regnum));
719 else
720 printf_filtered ("0x%lx %ld",
721 (long) read_register (regnum),
722 (long) read_register (regnum));
723 }
724 else
725 {
726 char regbuf[MAX_REGISTER_RAW_SIZE];
727
728 read_relative_register_raw_bytes (regnum, regbuf);
729
730 val_print (REGISTER_VIRTUAL_TYPE (regnum), regbuf, 0, 0,
731 gdb_stdout, 'x', 1, 0, Val_pretty_default);
732
733 if (!tabular)
734 {
735 printf_filtered (" ");
736 val_print (REGISTER_VIRTUAL_TYPE (regnum), regbuf, 0, 0,
737 gdb_stdout, 'd', 1, 0, Val_pretty_default);
738 }
739 }
740 }
741
742 static void
743 d30v_print_flags (void)
744 {
745 long psw = read_register (PSW_REGNUM);
746 printf_filtered ("flags #1");
747 printf_filtered (" (sm) %d", (psw & PSW_SM) != 0);
748 printf_filtered (" (ea) %d", (psw & PSW_EA) != 0);
749 printf_filtered (" (db) %d", (psw & PSW_DB) != 0);
750 printf_filtered (" (ds) %d", (psw & PSW_DS) != 0);
751 printf_filtered (" (ie) %d", (psw & PSW_IE) != 0);
752 printf_filtered (" (rp) %d", (psw & PSW_RP) != 0);
753 printf_filtered (" (md) %d\n", (psw & PSW_MD) != 0);
754
755 printf_filtered ("flags #2");
756 printf_filtered (" (f0) %d", (psw & PSW_F0) != 0);
757 printf_filtered (" (f1) %d", (psw & PSW_F1) != 0);
758 printf_filtered (" (f2) %d", (psw & PSW_F2) != 0);
759 printf_filtered (" (f3) %d", (psw & PSW_F3) != 0);
760 printf_filtered (" (s) %d", (psw & PSW_S) != 0);
761 printf_filtered (" (v) %d", (psw & PSW_V) != 0);
762 printf_filtered (" (va) %d", (psw & PSW_VA) != 0);
763 printf_filtered (" (c) %d\n", (psw & PSW_C) != 0);
764 }
765
766 static void
767 print_flags_command (char *args, int from_tty)
768 {
769 d30v_print_flags ();
770 }
771
772 void
773 d30v_do_registers_info (int regnum, int fpregs)
774 {
775 long long num1, num2;
776 long psw;
777
778 if (regnum != -1)
779 {
780 if (REGISTER_NAME (0) == NULL || REGISTER_NAME (0)[0] == '\000')
781 return;
782
783 printf_filtered ("%s ", REGISTER_NAME (regnum));
784 d30v_print_register (regnum, 0);
785
786 printf_filtered ("\n");
787 return;
788 }
789
790 /* Have to print all the registers. Format them nicely. */
791
792 printf_filtered ("PC=");
793 print_address (read_pc (), gdb_stdout);
794
795 printf_filtered (" PSW=");
796 d30v_print_register (PSW_REGNUM, 1);
797
798 printf_filtered (" BPC=");
799 print_address (read_register (BPC_REGNUM), gdb_stdout);
800
801 printf_filtered (" BPSW=");
802 d30v_print_register (BPSW_REGNUM, 1);
803 printf_filtered ("\n");
804
805 printf_filtered ("DPC=");
806 print_address (read_register (DPC_REGNUM), gdb_stdout);
807
808 printf_filtered (" DPSW=");
809 d30v_print_register (DPSW_REGNUM, 1);
810
811 printf_filtered (" IBA=");
812 print_address (read_register (IBA_REGNUM), gdb_stdout);
813 printf_filtered ("\n");
814
815 printf_filtered ("RPT_C=");
816 d30v_print_register (RPT_C_REGNUM, 1);
817
818 printf_filtered (" RPT_S=");
819 print_address (read_register (RPT_S_REGNUM), gdb_stdout);
820
821 printf_filtered (" RPT_E=");
822 print_address (read_register (RPT_E_REGNUM), gdb_stdout);
823 printf_filtered ("\n");
824
825 printf_filtered ("MOD_S=");
826 print_address (read_register (MOD_S_REGNUM), gdb_stdout);
827
828 printf_filtered (" MOD_E=");
829 print_address (read_register (MOD_E_REGNUM), gdb_stdout);
830 printf_filtered ("\n");
831
832 printf_filtered ("EIT_VB=");
833 print_address (read_register (EIT_VB_REGNUM), gdb_stdout);
834
835 printf_filtered (" INT_S=");
836 d30v_print_register (INT_S_REGNUM, 1);
837
838 printf_filtered (" INT_M=");
839 d30v_print_register (INT_M_REGNUM, 1);
840 printf_filtered ("\n");
841
842 d30v_print_flags ();
843 for (regnum = 0; regnum <= 63;)
844 {
845 int i;
846
847 printf_filtered ("R%d-R%d ", regnum, regnum + 7);
848 if (regnum < 10)
849 printf_filtered (" ");
850 if (regnum + 7 < 10)
851 printf_filtered (" ");
852
853 for (i = 0; i < 8; i++)
854 {
855 printf_filtered (" ");
856 d30v_print_register (regnum++, 1);
857 }
858
859 printf_filtered ("\n");
860 }
861
862 printf_filtered ("A0-A1 ");
863
864 d30v_print_register (A0_REGNUM, 1);
865 printf_filtered (" ");
866 d30v_print_register (A1_REGNUM, 1);
867 printf_filtered ("\n");
868 }
869
870 CORE_ADDR
871 d30v_fix_call_dummy (char *dummyname, CORE_ADDR start_sp, CORE_ADDR fun,
872 int nargs, value_ptr *args, struct type *type, int gcc_p)
873 {
874 int regnum;
875 CORE_ADDR sp;
876 char buffer[MAX_REGISTER_RAW_SIZE];
877 struct frame_info *frame = get_current_frame ();
878 frame->dummy = start_sp;
879 /*start_sp |= DMEM_START; */
880
881 sp = start_sp;
882 for (regnum = 0; regnum < NUM_REGS; regnum++)
883 {
884 sp -= REGISTER_RAW_SIZE (regnum);
885 store_address (buffer, REGISTER_RAW_SIZE (regnum), read_register (regnum));
886 write_memory (sp, buffer, REGISTER_RAW_SIZE (regnum));
887 }
888 write_register (SP_REGNUM, (LONGEST) sp);
889 /* now we need to load LR with the return address */
890 write_register (LR_REGNUM, (LONGEST) d30v_call_dummy_address ());
891 return sp;
892 }
893
894 static void
895 d30v_pop_dummy_frame (struct frame_info *fi)
896 {
897 CORE_ADDR sp = fi->dummy;
898 int regnum;
899
900 for (regnum = 0; regnum < NUM_REGS; regnum++)
901 {
902 sp -= REGISTER_RAW_SIZE (regnum);
903 write_register (regnum, read_memory_unsigned_integer (sp, REGISTER_RAW_SIZE (regnum)));
904 }
905 flush_cached_frames (); /* needed? */
906 }
907
908
909 CORE_ADDR
910 d30v_push_arguments (int nargs, value_ptr *args, CORE_ADDR sp,
911 int struct_return, CORE_ADDR struct_addr)
912 {
913 int i, len, index = 0, regnum = 2;
914 char buffer[4], *contents;
915 LONGEST val;
916 CORE_ADDR ptrs[10];
917
918 #if 0
919 /* Pass 1. Put all large args on stack */
920 for (i = 0; i < nargs; i++)
921 {
922 value_ptr arg = args[i];
923 struct type *arg_type = check_typedef (VALUE_TYPE (arg));
924 len = TYPE_LENGTH (arg_type);
925 contents = VALUE_CONTENTS (arg);
926 val = extract_signed_integer (contents, len);
927 if (len > 4)
928 {
929 /* put on stack and pass pointers */
930 sp -= len;
931 write_memory (sp, contents, len);
932 ptrs[index++] = sp;
933 }
934 }
935 #endif
936 index = 0;
937
938 for (i = 0; i < nargs; i++)
939 {
940 value_ptr arg = args[i];
941 struct type *arg_type = check_typedef (VALUE_TYPE (arg));
942 len = TYPE_LENGTH (arg_type);
943 contents = VALUE_CONTENTS (arg);
944 if (len > 4)
945 {
946 /* we need multiple registers */
947 int ndx;
948
949 for (ndx = 0; len > 0; ndx += 8, len -= 8)
950 {
951 if (regnum & 1)
952 regnum++; /* all args > 4 bytes start in even register */
953
954 if (regnum < 18)
955 {
956 val = extract_signed_integer (&contents[ndx], 4);
957 write_register (regnum++, val);
958
959 if (len >= 8)
960 val = extract_signed_integer (&contents[ndx + 4], 4);
961 else
962 val = extract_signed_integer (&contents[ndx + 4], len - 4);
963 write_register (regnum++, val);
964 }
965 else
966 {
967 /* no more registers available. put it on the stack */
968
969 /* all args > 4 bytes are padded to a multiple of 8 bytes
970 and start on an 8 byte boundary */
971 if (sp & 7)
972 sp -= (sp & 7); /* align it */
973
974 sp -= ((len + 7) & ~7); /* allocate space */
975 write_memory (sp, &contents[ndx], len);
976 break;
977 }
978 }
979 }
980 else
981 {
982 if (regnum < 18)
983 {
984 val = extract_signed_integer (contents, len);
985 write_register (regnum++, val);
986 }
987 else
988 {
989 /* all args are padded to a multiple of 4 bytes (at least) */
990 sp -= ((len + 3) & ~3);
991 write_memory (sp, contents, len);
992 }
993 }
994 }
995 if (sp & 7)
996 /* stack pointer is not on an 8 byte boundary -- align it */
997 sp -= (sp & 7);
998 return sp;
999 }
1000
1001
1002 /* pick an out-of-the-way place to set the return value */
1003 /* for an inferior function call. The link register is set to this */
1004 /* value and a momentary breakpoint is set there. When the breakpoint */
1005 /* is hit, the dummy frame is popped and the previous environment is */
1006 /* restored. */
1007
1008 CORE_ADDR
1009 d30v_call_dummy_address (void)
1010 {
1011 CORE_ADDR entry;
1012 struct minimal_symbol *sym;
1013
1014 entry = entry_point_address ();
1015
1016 if (entry != 0)
1017 return entry;
1018
1019 sym = lookup_minimal_symbol ("_start", NULL, symfile_objfile);
1020
1021 if (!sym || MSYMBOL_TYPE (sym) != mst_text)
1022 return 0;
1023 else
1024 return SYMBOL_VALUE_ADDRESS (sym);
1025 }
1026
1027 /* Given a return value in `regbuf' with a type `valtype',
1028 extract and copy its value into `valbuf'. */
1029
1030 void
1031 d30v_extract_return_value (struct type *valtype, char regbuf[REGISTER_BYTES],
1032 char *valbuf)
1033 {
1034 memcpy (valbuf, regbuf + REGISTER_BYTE (2), TYPE_LENGTH (valtype));
1035 }
1036
1037 /* The following code implements access to, and display of, the D30V's
1038 instruction trace buffer. The buffer consists of 64K or more
1039 4-byte words of data, of which each words includes an 8-bit count,
1040 an 8-bit segment number, and a 16-bit instruction address.
1041
1042 In theory, the trace buffer is continuously capturing instruction
1043 data that the CPU presents on its "debug bus", but in practice, the
1044 ROMified GDB stub only enables tracing when it continues or steps
1045 the program, and stops tracing when the program stops; so it
1046 actually works for GDB to read the buffer counter out of memory and
1047 then read each trace word. The counter records where the tracing
1048 stops, but there is no record of where it started, so we remember
1049 the PC when we resumed and then search backwards in the trace
1050 buffer for a word that includes that address. This is not perfect,
1051 because you will miss trace data if the resumption PC is the target
1052 of a branch. (The value of the buffer counter is semi-random, any
1053 trace data from a previous program stop is gone.) */
1054
1055 /* The address of the last word recorded in the trace buffer. */
1056
1057 #define DBBC_ADDR (0xd80000)
1058
1059 /* The base of the trace buffer, at least for the "Board_0". */
1060
1061 #define TRACE_BUFFER_BASE (0xf40000)
1062
1063 static void trace_command (char *, int);
1064
1065 static void untrace_command (char *, int);
1066
1067 static void trace_info (char *, int);
1068
1069 static void tdisassemble_command (char *, int);
1070
1071 static void display_trace (int, int);
1072
1073 /* True when instruction traces are being collected. */
1074
1075 static int tracing;
1076
1077 /* Remembered PC. */
1078
1079 static CORE_ADDR last_pc;
1080
1081 /* True when trace output should be displayed whenever program stops. */
1082
1083 static int trace_display;
1084
1085 /* True when trace listing should include source lines. */
1086
1087 static int default_trace_show_source = 1;
1088
1089 struct trace_buffer
1090 {
1091 int size;
1092 short *counts;
1093 CORE_ADDR *addrs;
1094 }
1095 trace_data;
1096
1097 static void
1098 trace_command (char *args, int from_tty)
1099 {
1100 /* Clear the host-side trace buffer, allocating space if needed. */
1101 trace_data.size = 0;
1102 if (trace_data.counts == NULL)
1103 trace_data.counts = (short *) xmalloc (65536 * sizeof (short));
1104 if (trace_data.addrs == NULL)
1105 trace_data.addrs = (CORE_ADDR *) xmalloc (65536 * sizeof (CORE_ADDR));
1106
1107 tracing = 1;
1108
1109 printf_filtered ("Tracing is now on.\n");
1110 }
1111
1112 static void
1113 untrace_command (char *args, int from_tty)
1114 {
1115 tracing = 0;
1116
1117 printf_filtered ("Tracing is now off.\n");
1118 }
1119
1120 static void
1121 trace_info (char *args, int from_tty)
1122 {
1123 int i;
1124
1125 if (trace_data.size)
1126 {
1127 printf_filtered ("%d entries in trace buffer:\n", trace_data.size);
1128
1129 for (i = 0; i < trace_data.size; ++i)
1130 {
1131 printf_filtered ("%d: %d instruction%s at 0x%s\n",
1132 i, trace_data.counts[i],
1133 (trace_data.counts[i] == 1 ? "" : "s"),
1134 paddr_nz (trace_data.addrs[i]));
1135 }
1136 }
1137 else
1138 printf_filtered ("No entries in trace buffer.\n");
1139
1140 printf_filtered ("Tracing is currently %s.\n", (tracing ? "on" : "off"));
1141 }
1142
1143 /* Print the instruction at address MEMADDR in debugged memory,
1144 on STREAM. Returns length of the instruction, in bytes. */
1145
1146 static int
1147 print_insn (CORE_ADDR memaddr, struct ui_file *stream)
1148 {
1149 /* If there's no disassembler, something is very wrong. */
1150 if (tm_print_insn == NULL)
1151 internal_error (__FILE__, __LINE__,
1152 "print_insn: no disassembler");
1153
1154 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1155 tm_print_insn_info.endian = BFD_ENDIAN_BIG;
1156 else
1157 tm_print_insn_info.endian = BFD_ENDIAN_LITTLE;
1158 return (*tm_print_insn) (memaddr, &tm_print_insn_info);
1159 }
1160
1161 void
1162 d30v_eva_prepare_to_trace (void)
1163 {
1164 if (!tracing)
1165 return;
1166
1167 last_pc = read_register (PC_REGNUM);
1168 }
1169
1170 /* Collect trace data from the target board and format it into a form
1171 more useful for display. */
1172
1173 void
1174 d30v_eva_get_trace_data (void)
1175 {
1176 int count, i, j, oldsize;
1177 int trace_addr, trace_seg, trace_cnt, next_cnt;
1178 unsigned int last_trace, trace_word, next_word;
1179 unsigned int *tmpspace;
1180
1181 if (!tracing)
1182 return;
1183
1184 tmpspace = xmalloc (65536 * sizeof (unsigned int));
1185
1186 last_trace = read_memory_unsigned_integer (DBBC_ADDR, 2) << 2;
1187
1188 /* Collect buffer contents from the target, stopping when we reach
1189 the word recorded when execution resumed. */
1190
1191 count = 0;
1192 while (last_trace > 0)
1193 {
1194 QUIT;
1195 trace_word =
1196 read_memory_unsigned_integer (TRACE_BUFFER_BASE + last_trace, 4);
1197 trace_addr = trace_word & 0xffff;
1198 last_trace -= 4;
1199 /* Ignore an apparently nonsensical entry. */
1200 if (trace_addr == 0xffd5)
1201 continue;
1202 tmpspace[count++] = trace_word;
1203 if (trace_addr == last_pc)
1204 break;
1205 if (count > 65535)
1206 break;
1207 }
1208
1209 /* Move the data to the host-side trace buffer, adjusting counts to
1210 include the last instruction executed and transforming the address
1211 into something that GDB likes. */
1212
1213 for (i = 0; i < count; ++i)
1214 {
1215 trace_word = tmpspace[i];
1216 next_word = ((i == 0) ? 0 : tmpspace[i - 1]);
1217 trace_addr = trace_word & 0xffff;
1218 next_cnt = (next_word >> 24) & 0xff;
1219 j = trace_data.size + count - i - 1;
1220 trace_data.addrs[j] = (trace_addr << 2) + 0x1000000;
1221 trace_data.counts[j] = next_cnt + 1;
1222 }
1223
1224 oldsize = trace_data.size;
1225 trace_data.size += count;
1226
1227 xfree (tmpspace);
1228
1229 if (trace_display)
1230 display_trace (oldsize, trace_data.size);
1231 }
1232
1233 static void
1234 tdisassemble_command (char *arg, int from_tty)
1235 {
1236 int i, count;
1237 CORE_ADDR low, high;
1238 char *space_index;
1239
1240 if (!arg)
1241 {
1242 low = 0;
1243 high = trace_data.size;
1244 }
1245 else if (!(space_index = (char *) strchr (arg, ' ')))
1246 {
1247 low = parse_and_eval_address (arg);
1248 high = low + 5;
1249 }
1250 else
1251 {
1252 /* Two arguments. */
1253 *space_index = '\0';
1254 low = parse_and_eval_address (arg);
1255 high = parse_and_eval_address (space_index + 1);
1256 if (high < low)
1257 high = low;
1258 }
1259
1260 printf_filtered ("Dump of trace from %s to %s:\n",
1261 paddr_u (low),
1262 paddr_u (high));
1263
1264 display_trace (low, high);
1265
1266 printf_filtered ("End of trace dump.\n");
1267 gdb_flush (gdb_stdout);
1268 }
1269
1270 static void
1271 display_trace (int low, int high)
1272 {
1273 int i, count, trace_show_source, first, suppress;
1274 CORE_ADDR next_address;
1275
1276 trace_show_source = default_trace_show_source;
1277 if (!have_full_symbols () && !have_partial_symbols ())
1278 {
1279 trace_show_source = 0;
1280 printf_filtered ("No symbol table is loaded. Use the \"file\" command.\n");
1281 printf_filtered ("Trace will not display any source.\n");
1282 }
1283
1284 first = 1;
1285 suppress = 0;
1286 for (i = low; i < high; ++i)
1287 {
1288 next_address = trace_data.addrs[i];
1289 count = trace_data.counts[i];
1290 while (count-- > 0)
1291 {
1292 QUIT;
1293 if (trace_show_source)
1294 {
1295 struct symtab_and_line sal, sal_prev;
1296
1297 sal_prev = find_pc_line (next_address - 4, 0);
1298 sal = find_pc_line (next_address, 0);
1299
1300 if (sal.symtab)
1301 {
1302 if (first || sal.line != sal_prev.line)
1303 print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
1304 suppress = 0;
1305 }
1306 else
1307 {
1308 if (!suppress)
1309 /* FIXME-32x64--assumes sal.pc fits in long. */
1310 printf_filtered ("No source file for address %s.\n",
1311 local_hex_string ((unsigned long) sal.pc));
1312 suppress = 1;
1313 }
1314 }
1315 first = 0;
1316 print_address (next_address, gdb_stdout);
1317 printf_filtered (":");
1318 printf_filtered ("\t");
1319 wrap_here (" ");
1320 next_address = next_address + print_insn (next_address, gdb_stdout);
1321 printf_filtered ("\n");
1322 gdb_flush (gdb_stdout);
1323 }
1324 }
1325 }
1326
1327 extern void (*target_resume_hook) (void);
1328 extern void (*target_wait_loop_hook) (void);
1329
1330 void
1331 _initialize_d30v_tdep (void)
1332 {
1333 tm_print_insn = print_insn_d30v;
1334
1335 target_resume_hook = d30v_eva_prepare_to_trace;
1336 target_wait_loop_hook = d30v_eva_get_trace_data;
1337
1338 add_info ("flags", print_flags_command, "Print d30v flags.");
1339
1340 add_com ("trace", class_support, trace_command,
1341 "Enable tracing of instruction execution.");
1342
1343 add_com ("untrace", class_support, untrace_command,
1344 "Disable tracing of instruction execution.");
1345
1346 add_com ("tdisassemble", class_vars, tdisassemble_command,
1347 "Disassemble the trace buffer.\n\
1348 Two optional arguments specify a range of trace buffer entries\n\
1349 as reported by info trace (NOT addresses!).");
1350
1351 add_info ("trace", trace_info,
1352 "Display info about the trace data buffer.");
1353
1354 add_show_from_set (add_set_cmd ("tracedisplay", no_class,
1355 var_integer, (char *) &trace_display,
1356 "Set automatic display of trace.\n", &setlist),
1357 &showlist);
1358 add_show_from_set (add_set_cmd ("tracesource", no_class,
1359 var_integer, (char *) &default_trace_show_source,
1360 "Set display of source code with trace.\n", &setlist),
1361 &showlist);
1362
1363 }
This page took 0.058193 seconds and 4 git commands to generate.