* source.c (openp): Squelch warning about "filename".
[deliverable/binutils-gdb.git] / gdb / h8500-tdep.c
1 /* Target-dependent code for Hitachi H8/500, for GDB.
2
3 Copyright 1993, 1994, 1995, 1998, 2000, 2001, 2002 Free Software
4 Foundation, Inc.
5
6 This file is part of GDB.
7
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.
12
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.
17
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. */
22
23 /*
24 Contributed by Steve Chamberlain
25 sac@cygnus.com
26 */
27
28 #include "defs.h"
29 #include "frame.h"
30 #include "symtab.h"
31 #include "gdbtypes.h"
32 #include "gdbcmd.h"
33 #include "value.h"
34 #include "dis-asm.h"
35 #include "gdbcore.h"
36 #include "regcache.h"
37
38 #define UNSIGNED_SHORT(X) ((X) & 0xffff)
39
40 static int code_size = 2;
41
42 static int data_size = 2;
43
44 /* Shape of an H8/500 frame :
45
46 arg-n
47 ..
48 arg-2
49 arg-1
50 return address <2 or 4 bytes>
51 old fp <2 bytes>
52 auto-n
53 ..
54 auto-1
55 saved registers
56
57 */
58
59 /* an easy to debug H8 stack frame looks like:
60 0x6df6 push r6
61 0x0d76 mov.w r7,r6
62 0x6dfn push reg
63 0x7905 nnnn mov.w #n,r5 or 0x1b87 subs #2,sp
64 0x1957 sub.w r5,sp
65
66 */
67
68 #define IS_PUSH(x) (((x) & 0xff00)==0x6d00)
69 #define IS_LINK_8(x) ((x) == 0x17)
70 #define IS_LINK_16(x) ((x) == 0x1f)
71 #define IS_MOVE_FP(x) ((x) == 0x0d76)
72 #define IS_MOV_SP_FP(x) ((x) == 0x0d76)
73 #define IS_SUB2_SP(x) ((x) == 0x1b87)
74 #define IS_MOVK_R5(x) ((x) == 0x7905)
75 #define IS_SUB_R5SP(x) ((x) == 0x1957)
76
77 #define LINK_8 0x17
78 #define LINK_16 0x1f
79
80 int minimum_mode = 1;
81
82 CORE_ADDR
83 h8500_skip_prologue (CORE_ADDR start_pc)
84 {
85 short int w;
86
87 w = read_memory_integer (start_pc, 1);
88 if (w == LINK_8)
89 {
90 start_pc += 2;
91 w = read_memory_integer (start_pc, 1);
92 }
93
94 if (w == LINK_16)
95 {
96 start_pc += 3;
97 w = read_memory_integer (start_pc, 2);
98 }
99
100 return start_pc;
101 }
102
103 CORE_ADDR
104 h8500_addr_bits_remove (CORE_ADDR addr)
105 {
106 return ((addr) & 0xffffff);
107 }
108
109 /* Given a GDB frame, determine the address of the calling function's
110 frame. This will be used to create a new GDB frame struct, and
111 then INIT_EXTRA_FRAME_INFO and DEPRECATED_INIT_FRAME_PC will be
112 called for the new frame.
113
114 For us, the frame address is its stack pointer value, so we look up
115 the function prologue to determine the caller's sp value, and return it. */
116
117 CORE_ADDR
118 h8500_frame_chain (struct frame_info *thisframe)
119 {
120 if (!inside_entry_file (thisframe->pc))
121 return (read_memory_integer (get_frame_base (thisframe), PTR_SIZE));
122 else
123 return 0;
124 }
125
126 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
127 is not the address of a valid instruction, the address of the next
128 instruction beyond ADDR otherwise. *PWORD1 receives the first word
129 of the instruction. */
130
131 CORE_ADDR
132 NEXT_PROLOGUE_INSN (CORE_ADDR addr, CORE_ADDR lim, char *pword1)
133 {
134 if (addr < lim + 8)
135 {
136 read_memory (addr, pword1, 1);
137 read_memory (addr, pword1 + 1, 1);
138 return 1;
139 }
140 return 0;
141 }
142
143 /* Examine the prologue of a function. `ip' points to the first
144 instruction. `limit' is the limit of the prologue (e.g. the addr
145 of the first linenumber, or perhaps the program counter if we're
146 stepping through). `frame_sp' is the stack pointer value in use in
147 this frame. `fsr' is a pointer to a frame_saved_regs structure
148 into which we put info about the registers saved by this frame.
149 `fi' is a struct frame_info pointer; we fill in various fields in
150 it to reflect the offsets of the arg pointer and the locals
151 pointer. */
152
153 /* Return the saved PC from this frame. */
154
155 CORE_ADDR
156 frame_saved_pc (struct frame_info *frame)
157 {
158 return read_memory_integer (get_frame_base (frame) + 2, PTR_SIZE);
159 }
160
161 void
162 h8500_pop_frame (void)
163 {
164 unsigned regnum;
165 struct frame_saved_regs fsr;
166 struct frame_info *frame = get_current_frame ();
167
168 deprecated_get_frame_saved_regs (frame, &fsr);
169
170 for (regnum = 0; regnum < 8; regnum++)
171 {
172 if (fsr.regs[regnum])
173 write_register (regnum, read_memory_short (fsr.regs[regnum]));
174
175 flush_cached_frames ();
176 }
177 }
178
179 static void
180 h8500_print_register_hook (int regno)
181 {
182 if (regno == CCR_REGNUM)
183 {
184 /* CCR register */
185
186 int C, Z, N, V;
187 unsigned char b[2];
188 unsigned char l;
189
190 frame_register_read (deprecated_selected_frame, regno, b);
191 l = b[1];
192 printf_unfiltered ("\t");
193 printf_unfiltered ("I-%d - ", (l & 0x80) != 0);
194 N = (l & 0x8) != 0;
195 Z = (l & 0x4) != 0;
196 V = (l & 0x2) != 0;
197 C = (l & 0x1) != 0;
198 printf_unfiltered ("N-%d ", N);
199 printf_unfiltered ("Z-%d ", Z);
200 printf_unfiltered ("V-%d ", V);
201 printf_unfiltered ("C-%d ", C);
202 if ((C | Z) == 0)
203 printf_unfiltered ("u> ");
204 if ((C | Z) == 1)
205 printf_unfiltered ("u<= ");
206 if ((C == 0))
207 printf_unfiltered ("u>= ");
208 if (C == 1)
209 printf_unfiltered ("u< ");
210 if (Z == 0)
211 printf_unfiltered ("!= ");
212 if (Z == 1)
213 printf_unfiltered ("== ");
214 if ((N ^ V) == 0)
215 printf_unfiltered (">= ");
216 if ((N ^ V) == 1)
217 printf_unfiltered ("< ");
218 if ((Z | (N ^ V)) == 0)
219 printf_unfiltered ("> ");
220 if ((Z | (N ^ V)) == 1)
221 printf_unfiltered ("<= ");
222 }
223 }
224
225 static void
226 h8500_print_registers_info (struct gdbarch *gdbarch,
227 struct ui_file *file,
228 struct frame_info *frame,
229 int regnum, int print_all)
230 {
231 int i;
232 const int numregs = NUM_REGS + NUM_PSEUDO_REGS;
233 char *raw_buffer = alloca (MAX_REGISTER_RAW_SIZE);
234 char *virtual_buffer = alloca (MAX_REGISTER_VIRTUAL_SIZE);
235
236 for (i = 0; i < numregs; i++)
237 {
238 /* Decide between printing all regs, non-float / vector regs, or
239 specific reg. */
240 if (regnum == -1)
241 {
242 if (!print_all)
243 {
244 if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT)
245 continue;
246 if (TYPE_VECTOR (REGISTER_VIRTUAL_TYPE (i)))
247 continue;
248 }
249 }
250 else
251 {
252 if (i != regnum)
253 continue;
254 }
255
256 /* If the register name is empty, it is undefined for this
257 processor, so don't display anything. */
258 if (REGISTER_NAME (i) == NULL || *(REGISTER_NAME (i)) == '\0')
259 continue;
260
261 fputs_filtered (REGISTER_NAME (i), file);
262 print_spaces_filtered (15 - strlen (REGISTER_NAME (i)), file);
263
264 /* Get the data in raw format. */
265 if (! frame_register_read (frame, i, raw_buffer))
266 {
267 fprintf_filtered (file, "*value not available*\n");
268 continue;
269 }
270
271 /* FIXME: cagney/2002-08-03: This code shouldn't be necessary.
272 The function frame_register_read() should have returned the
273 pre-cooked register so no conversion is necessary. */
274 /* Convert raw data to virtual format if necessary. */
275 if (REGISTER_CONVERTIBLE (i))
276 {
277 REGISTER_CONVERT_TO_VIRTUAL (i, REGISTER_VIRTUAL_TYPE (i),
278 raw_buffer, virtual_buffer);
279 }
280 else
281 {
282 memcpy (virtual_buffer, raw_buffer,
283 REGISTER_VIRTUAL_SIZE (i));
284 }
285
286 /* If virtual format is floating, print it that way, and in raw
287 hex. */
288 if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT)
289 {
290 int j;
291
292 val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, 0,
293 file, 0, 1, 0, Val_pretty_default);
294
295 fprintf_filtered (file, "\t(raw 0x");
296 for (j = 0; j < REGISTER_RAW_SIZE (i); j++)
297 {
298 int idx;
299 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
300 idx = j;
301 else
302 idx = REGISTER_RAW_SIZE (i) - 1 - j;
303 fprintf_filtered (file, "%02x", (unsigned char) raw_buffer[idx]);
304 }
305 fprintf_filtered (file, ")");
306 }
307 else
308 {
309 /* Print the register in hex. */
310 val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, 0,
311 file, 'x', 1, 0, Val_pretty_default);
312 /* If not a vector register, print it also according to its
313 natural format. */
314 if (TYPE_VECTOR (REGISTER_VIRTUAL_TYPE (i)) == 0)
315 {
316 fprintf_filtered (file, "\t");
317 val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0, 0,
318 file, 0, 1, 0, Val_pretty_default);
319 }
320 }
321
322 /* Some h8500 specific info. */
323 h8500_print_register_hook (i);
324
325 fprintf_filtered (file, "\n");
326 }
327 }
328
329 void
330 h8500_do_registers_info (int regnum, int all)
331 {
332 h8500_print_registers_info (current_gdbarch, gdb_stdout, deprecated_selected_frame,
333 regnum, all);
334 }
335
336 int
337 h8500_register_size (int regno)
338 {
339 switch (regno)
340 {
341 case SEG_C_REGNUM:
342 case SEG_D_REGNUM:
343 case SEG_E_REGNUM:
344 case SEG_T_REGNUM:
345 return 1;
346 case R0_REGNUM:
347 case R1_REGNUM:
348 case R2_REGNUM:
349 case R3_REGNUM:
350 case R4_REGNUM:
351 case R5_REGNUM:
352 case R6_REGNUM:
353 case R7_REGNUM:
354 case CCR_REGNUM:
355 return 2;
356
357 case PR0_REGNUM:
358 case PR1_REGNUM:
359 case PR2_REGNUM:
360 case PR3_REGNUM:
361 case PR4_REGNUM:
362 case PR5_REGNUM:
363 case PR6_REGNUM:
364 case PR7_REGNUM:
365 case PC_REGNUM:
366 return 4;
367 default:
368 internal_error (__FILE__, __LINE__, "failed internal consistency check");
369 }
370 }
371
372 struct type *
373 h8500_register_virtual_type (int regno)
374 {
375 switch (regno)
376 {
377 case SEG_C_REGNUM:
378 case SEG_E_REGNUM:
379 case SEG_D_REGNUM:
380 case SEG_T_REGNUM:
381 return builtin_type_unsigned_char;
382 case R0_REGNUM:
383 case R1_REGNUM:
384 case R2_REGNUM:
385 case R3_REGNUM:
386 case R4_REGNUM:
387 case R5_REGNUM:
388 case R6_REGNUM:
389 case R7_REGNUM:
390 case CCR_REGNUM:
391 return builtin_type_unsigned_short;
392 case PR0_REGNUM:
393 case PR1_REGNUM:
394 case PR2_REGNUM:
395 case PR3_REGNUM:
396 case PR4_REGNUM:
397 case PR5_REGNUM:
398 case PR6_REGNUM:
399 case PR7_REGNUM:
400 case PC_REGNUM:
401 return builtin_type_unsigned_long;
402 default:
403 internal_error (__FILE__, __LINE__, "failed internal consistency check");
404 }
405 }
406
407 /* Put here the code to store, into a struct frame_saved_regs,
408 the addresses of the saved registers of frame described by FRAME_INFO.
409 This includes special registers such as pc and fp saved in special
410 ways in the stack frame. sp is even more special:
411 the address we return for it IS the sp for the next frame. */
412
413 void
414 frame_find_saved_regs (struct frame_info *frame_info,
415 struct frame_saved_regs *frame_saved_regs)
416 {
417 register int regnum;
418 register int regmask;
419 register CORE_ADDR next_addr;
420 register CORE_ADDR pc;
421 unsigned char thebyte;
422
423 memset (frame_saved_regs, '\0', sizeof *frame_saved_regs);
424
425 if ((frame_info)->pc >= (frame_info)->frame - CALL_DUMMY_LENGTH - FP_REGNUM * 4 - 4
426 && (frame_info)->pc <= (frame_info)->frame)
427 {
428 next_addr = (frame_info)->frame;
429 pc = (frame_info)->frame - CALL_DUMMY_LENGTH - FP_REGNUM * 4 - 4;
430 }
431 else
432 {
433 pc = get_pc_function_start ((frame_info)->pc);
434 /* Verify we have a link a6 instruction next;
435 if not we lose. If we win, find the address above the saved
436 regs using the amount of storage from the link instruction.
437 */
438
439 thebyte = read_memory_integer (pc, 1);
440 if (0x1f == thebyte)
441 next_addr = (frame_info)->frame + read_memory_integer (pc += 1, 2), pc += 2;
442 else if (0x17 == thebyte)
443 next_addr = (frame_info)->frame + read_memory_integer (pc += 1, 1), pc += 1;
444 else
445 goto lose;
446 #if 0
447 /* FIXME steve */
448 /* If have an add:g.waddal #-n, sp next, adjust next_addr. */
449 if ((0x0c0177777 & read_memory_integer (pc, 2)) == 0157774)
450 next_addr += read_memory_integer (pc += 2, 4), pc += 4;
451 #endif
452 }
453
454 thebyte = read_memory_integer (pc, 1);
455 if (thebyte == 0x12)
456 {
457 /* Got stm */
458 pc++;
459 regmask = read_memory_integer (pc, 1);
460 pc++;
461 for (regnum = 0; regnum < 8; regnum++, regmask >>= 1)
462 {
463 if (regmask & 1)
464 {
465 (frame_saved_regs)->regs[regnum] = (next_addr += 2) - 2;
466 }
467 }
468 thebyte = read_memory_integer (pc, 1);
469 }
470 /* Maybe got a load of pushes */
471 while (thebyte == 0xbf)
472 {
473 pc++;
474 regnum = read_memory_integer (pc, 1) & 0x7;
475 pc++;
476 (frame_saved_regs)->regs[regnum] = (next_addr += 2) - 2;
477 thebyte = read_memory_integer (pc, 1);
478 }
479
480 lose:;
481
482 /* Remember the address of the frame pointer */
483 (frame_saved_regs)->regs[FP_REGNUM] = (frame_info)->frame;
484
485 /* This is where the old sp is hidden */
486 (frame_saved_regs)->regs[SP_REGNUM] = (frame_info)->frame;
487
488 /* And the PC - remember the pushed FP is always two bytes long */
489 (frame_saved_regs)->regs[PC_REGNUM] = (frame_info)->frame + 2;
490 }
491
492 CORE_ADDR
493 saved_pc_after_call (void)
494 {
495 int x;
496 int a = read_register (SP_REGNUM);
497
498 x = read_memory_integer (a, code_size);
499 if (code_size == 2)
500 {
501 /* Stick current code segement onto top */
502 x &= 0xffff;
503 x |= read_register (SEG_C_REGNUM) << 16;
504 }
505 x &= 0xffffff;
506 return x;
507 }
508
509 void
510 h8500_set_pointer_size (int newsize)
511 {
512 static int oldsize = 0;
513
514 if (oldsize != newsize)
515 {
516 printf_unfiltered ("pointer size set to %d bits\n", newsize);
517 oldsize = newsize;
518 if (newsize == 32)
519 {
520 minimum_mode = 0;
521 }
522 else
523 {
524 minimum_mode = 1;
525 }
526 _initialize_gdbtypes ();
527 }
528 }
529
530 static void
531 big_command (char *arg, int from_tty)
532 {
533 h8500_set_pointer_size (32);
534 code_size = 4;
535 data_size = 4;
536 }
537
538 static void
539 medium_command (char *arg, int from_tty)
540 {
541 h8500_set_pointer_size (32);
542 code_size = 4;
543 data_size = 2;
544 }
545
546 static void
547 compact_command (char *arg, int from_tty)
548 {
549 h8500_set_pointer_size (32);
550 code_size = 2;
551 data_size = 4;
552 }
553
554 static void
555 small_command (char *arg, int from_tty)
556 {
557 h8500_set_pointer_size (16);
558 code_size = 2;
559 data_size = 2;
560 }
561
562 static struct cmd_list_element *setmemorylist;
563
564 static void
565 set_memory (char *args, int from_tty)
566 {
567 printf_unfiltered ("\"set memory\" must be followed by the name of a memory subcommand.\n");
568 help_list (setmemorylist, "set memory ", -1, gdb_stdout);
569 }
570
571 /* See if variable name is ppc or pr[0-7] */
572
573 int
574 h8500_is_trapped_internalvar (char *name)
575 {
576 if (name[0] != 'p')
577 return 0;
578
579 if (strcmp (name + 1, "pc") == 0)
580 return 1;
581
582 if (name[1] == 'r'
583 && name[2] >= '0'
584 && name[2] <= '7'
585 && name[3] == '\000')
586 return 1;
587 else
588 return 0;
589 }
590
591 struct value *
592 h8500_value_of_trapped_internalvar (struct internalvar *var)
593 {
594 LONGEST regval;
595 unsigned char regbuf[4];
596 int page_regnum, regnum;
597
598 regnum = var->name[2] == 'c' ? PC_REGNUM : var->name[2] - '0';
599
600 switch (var->name[2])
601 {
602 case 'c':
603 page_regnum = SEG_C_REGNUM;
604 break;
605 case '0':
606 case '1':
607 case '2':
608 case '3':
609 page_regnum = SEG_D_REGNUM;
610 break;
611 case '4':
612 case '5':
613 page_regnum = SEG_E_REGNUM;
614 break;
615 case '6':
616 case '7':
617 page_regnum = SEG_T_REGNUM;
618 break;
619 }
620
621 get_saved_register (regbuf, NULL, NULL, deprecated_selected_frame, page_regnum, NULL);
622 regval = regbuf[0] << 16;
623
624 get_saved_register (regbuf, NULL, NULL, deprecated_selected_frame, regnum, NULL);
625 regval |= regbuf[0] << 8 | regbuf[1]; /* XXX host/target byte order */
626
627 xfree (var->value); /* Free up old value */
628
629 var->value = value_from_longest (builtin_type_unsigned_long, regval);
630 release_value (var->value); /* Unchain new value */
631
632 VALUE_LVAL (var->value) = lval_internalvar;
633 VALUE_INTERNALVAR (var->value) = var;
634 return var->value;
635 }
636
637 void
638 h8500_set_trapped_internalvar (struct internalvar *var, struct value *newval,
639 int bitpos, int bitsize, int offset)
640 {
641 char *page_regnum, *regnum;
642 char expression[100];
643 unsigned new_regval;
644 struct type *type;
645 enum type_code newval_type_code;
646
647 type = check_typedef (VALUE_TYPE (newval));
648 newval_type_code = TYPE_CODE (type);
649
650 if ((newval_type_code != TYPE_CODE_INT
651 && newval_type_code != TYPE_CODE_PTR)
652 || TYPE_LENGTH (type) != sizeof (new_regval))
653 error ("Illegal type (%s) for assignment to $%s\n",
654 TYPE_NAME (VALUE_TYPE (newval)), var->name);
655
656 new_regval = *(long *) VALUE_CONTENTS_RAW (newval);
657
658 regnum = var->name + 1;
659
660 switch (var->name[2])
661 {
662 case 'c':
663 page_regnum = "cp";
664 break;
665 case '0':
666 case '1':
667 case '2':
668 case '3':
669 page_regnum = "dp";
670 break;
671 case '4':
672 case '5':
673 page_regnum = "ep";
674 break;
675 case '6':
676 case '7':
677 page_regnum = "tp";
678 break;
679 }
680
681 sprintf (expression, "$%s=%d", page_regnum, new_regval >> 16);
682 parse_and_eval (expression);
683
684 sprintf (expression, "$%s=%d", regnum, new_regval & 0xffff);
685 parse_and_eval (expression);
686 }
687
688 CORE_ADDR
689 h8500_read_sp (void)
690 {
691 return read_register (PR7_REGNUM);
692 }
693
694 void
695 h8500_write_sp (CORE_ADDR v)
696 {
697 write_register (PR7_REGNUM, v);
698 }
699
700 CORE_ADDR
701 h8500_read_pc (ptid_t ptid)
702 {
703 return read_register (PC_REGNUM);
704 }
705
706 void
707 h8500_write_pc (CORE_ADDR v, ptid_t ptid)
708 {
709 write_register (PC_REGNUM, v);
710 }
711
712 CORE_ADDR
713 h8500_read_fp (void)
714 {
715 return read_register (PR6_REGNUM);
716 }
717
718 void
719 _initialize_h8500_tdep (void)
720 {
721 tm_print_insn = print_insn_h8500;
722
723 add_prefix_cmd ("memory", no_class, set_memory,
724 "set the memory model", &setmemorylist, "set memory ", 0,
725 &setlist);
726
727 add_cmd ("small", class_support, small_command,
728 "Set small memory model. (16 bit code, 16 bit data)", &setmemorylist);
729
730 add_cmd ("big", class_support, big_command,
731 "Set big memory model. (32 bit code, 32 bit data)", &setmemorylist);
732
733 add_cmd ("medium", class_support, medium_command,
734 "Set medium memory model. (32 bit code, 16 bit data)", &setmemorylist);
735
736 add_cmd ("compact", class_support, compact_command,
737 "Set compact memory model. (16 bit code, 32 bit data)", &setmemorylist);
738
739 }
This page took 0.064784 seconds and 4 git commands to generate.