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