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
32 #include "../opcodes/h8500-opc.h"
37 #define UNSIGNED_SHORT(X) ((X) & 0xffff)
40 /* Shape of an H8/500 frame :
47 return address <2 or 4 bytes>
57 /* an easy to debug H8 stack frame looks like:
61 0x7905 nnnn mov.w #n,r5 or 0x1b87 subs #2,sp
66 #define IS_PUSH(x) ((x & 0xff00)==0x6d00)
67 #define IS_LINK_8(x) ((x) == 0x17)
68 #define IS_LINK_16(x) ((x) == 0x1f)
69 #define IS_MOVE_FP(x) (x == 0x0d76)
70 #define IS_MOV_SP_FP(x) (x == 0x0d76)
71 #define IS_SUB2_SP(x) (x==0x1b87)
72 #define IS_MOVK_R5(x) (x==0x7905)
73 #define IS_SUB_R5SP(x) (x==0x1957)
79 CORE_ADDR examine_prologue ();
81 void frame_find_saved_regs ();
83 h8500_skip_prologue (start_pc)
90 w = read_memory_integer (start_pc, 1);
94 w = read_memory_integer (start_pc,1);
100 w = read_memory_integer (start_pc,2);
103 /* Skip past a move to FP */
107 w = read_memory_short (start_pc);
110 /* Skip the stack adjust */
115 w = read_memory_short (start_pc);
120 w = read_memory_short (start_pc);
122 while (IS_SUB2_SP (w))
125 w = read_memory_short (start_pc);
133 print_insn (memaddr, stream)
137 /* Nothing is bigger than 8 bytes */
139 disassemble_info info;
140 read_memory (memaddr, data, sizeof (data));
141 GDB_INIT_DISASSEMBLE_INFO(info, stream);
142 return print_insn_h8500 (memaddr, data, &info);
145 /* Given a GDB frame, determine the address of the calling function's frame.
146 This will be used to create a new GDB frame struct, and then
147 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
149 For us, the frame address is its stack pointer value, so we look up
150 the function prologue to determine the caller's sp value, and return it. */
153 FRAME_CHAIN (thisframe)
156 static int loopcount;
158 if (!inside_entry_file ((thisframe)->pc))
160 int v = read_memory_integer ((thisframe)->frame, PTR_SIZE) ;
162 /* Detect loops in the stack */
163 if (v == prevr) loopcount++;
166 if (loopcount > 5) return 0;
171 /* Put here the code to store, into a struct frame_saved_regs,
172 the addresses of the saved registers of frame described by FRAME_INFO.
173 This includes special registers such as pc and fp saved in special
174 ways in the stack frame. sp is even more special:
175 the address we return for it IS the sp for the next frame.
177 We cache the result of doing this in the frame_cache_obstack, since
178 it is fairly expensive. */
182 frame_find_saved_regs (fi, fsr)
183 struct frame_info *fi;
184 struct frame_saved_regs *fsr;
186 register CORE_ADDR next_addr;
187 register CORE_ADDR *saved_regs;
189 register struct frame_saved_regs *cache_fsr;
190 extern struct obstack frame_cache_obstack;
192 struct symtab_and_line sal;
197 cache_fsr = (struct frame_saved_regs *)
198 obstack_alloc (&frame_cache_obstack,
199 sizeof (struct frame_saved_regs));
200 bzero (cache_fsr, sizeof (struct frame_saved_regs));
204 /* Find the start and end of the function prologue. If the PC
205 is in the function prologue, we only consider the part that
206 has executed already. */
208 ip = get_pc_function_start (fi->pc);
209 sal = find_pc_line (ip, 0);
210 limit = (sal.end && sal.end < fi->pc) ? sal.end : fi->pc;
212 /* This will fill in fields in *fi as well as in cache_fsr. */
213 examine_prologue (ip, limit, fi->frame, cache_fsr, fi);
222 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
223 is not the address of a valid instruction, the address of the next
224 instruction beyond ADDR otherwise. *PWORD1 receives the first word
225 of the instruction.*/
228 NEXT_PROLOGUE_INSN (addr, lim, pword1)
235 read_memory (addr, pword1, 1);
236 read_memory (addr, pword1 + 1, 1);
242 /* Examine the prologue of a function. `ip' points to the first instruction.
243 `limit' is the limit of the prologue (e.g. the addr of the first
244 linenumber, or perhaps the program counter if we're stepping through).
245 `frame_sp' is the stack pointer value in use in this frame.
246 `fsr' is a pointer to a frame_saved_regs structure into which we put
247 info about the registers saved by this frame.
248 `fi' is a struct frame_info pointer; we fill in various fields in it
249 to reflect the offsets of the arg pointer and the locals pointer. */
252 examine_prologue (ip, limit, after_prolog_fp, fsr, fi)
253 register CORE_ADDR ip;
254 register CORE_ADDR limit;
255 FRAME_ADDR after_prolog_fp;
256 struct frame_saved_regs *fsr;
257 struct frame_info *fi;
259 register CORE_ADDR next_ip;
265 register struct pic_prologue_code *pcode;
268 unsigned int reg_save_depth = 2; /* Number of things pushed onto
269 stack, starts at 2, 'cause the
270 PC is already there */
272 unsigned int auto_depth = 0; /* Number of bytes of autos */
274 char in_frame[8]; /* One for each reg */
276 memset (in_frame, 1, 8);
277 for (r = 0; r < 8; r++)
281 if (after_prolog_fp == 0)
283 after_prolog_fp = read_register (SP_REGNUM);
285 if (ip == 0 || ip & ~0xffffff)
288 ok = NEXT_PROLOGUE_INSN (ip, limit, &insn[0]);
290 /* Skip over any fp push instructions */
291 fsr->regs[6] = after_prolog_fp;
293 if (ok && IS_LINK_8 (insn[0]))
297 in_frame[6] = reg_save_depth;
301 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
303 /* Is this a move into the fp */
304 if (next_ip && IS_MOV_SP_FP (insn_word))
307 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
311 /* Skip over any stack adjustment, happens either with a number of
312 sub#2,sp or a mov #x,r5 sub r5,sp */
314 if (next_ip && IS_SUB2_SP (insn_word))
316 while (next_ip && IS_SUB2_SP (insn_word))
320 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
325 if (next_ip && IS_MOVK_R5 (insn_word))
328 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
329 auto_depth += insn_word;
331 next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn_word);
332 auto_depth += insn_word;
336 /* Work out which regs are stored where */
337 while (next_ip && IS_PUSH (insn_word))
340 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
341 fsr->regs[r] = after_prolog_fp + auto_depth;
345 /* The args are always reffed based from the stack pointer */
346 fi->args_pointer = after_prolog_fp;
347 /* Locals are always reffed based from the fp */
348 fi->locals_pointer = after_prolog_fp;
349 /* The PC is at a known place */
350 fi->from_pc = read_memory_short (after_prolog_fp + 2);
352 /* Rememeber any others too */
353 in_frame[PC_REGNUM] = 0;
356 /* We keep the old FP in the SP spot */
357 fsr->regs[SP_REGNUM] = (read_memory_short (fsr->regs[6]));
359 fsr->regs[SP_REGNUM] = after_prolog_fp + auto_depth;
366 init_extra_frame_info (fromleaf, fi)
368 struct frame_info *fi;
370 fi->fsr = 0; /* Not yet allocated */
371 fi->args_pointer = 0; /* Unknown */
372 fi->locals_pointer = 0; /* Unknown */
378 /* Return the saved PC from this frame. */
381 frame_saved_pc (frame)
384 return read_memory_integer ((frame)->frame + 2, PTR_SIZE);
388 frame_locals_address (fi)
389 struct frame_info *fi;
394 /* Return the address of the argument block for the frame
395 described by FI. Returns 0 if the address is unknown. */
398 frame_args_address (fi)
399 struct frame_info *fi;
401 return fi->frame + PTR_SIZE; /* Skip the PC */
408 struct frame_saved_regs fsr;
409 struct frame_info *fi;
411 FRAME frame = get_current_frame ();
413 fi = get_frame_info (frame);
414 get_frame_saved_regs (fi, &fsr);
416 for (regnum = 0; regnum < 8; regnum++)
418 if (fsr.regs[regnum])
420 write_register (regnum, read_memory_short (fsr.regs[regnum]));
423 flush_cached_frames ();
424 set_current_frame (create_new_frame (read_register (FP_REGNUM),
432 print_register_hook (regno)
434 if (regno == CCR_REGNUM)
442 read_relative_register_raw_bytes (regno, b);
445 printf ("I-%d - ", (l & 0x80) != 0);
470 if ((Z | (N ^ V)) == 0)
472 if ((Z | (N ^ V)) == 1)
482 return reginfo[N].offset;
485 register_raw_size (N)
487 if (N <= R7) return 2;
491 register_virtual_size (N)
493 if (N <= R7) return 2;
499 register_convert_to_raw (regnum, from, to)
528 register_convert_to_virtual (regnum, from, to)
557 register_virtual_type (N)
561 /* Although these are actually word size registers, we treat them
562 like longs so that we can deal with any implicit segmentation */
572 return builtin_type_unsigned_long;
577 return builtin_type_unsigned_char;
587 return builtin_type_unsigned_short;
598 /* Put here the code to store, into a struct frame_saved_regs,
599 the addresses of the saved registers of frame described by FRAME_INFO.
600 This includes special registers such as pc and fp saved in special
601 ways in the stack frame. sp is even more special:
602 the address we return for it IS the sp for the next frame. */
605 frame_find_saved_regs (frame_info, frame_saved_regs)
606 struct frame_info *frame_info;
607 struct frame_saved_regs *frame_saved_regs;
611 register int regmask;
612 register CORE_ADDR next_addr;
613 register CORE_ADDR pc;
614 unsigned char thebyte;
616 bzero (frame_saved_regs, sizeof *frame_saved_regs);
618 if ((frame_info)->pc >= (frame_info)->frame - CALL_DUMMY_LENGTH - FP_REGNUM * 4 - 4
619 && (frame_info)->pc <= (frame_info)->frame)
621 next_addr = (frame_info)->frame;
622 pc = (frame_info)->frame - CALL_DUMMY_LENGTH - FP_REGNUM * 4 - 4;
626 pc = get_pc_function_start ((frame_info)->pc);
627 /* Verify we have a link a6 instruction next;
628 if not we lose. If we win, find the address above the saved
629 regs using the amount of storage from the link instruction.
632 thebyte = read_memory_integer(pc, 1);
634 next_addr = (frame_info)->frame + read_memory_integer (pc += 1, 2), pc += 2;
635 else if (0x17 == thebyte)
636 next_addr = (frame_info)->frame + read_memory_integer (pc += 1, 1), pc += 1;
641 /* If have an add:g.waddal #-n, sp next, adjust next_addr. */
642 if ((0x0c0177777 & read_memory_integer (pc, 2)) == 0157774)
643 next_addr += read_memory_integer (pc += 2, 4), pc += 4;
647 thebyte = read_memory_integer(pc, 1);
648 if (thebyte == 0x12) {
651 regmask = read_memory_integer(pc,1);
653 for (regnum = 0; regnum < 8; regnum ++, regmask >>=1)
657 (frame_saved_regs)->regs[regnum] = (next_addr += 2) - 2;
660 thebyte = read_memory_integer(pc, 1);
662 /* Maybe got a load of pushes */
663 while (thebyte == 0xbf) {
665 regnum = read_memory_integer(pc,1) & 0x7;
667 (frame_saved_regs)->regs[regnum] = (next_addr += 2) - 2;
668 thebyte = read_memory_integer(pc, 1);
673 /* Remember the address of the frame pointer */
674 (frame_saved_regs)->regs[FP_REGNUM] = (frame_info)->frame;
676 /* This is where the old sp is hidden */
677 (frame_saved_regs)->regs[SP_REGNUM] = (frame_info)->frame;
679 /* And the PC - remember the pushed FP is always two bytes long */
680 (frame_saved_regs)->regs[PC_REGNUM] = (frame_info)->frame + 2;
683 saved_pc_after_call(frame)
686 int a = read_register(SP_REGNUM);
687 x = read_memory_integer (a, PTR_SIZE);
692 /* Nonzero if instruction at PC is a return instruction. */
696 int b1 = read_memory_integer(pc,1);
700 case 0x14: /* rtd #8 */
701 case 0x1c: /* rtd #16 */
707 int b2 = read_memory_integer(pc+1,1);
710 case 0x18: /* prts */
711 case 0x14: /* prtd #8 */
712 case 0x16: /* prtd #16 */
722 h8500_set_pointer_size (newsize)
725 static int oldsize = 0;
727 if (oldsize != newsize)
729 printf ("pointer size set to %d bits\n", newsize);
739 _initialize_gdbtypes ();
744 struct cmd_list_element *setmemorylist;
748 segmented_command (args, from_tty)
752 h8500_set_pointer_size (32);
756 unsegmented_command (args, from_tty)
760 h8500_set_pointer_size (16);
764 set_memory (args, from_tty)
768 printf ("\"set memory\" must be followed by the name of a memory subcommand.\n");
769 help_list (setmemorylist, "set memory ", -1, stdout);
773 _initialize_h8500_tdep ()
775 /* Sanitity check a few things */
776 if (FP_REGNUM != GPR6
778 || CCR_REGNUM != GCCR
794 add_prefix_cmd ("memory", no_class, set_memory,
795 "set the memory model", &setmemorylist, "set memory ", 0,
797 add_cmd ("segmented", class_support, segmented_command,
798 "Set segmented memory model.", &setmemorylist);
799 add_cmd ("unsegmented", class_support, unsegmented_command,
800 "Set unsegmented memory model.", &setmemorylist);