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 disassemble_info info;
114 GDB_INIT_DISASSEMBLE_INFO(info, stream);
115 return print_insn_h8500 (memaddr, &info);
118 /* Given a GDB frame, determine the address of the calling function's frame.
119 This will be used to create a new GDB frame struct, and then
120 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
122 For us, the frame address is its stack pointer value, so we look up
123 the function prologue to determine the caller's sp value, and return it. */
126 h8500_frame_chain (thisframe)
130 if (!inside_entry_file (thisframe->pc))
131 return read_memory_integer(thisframe->frame, 2)
132 | (read_register(SEG_T_REGNUM) << 16);
137 /* Put here the code to store, into a struct frame_saved_regs,
138 the addresses of the saved registers of frame described by FRAME_INFO.
139 This includes special registers such as pc and fp saved in special
140 ways in the stack frame. sp is even more special:
141 the address we return for it IS the sp for the next frame.
143 We cache the result of doing this in the frame_cache_obstack, since
144 it is fairly expensive. */
148 frame_find_saved_regs (fi, fsr)
149 struct frame_info *fi;
150 struct frame_saved_regs *fsr;
152 register CORE_ADDR next_addr;
153 register CORE_ADDR *saved_regs;
155 register struct frame_saved_regs *cache_fsr;
156 extern struct obstack frame_cache_obstack;
158 struct symtab_and_line sal;
163 cache_fsr = (struct frame_saved_regs *)
164 obstack_alloc (&frame_cache_obstack,
165 sizeof (struct frame_saved_regs));
166 bzero (cache_fsr, sizeof (struct frame_saved_regs));
170 /* Find the start and end of the function prologue. If the PC
171 is in the function prologue, we only consider the part that
172 has executed already. */
174 ip = get_pc_function_start (fi->pc);
175 sal = find_pc_line (ip, 0);
176 limit = (sal.end && sal.end < fi->pc) ? sal.end : fi->pc;
178 /* This will fill in fields in *fi as well as in cache_fsr. */
179 examine_prologue (ip, limit, fi->frame, cache_fsr, fi);
188 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
189 is not the address of a valid instruction, the address of the next
190 instruction beyond ADDR otherwise. *PWORD1 receives the first word
191 of the instruction.*/
194 NEXT_PROLOGUE_INSN (addr, lim, pword1)
201 read_memory (addr, pword1, 1);
202 read_memory (addr, pword1 + 1, 1);
208 /* Examine the prologue of a function. `ip' points to the first instruction.
209 `limit' is the limit of the prologue (e.g. the addr of the first
210 linenumber, or perhaps the program counter if we're stepping through).
211 `frame_sp' is the stack pointer value in use in this frame.
212 `fsr' is a pointer to a frame_saved_regs structure into which we put
213 info about the registers saved by this frame.
214 `fi' is a struct frame_info pointer; we fill in various fields in it
215 to reflect the offsets of the arg pointer and the locals pointer. */
218 examine_prologue (ip, limit, after_prolog_fp, fsr, fi)
219 register CORE_ADDR ip;
220 register CORE_ADDR limit;
221 FRAME_ADDR after_prolog_fp;
222 struct frame_saved_regs *fsr;
223 struct frame_info *fi;
225 register CORE_ADDR next_ip;
231 register struct pic_prologue_code *pcode;
234 unsigned int reg_save_depth = 2; /* Number of things pushed onto
235 stack, starts at 2, 'cause the
236 PC is already there */
238 unsigned int auto_depth = 0; /* Number of bytes of autos */
240 char in_frame[8]; /* One for each reg */
242 memset (in_frame, 1, 8);
243 for (r = 0; r < 8; r++)
247 if (after_prolog_fp == 0)
249 after_prolog_fp = read_register (SP_REGNUM);
251 if (ip == 0 || ip & ~0xffffff)
254 ok = NEXT_PROLOGUE_INSN (ip, limit, &insn[0]);
256 /* Skip over any fp push instructions */
257 fsr->regs[6] = after_prolog_fp;
259 if (ok && IS_LINK_8 (insn[0]))
263 in_frame[6] = reg_save_depth;
267 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
269 /* Is this a move into the fp */
270 if (next_ip && IS_MOV_SP_FP (insn_word))
273 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
277 /* Skip over any stack adjustment, happens either with a number of
278 sub#2,sp or a mov #x,r5 sub r5,sp */
280 if (next_ip && IS_SUB2_SP (insn_word))
282 while (next_ip && IS_SUB2_SP (insn_word))
286 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
291 if (next_ip && IS_MOVK_R5 (insn_word))
294 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
295 auto_depth += insn_word;
297 next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn_word);
298 auto_depth += insn_word;
302 /* Work out which regs are stored where */
303 while (next_ip && IS_PUSH (insn_word))
306 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
307 fsr->regs[r] = after_prolog_fp + auto_depth;
311 /* The args are always reffed based from the stack pointer */
312 fi->args_pointer = after_prolog_fp;
313 /* Locals are always reffed based from the fp */
314 fi->locals_pointer = after_prolog_fp;
315 /* The PC is at a known place */
316 fi->from_pc = read_memory_short (after_prolog_fp + 2);
318 /* Rememeber any others too */
319 in_frame[PC_REGNUM] = 0;
322 /* We keep the old FP in the SP spot */
323 fsr->regs[SP_REGNUM] = (read_memory_short (fsr->regs[6]));
325 fsr->regs[SP_REGNUM] = after_prolog_fp + auto_depth;
331 /* Return the saved PC from this frame. */
334 frame_saved_pc (frame)
337 return read_memory_integer ((frame)->frame + 2, PTR_SIZE);
341 frame_locals_address (fi)
342 struct frame_info *fi;
347 /* Return the address of the argument block for the frame
348 described by FI. Returns 0 if the address is unknown. */
351 frame_args_address (fi)
352 struct frame_info *fi;
361 struct frame_saved_regs fsr;
362 struct frame_info *fi;
364 FRAME frame = get_current_frame ();
366 fi = get_frame_info (frame);
367 get_frame_saved_regs (fi, &fsr);
369 for (regnum = 0; regnum < 8; regnum++)
371 if (fsr.regs[regnum])
373 write_register (regnum, read_memory_short (fsr.regs[regnum]));
376 flush_cached_frames ();
377 set_current_frame (create_new_frame (read_register (FP_REGNUM),
385 print_register_hook (regno)
387 if (regno == CCR_REGNUM)
395 read_relative_register_raw_bytes (regno, b);
398 printf ("I-%d - ", (l & 0x80) != 0);
423 if ((Z | (N ^ V)) == 0)
425 if ((Z | (N ^ V)) == 1)
431 h8500_register_size (regno)
434 if (regno <= PC_REGNUM)
441 h8500_register_virtual_type (regno)
450 return builtin_type_unsigned_char;
461 return builtin_type_unsigned_short;
467 /* Put here the code to store, into a struct frame_saved_regs,
468 the addresses of the saved registers of frame described by FRAME_INFO.
469 This includes special registers such as pc and fp saved in special
470 ways in the stack frame. sp is even more special:
471 the address we return for it IS the sp for the next frame. */
474 frame_find_saved_regs (frame_info, frame_saved_regs)
475 struct frame_info *frame_info;
476 struct frame_saved_regs *frame_saved_regs;
480 register int regmask;
481 register CORE_ADDR next_addr;
482 register CORE_ADDR pc;
483 unsigned char thebyte;
485 bzero (frame_saved_regs, sizeof *frame_saved_regs);
487 if ((frame_info)->pc >= (frame_info)->frame - CALL_DUMMY_LENGTH - FP_REGNUM * 4 - 4
488 && (frame_info)->pc <= (frame_info)->frame)
490 next_addr = (frame_info)->frame;
491 pc = (frame_info)->frame - CALL_DUMMY_LENGTH - FP_REGNUM * 4 - 4;
495 pc = get_pc_function_start ((frame_info)->pc);
496 /* Verify we have a link a6 instruction next;
497 if not we lose. If we win, find the address above the saved
498 regs using the amount of storage from the link instruction.
501 thebyte = read_memory_integer(pc, 1);
503 next_addr = (frame_info)->frame + read_memory_integer (pc += 1, 2), pc += 2;
504 else if (0x17 == thebyte)
505 next_addr = (frame_info)->frame + read_memory_integer (pc += 1, 1), pc += 1;
510 /* If have an add:g.waddal #-n, sp next, adjust next_addr. */
511 if ((0x0c0177777 & read_memory_integer (pc, 2)) == 0157774)
512 next_addr += read_memory_integer (pc += 2, 4), pc += 4;
516 thebyte = read_memory_integer(pc, 1);
517 if (thebyte == 0x12) {
520 regmask = read_memory_integer(pc,1);
522 for (regnum = 0; regnum < 8; regnum ++, regmask >>=1)
526 (frame_saved_regs)->regs[regnum] = (next_addr += 2) - 2;
529 thebyte = read_memory_integer(pc, 1);
531 /* Maybe got a load of pushes */
532 while (thebyte == 0xbf) {
534 regnum = read_memory_integer(pc,1) & 0x7;
536 (frame_saved_regs)->regs[regnum] = (next_addr += 2) - 2;
537 thebyte = read_memory_integer(pc, 1);
542 /* Remember the address of the frame pointer */
543 (frame_saved_regs)->regs[FP_REGNUM] = (frame_info)->frame;
545 /* This is where the old sp is hidden */
546 (frame_saved_regs)->regs[SP_REGNUM] = (frame_info)->frame;
548 /* And the PC - remember the pushed FP is always two bytes long */
549 (frame_saved_regs)->regs[PC_REGNUM] = (frame_info)->frame + 2;
552 saved_pc_after_call(frame)
555 int a = read_register(SP_REGNUM);
556 x = read_memory_integer (a, PTR_SIZE);
561 /* Nonzero if instruction at PC is a return instruction. */
565 int b1 = read_memory_integer(pc,1);
569 case 0x14: /* rtd #8 */
570 case 0x1c: /* rtd #16 */
576 int b2 = read_memory_integer(pc+1,1);
579 case 0x18: /* prts */
580 case 0x14: /* prtd #8 */
581 case 0x16: /* prtd #16 */
591 h8500_set_pointer_size (newsize)
594 static int oldsize = 0;
596 if (oldsize != newsize)
598 printf ("pointer size set to %d bits\n", newsize);
608 _initialize_gdbtypes ();
613 struct cmd_list_element *setmemorylist;
617 segmented_command (args, from_tty)
621 h8500_set_pointer_size (32);
625 unsegmented_command (args, from_tty)
629 h8500_set_pointer_size (16);
633 set_memory (args, from_tty)
637 printf ("\"set memory\" must be followed by the name of a memory subcommand.\n");
638 help_list (setmemorylist, "set memory ", -1, stdout);
641 /* See if variable name is ppc or pr[0-7] */
644 h8500_is_trapped_internalvar (name)
650 if (strcmp(name+1, "pc") == 0)
656 && name[3] == '\000')
663 h8500_value_of_trapped_internalvar (var)
664 struct internalvar *var;
667 unsigned char regbuf[4];
668 int page_regnum, regnum;
670 regnum = var->name[2] == 'c' ? PC_REGNUM : var->name[2] - '0';
672 switch (var->name[2])
675 page_regnum = SEG_C_REGNUM;
677 case '0': case '1': case '2': case '3':
678 page_regnum = SEG_D_REGNUM;
681 page_regnum = SEG_E_REGNUM;
684 page_regnum = SEG_T_REGNUM;
688 get_saved_register (regbuf, NULL, NULL, selected_frame, page_regnum, NULL);
689 regval = regbuf[0] << 16;
691 get_saved_register (regbuf, NULL, NULL, selected_frame, regnum, NULL);
692 regval |= regbuf[0] << 8 | regbuf[1]; /* XXX host/target byte order */
694 free (var->value); /* Free up old value */
696 var->value = value_from_longest (builtin_type_unsigned_long, regval);
697 release_value (var->value); /* Unchain new value */
699 VALUE_LVAL (var->value) = lval_internalvar;
700 VALUE_INTERNALVAR (var->value) = var;
705 h8500_set_trapped_internalvar (var, newval, bitpos, bitsize, offset)
706 struct internalvar *var;
707 int offset, bitpos, bitsize;
710 char *page_regnum, *regnum;
711 char expression[100];
714 enum type_code newval_type_code;
716 type = VALUE_TYPE (newval);
717 newval_type_code = TYPE_CODE (type);
719 if ((newval_type_code != TYPE_CODE_INT
720 && newval_type_code != TYPE_CODE_PTR)
721 || TYPE_LENGTH (type) != sizeof(new_regval))
722 error("Illegal type (%s) for assignment to $%s\n",
723 TYPE_NAME (type), var->name);
725 new_regval = *(long *)VALUE_CONTENTS_RAW(newval);
727 regnum = var->name + 1;
729 switch (var->name[2])
734 case '0': case '1': case '2': case '3':
745 sprintf (expression, "$%s=%d", page_regnum, new_regval >> 16);
746 parse_and_eval(expression);
748 sprintf (expression, "$%s=%d", regnum, new_regval & 0xffff);
749 parse_and_eval(expression);
752 _initialize_h8500_tdep ()
754 add_prefix_cmd ("memory", no_class, set_memory,
755 "set the memory model", &setmemorylist, "set memory ", 0,
757 add_cmd ("segmented", class_support, segmented_command,
758 "Set segmented memory model.", &setmemorylist);
759 add_cmd ("unsegmented", class_support, unsegmented_command,
760 "Set unsegmented memory model.", &setmemorylist);