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 ();
83 h8500_skip_prologue (start_pc)
89 w = read_memory_integer (start_pc, 1);
93 w = read_memory_integer (start_pc, 1);
99 w = read_memory_integer (start_pc, 2);
106 print_insn (memaddr, stream)
110 disassemble_info info;
111 GDB_INIT_DISASSEMBLE_INFO (info, stream);
112 return print_insn_h8500 (memaddr, &info);
115 /* Given a GDB frame, determine the address of the calling function's frame.
116 This will be used to create a new GDB frame struct, and then
117 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
119 For us, the frame address is its stack pointer value, so we look up
120 the function prologue to determine the caller's sp value, and return it. */
123 h8500_frame_chain (thisframe)
126 if (!inside_entry_file (thisframe->pc))
127 return (read_memory_integer (FRAME_FP (thisframe), PTR_SIZE));
133 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
134 is not the address of a valid instruction, the address of the next
135 instruction beyond ADDR otherwise. *PWORD1 receives the first word
136 of the instruction.*/
139 NEXT_PROLOGUE_INSN (addr, lim, pword1)
146 read_memory (addr, pword1, 1);
147 read_memory (addr, pword1 + 1, 1);
153 /* Examine the prologue of a function. `ip' points to the first instruction.
154 `limit' is the limit of the prologue (e.g. the addr of the first
155 linenumber, or perhaps the program counter if we're stepping through).
156 `frame_sp' is the stack pointer value in use in this frame.
157 `fsr' is a pointer to a frame_saved_regs structure into which we put
158 info about the registers saved by this frame.
159 `fi' is a struct frame_info pointer; we fill in various fields in it
160 to reflect the offsets of the arg pointer and the locals pointer. */
163 /* Return the saved PC from this frame. */
166 frame_saved_pc (frame)
169 return read_memory_integer ((frame)->frame + 2, PTR_SIZE);
173 frame_locals_address (fi)
174 struct frame_info *fi;
179 /* Return the address of the argument block for the frame
180 described by FI. Returns 0 if the address is unknown. */
183 frame_args_address (fi)
184 struct frame_info *fi;
193 struct frame_saved_regs fsr;
194 struct frame_info *fi;
196 FRAME frame = get_current_frame ();
198 fi = get_frame_info (frame);
199 get_frame_saved_regs (fi, &fsr);
201 for (regnum = 0; regnum < 8; regnum++)
203 if (fsr.regs[regnum])
205 write_register (regnum, read_memory_short (fsr.regs[regnum]));
208 flush_cached_frames ();
209 set_current_frame (create_new_frame (read_register (FP_REGNUM),
217 print_register_hook (regno)
219 if (regno == CCR_REGNUM)
227 read_relative_register_raw_bytes (regno, b);
229 printf_unfiltered ("\t");
230 printf_unfiltered ("I-%d - ", (l & 0x80) != 0);
235 printf_unfiltered ("N-%d ", N);
236 printf_unfiltered ("Z-%d ", Z);
237 printf_unfiltered ("V-%d ", V);
238 printf_unfiltered ("C-%d ", C);
240 printf_unfiltered ("u> ");
242 printf_unfiltered ("u<= ");
244 printf_unfiltered ("u>= ");
246 printf_unfiltered ("u< ");
248 printf_unfiltered ("!= ");
250 printf_unfiltered ("== ");
252 printf_unfiltered (">= ");
254 printf_unfiltered ("< ");
255 if ((Z | (N ^ V)) == 0)
256 printf_unfiltered ("> ");
257 if ((Z | (N ^ V)) == 1)
258 printf_unfiltered ("<= ");
263 h8500_register_size (regno)
297 h8500_register_virtual_type (regno)
306 return builtin_type_unsigned_char;
316 return builtin_type_unsigned_short;
326 return builtin_type_unsigned_long;
332 /* Put here the code to store, into a struct frame_saved_regs,
333 the addresses of the saved registers of frame described by FRAME_INFO.
334 This includes special registers such as pc and fp saved in special
335 ways in the stack frame. sp is even more special:
336 the address we return for it IS the sp for the next frame. */
339 frame_find_saved_regs (frame_info, frame_saved_regs)
340 struct frame_info *frame_info;
341 struct frame_saved_regs *frame_saved_regs;
345 register int regmask;
346 register CORE_ADDR next_addr;
347 register CORE_ADDR pc;
348 unsigned char thebyte;
350 memset (frame_saved_regs, '\0', sizeof *frame_saved_regs);
352 if ((frame_info)->pc >= (frame_info)->frame - CALL_DUMMY_LENGTH - FP_REGNUM * 4 - 4
353 && (frame_info)->pc <= (frame_info)->frame)
355 next_addr = (frame_info)->frame;
356 pc = (frame_info)->frame - CALL_DUMMY_LENGTH - FP_REGNUM * 4 - 4;
360 pc = get_pc_function_start ((frame_info)->pc);
361 /* Verify we have a link a6 instruction next;
362 if not we lose. If we win, find the address above the saved
363 regs using the amount of storage from the link instruction.
366 thebyte = read_memory_integer (pc, 1);
368 next_addr = (frame_info)->frame + read_memory_integer (pc += 1, 2), pc += 2;
369 else if (0x17 == thebyte)
370 next_addr = (frame_info)->frame + read_memory_integer (pc += 1, 1), pc += 1;
375 /* If have an add:g.waddal #-n, sp next, adjust next_addr. */
376 if ((0x0c0177777 & read_memory_integer (pc, 2)) == 0157774)
377 next_addr += read_memory_integer (pc += 2, 4), pc += 4;
381 thebyte = read_memory_integer (pc, 1);
386 regmask = read_memory_integer (pc, 1);
388 for (regnum = 0; regnum < 8; regnum++, regmask >>= 1)
392 (frame_saved_regs)->regs[regnum] = (next_addr += 2) - 2;
395 thebyte = read_memory_integer (pc, 1);
397 /* Maybe got a load of pushes */
398 while (thebyte == 0xbf)
401 regnum = read_memory_integer (pc, 1) & 0x7;
403 (frame_saved_regs)->regs[regnum] = (next_addr += 2) - 2;
404 thebyte = read_memory_integer (pc, 1);
409 /* Remember the address of the frame pointer */
410 (frame_saved_regs)->regs[FP_REGNUM] = (frame_info)->frame;
412 /* This is where the old sp is hidden */
413 (frame_saved_regs)->regs[SP_REGNUM] = (frame_info)->frame;
415 /* And the PC - remember the pushed FP is always two bytes long */
416 (frame_saved_regs)->regs[PC_REGNUM] = (frame_info)->frame + 2;
419 saved_pc_after_call (frame)
422 int a = read_register (SP_REGNUM);
423 x = read_memory_integer (a, PTR_SIZE);
428 /* Nonzero if instruction at PC is a return instruction. */
432 int b1 = read_memory_integer (pc, 1);
436 case 0x14: /* rtd #8 */
437 case 0x1c: /* rtd #16 */
443 int b2 = read_memory_integer (pc + 1, 1);
446 case 0x18: /* prts */
447 case 0x14: /* prtd #8 */
448 case 0x16: /* prtd #16 */
458 h8500_set_pointer_size (newsize)
461 static int oldsize = 0;
463 if (oldsize != newsize)
465 printf_unfiltered ("pointer size set to %d bits\n", newsize);
475 _initialize_gdbtypes ();
480 struct cmd_list_element *setmemorylist;
484 segmented_command (args, from_tty)
488 h8500_set_pointer_size (32);
492 unsegmented_command (args, from_tty)
496 h8500_set_pointer_size (16);
500 set_memory (args, from_tty)
504 printf_unfiltered ("\"set memory\" must be followed by the name of a memory subcommand.\n");
505 help_list (setmemorylist, "set memory ", -1, gdb_stdout);
508 /* See if variable name is ppc or pr[0-7] */
511 h8500_is_trapped_internalvar (name)
517 if (strcmp (name + 1, "pc") == 0)
523 && name[3] == '\000')
530 h8500_value_of_trapped_internalvar (var)
531 struct internalvar *var;
534 unsigned char regbuf[4];
535 int page_regnum, regnum;
537 regnum = var->name[2] == 'c' ? PC_REGNUM : var->name[2] - '0';
539 switch (var->name[2])
542 page_regnum = SEG_C_REGNUM;
548 page_regnum = SEG_D_REGNUM;
552 page_regnum = SEG_E_REGNUM;
556 page_regnum = SEG_T_REGNUM;
560 get_saved_register (regbuf, NULL, NULL, selected_frame, page_regnum, NULL);
561 regval = regbuf[0] << 16;
563 get_saved_register (regbuf, NULL, NULL, selected_frame, regnum, NULL);
564 regval |= regbuf[0] << 8 | regbuf[1]; /* XXX host/target byte order */
566 free (var->value); /* Free up old value */
568 var->value = value_from_longest (builtin_type_unsigned_long, regval);
569 release_value (var->value); /* Unchain new value */
571 VALUE_LVAL (var->value) = lval_internalvar;
572 VALUE_INTERNALVAR (var->value) = var;
577 h8500_set_trapped_internalvar (var, newval, bitpos, bitsize, offset)
578 struct internalvar *var;
579 int offset, bitpos, bitsize;
582 char *page_regnum, *regnum;
583 char expression[100];
586 enum type_code newval_type_code;
588 type = VALUE_TYPE (newval);
589 newval_type_code = TYPE_CODE (type);
591 if ((newval_type_code != TYPE_CODE_INT
592 && newval_type_code != TYPE_CODE_PTR)
593 || TYPE_LENGTH (type) != sizeof (new_regval))
594 error ("Illegal type (%s) for assignment to $%s\n",
595 TYPE_NAME (type), var->name);
597 new_regval = *(long *) VALUE_CONTENTS_RAW (newval);
599 regnum = var->name + 1;
601 switch (var->name[2])
622 sprintf (expression, "$%s=%d", page_regnum, new_regval >> 16);
623 parse_and_eval (expression);
625 sprintf (expression, "$%s=%d", regnum, new_regval & 0xffff);
626 parse_and_eval (expression);
630 _initialize_h8500_tdep ()
632 add_prefix_cmd ("memory", no_class, set_memory,
633 "set the memory model", &setmemorylist, "set memory ", 0,
635 add_cmd ("segmented", class_support, segmented_command,
636 "Set segmented memory model.", &setmemorylist);
637 add_cmd ("unsegmented", class_support, unsegmented_command,
638 "Set unsegmented memory model.", &setmemorylist);
645 return read_register (PR7_REGNUM);
652 write_register (PR7_REGNUM, v);
658 return read_register (PC_REGNUM);
665 write_register (PC_REGNUM, v);
671 return read_register (PR6_REGNUM);
678 write_register (PR6_REGNUM, v);