1 /* Target-machine dependent code for Hitachi H8/500, for GDB.
2 Copyright (C) 1993 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 Contributed by Steve Chamberlain
33 #include "../opcodes/h8500-opc.h"
36 #define UNSIGNED_SHORT(X) ((X) & 0xffff)
38 /* Shape of an H8/500 frame :
45 return address <2 or 4 bytes>
55 /* an easy to debug H8 stack frame looks like:
59 0x7905 nnnn mov.w #n,r5 or 0x1b87 subs #2,sp
64 #define IS_PUSH(x) ((x & 0xff00)==0x6d00)
65 #define IS_LINK_8(x) ((x) == 0x17)
66 #define IS_LINK_16(x) ((x) == 0x1f)
67 #define IS_MOVE_FP(x) (x == 0x0d76)
68 #define IS_MOV_SP_FP(x) (x == 0x0d76)
69 #define IS_SUB2_SP(x) (x==0x1b87)
70 #define IS_MOVK_R5(x) (x==0x7905)
71 #define IS_SUB_R5SP(x) (x==0x1957)
77 CORE_ADDR
examine_prologue ();
79 void frame_find_saved_regs ();
81 int regoff
[NUM_REGS
] = {0, 2, 4, 6, 8, 10, 12, 14, /* r0->r7 */
83 20, 21, 22, 23}; /* cp, dp, ep, tp */
86 h8500_skip_prologue (start_pc
)
92 w
= read_memory_integer (start_pc
, 1);
96 w
= read_memory_integer (start_pc
,1);
102 w
= read_memory_integer (start_pc
,2);
109 print_insn (memaddr
, stream
)
113 /* Nothing is bigger than 8 bytes */
115 disassemble_info info
;
116 read_memory (memaddr
, data
, sizeof (data
));
117 GDB_INIT_DISASSEMBLE_INFO(info
, stream
);
118 return print_insn_h8500 (memaddr
, data
, &info
);
121 /* Given a GDB frame, determine the address of the calling function's frame.
122 This will be used to create a new GDB frame struct, and then
123 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
125 For us, the frame address is its stack pointer value, so we look up
126 the function prologue to determine the caller's sp value, and return it. */
129 h8500_frame_chain (thisframe
)
133 if (!inside_entry_file (thisframe
->pc
))
134 return read_memory_integer(thisframe
->frame
, 2)
135 | (read_register(SEG_T_REGNUM
) << 16);
140 /* Put here the code to store, into a struct frame_saved_regs,
141 the addresses of the saved registers of frame described by FRAME_INFO.
142 This includes special registers such as pc and fp saved in special
143 ways in the stack frame. sp is even more special:
144 the address we return for it IS the sp for the next frame.
146 We cache the result of doing this in the frame_cache_obstack, since
147 it is fairly expensive. */
151 frame_find_saved_regs (fi
, fsr
)
152 struct frame_info
*fi
;
153 struct frame_saved_regs
*fsr
;
155 register CORE_ADDR next_addr
;
156 register CORE_ADDR
*saved_regs
;
158 register struct frame_saved_regs
*cache_fsr
;
159 extern struct obstack frame_cache_obstack
;
161 struct symtab_and_line sal
;
166 cache_fsr
= (struct frame_saved_regs
*)
167 obstack_alloc (&frame_cache_obstack
,
168 sizeof (struct frame_saved_regs
));
169 bzero (cache_fsr
, sizeof (struct frame_saved_regs
));
173 /* Find the start and end of the function prologue. If the PC
174 is in the function prologue, we only consider the part that
175 has executed already. */
177 ip
= get_pc_function_start (fi
->pc
);
178 sal
= find_pc_line (ip
, 0);
179 limit
= (sal
.end
&& sal
.end
< fi
->pc
) ? sal
.end
: fi
->pc
;
181 /* This will fill in fields in *fi as well as in cache_fsr. */
182 examine_prologue (ip
, limit
, fi
->frame
, cache_fsr
, fi
);
191 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
192 is not the address of a valid instruction, the address of the next
193 instruction beyond ADDR otherwise. *PWORD1 receives the first word
194 of the instruction.*/
197 NEXT_PROLOGUE_INSN (addr
, lim
, pword1
)
204 read_memory (addr
, pword1
, 1);
205 read_memory (addr
, pword1
+ 1, 1);
211 /* Examine the prologue of a function. `ip' points to the first instruction.
212 `limit' is the limit of the prologue (e.g. the addr of the first
213 linenumber, or perhaps the program counter if we're stepping through).
214 `frame_sp' is the stack pointer value in use in this frame.
215 `fsr' is a pointer to a frame_saved_regs structure into which we put
216 info about the registers saved by this frame.
217 `fi' is a struct frame_info pointer; we fill in various fields in it
218 to reflect the offsets of the arg pointer and the locals pointer. */
221 examine_prologue (ip
, limit
, after_prolog_fp
, fsr
, fi
)
222 register CORE_ADDR ip
;
223 register CORE_ADDR limit
;
224 FRAME_ADDR after_prolog_fp
;
225 struct frame_saved_regs
*fsr
;
226 struct frame_info
*fi
;
228 register CORE_ADDR next_ip
;
234 register struct pic_prologue_code
*pcode
;
237 unsigned int reg_save_depth
= 2; /* Number of things pushed onto
238 stack, starts at 2, 'cause the
239 PC is already there */
241 unsigned int auto_depth
= 0; /* Number of bytes of autos */
243 char in_frame
[8]; /* One for each reg */
245 memset (in_frame
, 1, 8);
246 for (r
= 0; r
< 8; r
++)
250 if (after_prolog_fp
== 0)
252 after_prolog_fp
= read_register (SP_REGNUM
);
254 if (ip
== 0 || ip
& ~0xffffff)
257 ok
= NEXT_PROLOGUE_INSN (ip
, limit
, &insn
[0]);
259 /* Skip over any fp push instructions */
260 fsr
->regs
[6] = after_prolog_fp
;
262 if (ok
&& IS_LINK_8 (insn
[0]))
266 in_frame
[6] = reg_save_depth
;
270 next_ip
= NEXT_PROLOGUE_INSN (ip
, limit
, &insn_word
);
272 /* Is this a move into the fp */
273 if (next_ip
&& IS_MOV_SP_FP (insn_word
))
276 next_ip
= NEXT_PROLOGUE_INSN (ip
, limit
, &insn_word
);
280 /* Skip over any stack adjustment, happens either with a number of
281 sub#2,sp or a mov #x,r5 sub r5,sp */
283 if (next_ip
&& IS_SUB2_SP (insn_word
))
285 while (next_ip
&& IS_SUB2_SP (insn_word
))
289 next_ip
= NEXT_PROLOGUE_INSN (ip
, limit
, &insn_word
);
294 if (next_ip
&& IS_MOVK_R5 (insn_word
))
297 next_ip
= NEXT_PROLOGUE_INSN (ip
, limit
, &insn_word
);
298 auto_depth
+= insn_word
;
300 next_ip
= NEXT_PROLOGUE_INSN (next_ip
, limit
, &insn_word
);
301 auto_depth
+= insn_word
;
305 /* Work out which regs are stored where */
306 while (next_ip
&& IS_PUSH (insn_word
))
309 next_ip
= NEXT_PROLOGUE_INSN (ip
, limit
, &insn_word
);
310 fsr
->regs
[r
] = after_prolog_fp
+ auto_depth
;
314 /* The args are always reffed based from the stack pointer */
315 fi
->args_pointer
= after_prolog_fp
;
316 /* Locals are always reffed based from the fp */
317 fi
->locals_pointer
= after_prolog_fp
;
318 /* The PC is at a known place */
319 fi
->from_pc
= read_memory_short (after_prolog_fp
+ 2);
321 /* Rememeber any others too */
322 in_frame
[PC_REGNUM
] = 0;
325 /* We keep the old FP in the SP spot */
326 fsr
->regs
[SP_REGNUM
] = (read_memory_short (fsr
->regs
[6]));
328 fsr
->regs
[SP_REGNUM
] = after_prolog_fp
+ auto_depth
;
334 /* Return the saved PC from this frame. */
337 frame_saved_pc (frame
)
340 return read_memory_integer ((frame
)->frame
+ 2, PTR_SIZE
);
344 frame_locals_address (fi
)
345 struct frame_info
*fi
;
350 /* Return the address of the argument block for the frame
351 described by FI. Returns 0 if the address is unknown. */
354 frame_args_address (fi
)
355 struct frame_info
*fi
;
364 struct frame_saved_regs fsr
;
365 struct frame_info
*fi
;
367 FRAME frame
= get_current_frame ();
369 fi
= get_frame_info (frame
);
370 get_frame_saved_regs (fi
, &fsr
);
372 for (regnum
= 0; regnum
< 8; regnum
++)
374 if (fsr
.regs
[regnum
])
376 write_register (regnum
, read_memory_short (fsr
.regs
[regnum
]));
379 flush_cached_frames ();
380 set_current_frame (create_new_frame (read_register (FP_REGNUM
),
388 print_register_hook (regno
)
390 if (regno
== CCR_REGNUM
)
398 read_relative_register_raw_bytes (regno
, b
);
401 printf ("I-%d - ", (l
& 0x80) != 0);
426 if ((Z
| (N
^ V
)) == 0)
428 if ((Z
| (N
^ V
)) == 1)
434 h8500_register_size (regno
)
437 if (regno
<= PC_REGNUM
)
444 h8500_register_virtual_type (regno
)
453 return builtin_type_unsigned_char
;
464 return builtin_type_unsigned_short
;
470 /* Put here the code to store, into a struct frame_saved_regs,
471 the addresses of the saved registers of frame described by FRAME_INFO.
472 This includes special registers such as pc and fp saved in special
473 ways in the stack frame. sp is even more special:
474 the address we return for it IS the sp for the next frame. */
477 frame_find_saved_regs (frame_info
, frame_saved_regs
)
478 struct frame_info
*frame_info
;
479 struct frame_saved_regs
*frame_saved_regs
;
483 register int regmask
;
484 register CORE_ADDR next_addr
;
485 register CORE_ADDR pc
;
486 unsigned char thebyte
;
488 bzero (frame_saved_regs
, sizeof *frame_saved_regs
);
490 if ((frame_info
)->pc
>= (frame_info
)->frame
- CALL_DUMMY_LENGTH
- FP_REGNUM
* 4 - 4
491 && (frame_info
)->pc
<= (frame_info
)->frame
)
493 next_addr
= (frame_info
)->frame
;
494 pc
= (frame_info
)->frame
- CALL_DUMMY_LENGTH
- FP_REGNUM
* 4 - 4;
498 pc
= get_pc_function_start ((frame_info
)->pc
);
499 /* Verify we have a link a6 instruction next;
500 if not we lose. If we win, find the address above the saved
501 regs using the amount of storage from the link instruction.
504 thebyte
= read_memory_integer(pc
, 1);
506 next_addr
= (frame_info
)->frame
+ read_memory_integer (pc
+= 1, 2), pc
+= 2;
507 else if (0x17 == thebyte
)
508 next_addr
= (frame_info
)->frame
+ read_memory_integer (pc
+= 1, 1), pc
+= 1;
513 /* If have an add:g.waddal #-n, sp next, adjust next_addr. */
514 if ((0x0c0177777 & read_memory_integer (pc
, 2)) == 0157774)
515 next_addr
+= read_memory_integer (pc
+= 2, 4), pc
+= 4;
519 thebyte
= read_memory_integer(pc
, 1);
520 if (thebyte
== 0x12) {
523 regmask
= read_memory_integer(pc
,1);
525 for (regnum
= 0; regnum
< 8; regnum
++, regmask
>>=1)
529 (frame_saved_regs
)->regs
[regnum
] = (next_addr
+= 2) - 2;
532 thebyte
= read_memory_integer(pc
, 1);
534 /* Maybe got a load of pushes */
535 while (thebyte
== 0xbf) {
537 regnum
= read_memory_integer(pc
,1) & 0x7;
539 (frame_saved_regs
)->regs
[regnum
] = (next_addr
+= 2) - 2;
540 thebyte
= read_memory_integer(pc
, 1);
545 /* Remember the address of the frame pointer */
546 (frame_saved_regs
)->regs
[FP_REGNUM
] = (frame_info
)->frame
;
548 /* This is where the old sp is hidden */
549 (frame_saved_regs
)->regs
[SP_REGNUM
] = (frame_info
)->frame
;
551 /* And the PC - remember the pushed FP is always two bytes long */
552 (frame_saved_regs
)->regs
[PC_REGNUM
] = (frame_info
)->frame
+ 2;
555 saved_pc_after_call(frame
)
558 int a
= read_register(SP_REGNUM
);
559 x
= read_memory_integer (a
, PTR_SIZE
);
564 /* Nonzero if instruction at PC is a return instruction. */
568 int b1
= read_memory_integer(pc
,1);
572 case 0x14: /* rtd #8 */
573 case 0x1c: /* rtd #16 */
579 int b2
= read_memory_integer(pc
+1,1);
582 case 0x18: /* prts */
583 case 0x14: /* prtd #8 */
584 case 0x16: /* prtd #16 */
594 h8500_set_pointer_size (newsize
)
597 static int oldsize
= 0;
599 if (oldsize
!= newsize
)
601 printf ("pointer size set to %d bits\n", newsize
);
611 _initialize_gdbtypes ();
616 struct cmd_list_element
*setmemorylist
;
620 segmented_command (args
, from_tty
)
624 h8500_set_pointer_size (32);
628 unsegmented_command (args
, from_tty
)
632 h8500_set_pointer_size (16);
636 set_memory (args
, from_tty
)
640 printf ("\"set memory\" must be followed by the name of a memory subcommand.\n");
641 help_list (setmemorylist
, "set memory ", -1, stdout
);
644 /* See if variable name is ppc or pr[0-7] */
647 h8500_is_trapped_internalvar (name
)
653 if (strcmp(name
+1, "pc") == 0)
659 && name
[3] == '\000')
666 h8500_value_of_trapped_internalvar (var
)
667 struct internalvar
*var
;
670 unsigned char regbuf
[4];
671 int page_regnum
, regnum
;
673 regnum
= var
->name
[2] == 'c' ? PC_REGNUM
: var
->name
[2] - '0';
675 switch (var
->name
[2])
678 page_regnum
= SEG_C_REGNUM
;
680 case '0': case '1': case '2': case '3':
681 page_regnum
= SEG_D_REGNUM
;
684 page_regnum
= SEG_E_REGNUM
;
687 page_regnum
= SEG_T_REGNUM
;
691 get_saved_register (regbuf
, NULL
, NULL
, selected_frame
, page_regnum
, NULL
);
692 regval
= regbuf
[0] << 16;
694 get_saved_register (regbuf
, NULL
, NULL
, selected_frame
, regnum
, NULL
);
695 regval
|= regbuf
[0] << 8 | regbuf
[1]; /* XXX host/target byte order */
697 free (var
->value
); /* Free up old value */
699 var
->value
= value_from_longest (builtin_type_unsigned_long
, regval
);
700 release_value (var
->value
); /* Unchain new value */
702 VALUE_LVAL (var
->value
) = lval_internalvar
;
703 VALUE_INTERNALVAR (var
->value
) = var
;
708 h8500_set_trapped_internalvar (var
, newval
, bitpos
, bitsize
, offset
)
709 struct internalvar
*var
;
710 int offset
, bitpos
, bitsize
;
713 char *page_regnum
, *regnum
;
714 char expression
[100];
717 enum type_code newval_type_code
;
719 type
= VALUE_TYPE (newval
);
720 newval_type_code
= TYPE_CODE (type
);
722 if ((newval_type_code
!= TYPE_CODE_INT
723 && newval_type_code
!= TYPE_CODE_PTR
)
724 || TYPE_LENGTH (type
) != sizeof(new_regval
))
725 error("Illegal type (%s) for assignment to $%s\n",
726 TYPE_NAME (type
), var
->name
);
728 new_regval
= *(long *)VALUE_CONTENTS_RAW(newval
);
730 regnum
= var
->name
+ 1;
732 switch (var
->name
[2])
737 case '0': case '1': case '2': case '3':
748 sprintf (expression
, "$%s=%d", page_regnum
, new_regval
>> 16);
749 parse_and_eval(expression
);
751 sprintf (expression
, "$%s=%d", regnum
, new_regval
& 0xffff);
752 parse_and_eval(expression
);
755 _initialize_h8500_tdep ()
757 add_prefix_cmd ("memory", no_class
, set_memory
,
758 "set the memory model", &setmemorylist
, "set memory ", 0,
760 add_cmd ("segmented", class_support
, segmented_command
,
761 "Set segmented memory model.", &setmemorylist
);
762 add_cmd ("unsegmented", class_support
, unsegmented_command
,
763 "Set unsegmented memory model.", &setmemorylist
);