1 /* Target-machine dependent code for Renesas H8/300, for GDB.
3 Copyright 1988, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998,
4 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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. */
24 Contributed by Steve Chamberlain
32 #include "arch-utils.h"
37 #include "gdb_assert.h"
39 #include "dwarf2-frame.h"
41 #include "frame-base.h"
42 #include "frame-unwind.h"
44 /* Extra info which is saved in each frame_info. */
45 struct frame_extra_info
54 h8300_max_reg_size = 4,
57 static int is_h8300hmode (struct gdbarch *gdbarch);
58 static int is_h8300smode (struct gdbarch *gdbarch);
59 static int is_h8300sxmode (struct gdbarch *gdbarch);
60 static int is_h8300_normal_mode (struct gdbarch *gdbarch);
62 #define BINWORD (is_h8300hmode (current_gdbarch) && \
63 !is_h8300_normal_mode (current_gdbarch) ? h8300h_reg_size : h8300_reg_size)
67 E_R0_REGNUM, E_ER0_REGNUM = E_R0_REGNUM, E_ARG0_REGNUM = E_R0_REGNUM,
68 E_RET0_REGNUM = E_R0_REGNUM,
69 E_R1_REGNUM, E_ER1_REGNUM = E_R1_REGNUM, E_RET1_REGNUM = E_R1_REGNUM,
70 E_R2_REGNUM, E_ER2_REGNUM = E_R2_REGNUM, E_ARGLAST_REGNUM = E_R2_REGNUM,
71 E_R3_REGNUM, E_ER3_REGNUM = E_R3_REGNUM,
72 E_R4_REGNUM, E_ER4_REGNUM = E_R4_REGNUM,
73 E_R5_REGNUM, E_ER5_REGNUM = E_R5_REGNUM,
74 E_R6_REGNUM, E_ER6_REGNUM = E_R6_REGNUM, E_FP_REGNUM = E_R6_REGNUM,
79 E_TICK_REGNUM, E_EXR_REGNUM = E_TICK_REGNUM,
80 E_INST_REGNUM, E_TICKS_REGNUM = E_INST_REGNUM,
88 #define H8300_MAX_NUM_REGS 18
90 #define E_PSEUDO_CCR_REGNUM (NUM_REGS)
91 #define E_PSEUDO_EXR_REGNUM (NUM_REGS+1)
93 #define UNSIGNED_SHORT(X) ((X) & 0xffff)
95 #define IS_PUSH(x) ((x & 0xfff0)==0x6df0)
96 #define IS_PUSH_FP(x) (x == 0x6df6)
97 #define IS_MOVE_FP(x) (x == 0x0d76 || x == 0x0ff6)
98 #define IS_MOV_SP_FP(x) (x == 0x0d76 || x == 0x0ff6)
99 #define IS_SUB2_SP(x) (x==0x1b87)
100 #define IS_SUB4_SP(x) (x==0x1b97)
101 #define IS_SUBL_SP(x) (x==0x7a37)
102 #define IS_MOVK_R5(x) (x==0x7905)
103 #define IS_SUB_R5SP(x) (x==0x1957)
105 /* If the instruction at PC is an argument register spill, return its
106 length. Otherwise, return zero.
108 An argument register spill is an instruction that moves an argument
109 from the register in which it was passed to the stack slot in which
110 it really lives. It is a byte, word, or longword move from an
111 argument register to a negative offset from the frame pointer.
113 CV, 2003-06-16: Or, in optimized code or when the `register' qualifier
114 is used, it could be a byte, word or long move to registers r3-r5. */
117 h8300_is_argument_spill (CORE_ADDR pc)
119 int w = read_memory_unsigned_integer (pc, 2);
121 if (((w & 0xff88) == 0x0c88 /* mov.b Rsl, Rdl */
122 || (w & 0xff88) == 0x0d00 /* mov.w Rs, Rd */
123 || (w & 0xff88) == 0x0f80) /* mov.l Rs, Rd */
124 && (w & 0x70) <= 0x20 /* Rs is R0, R1 or R2 */
125 && (w & 0x7) >= 0x3 && (w & 0x7) <= 0x5) /* Rd is R3, R4 or R5 */
128 if ((w & 0xfff0) == 0x6ee0 /* mov.b Rs,@(d:16,er6) */
129 && 8 <= (w & 0xf) && (w & 0xf) <= 10) /* Rs is R0L, R1L, or R2L */
131 int w2 = read_memory_integer (pc + 2, 2);
133 /* ... and d:16 is negative. */
137 else if (w == 0x7860)
139 int w2 = read_memory_integer (pc + 2, 2);
141 if ((w2 & 0xfff0) == 0x6aa0) /* mov.b Rs, @(d:24,er6) */
143 LONGEST disp = read_memory_integer (pc + 4, 4);
145 /* ... and d:24 is negative. */
146 if (disp < 0 && disp > 0xffffff)
150 else if ((w & 0xfff0) == 0x6fe0 /* mov.w Rs,@(d:16,er6) */
151 && (w & 0xf) <= 2) /* Rs is R0, R1, or R2 */
153 int w2 = read_memory_integer (pc + 2, 2);
155 /* ... and d:16 is negative. */
159 else if (w == 0x78e0)
161 int w2 = read_memory_integer (pc + 2, 2);
163 if ((w2 & 0xfff0) == 0x6ba0) /* mov.b Rs, @(d:24,er6) */
165 LONGEST disp = read_memory_integer (pc + 4, 4);
167 /* ... and d:24 is negative. */
168 if (disp < 0 && disp > 0xffffff)
172 else if (w == 0x0100)
174 int w2 = read_memory_integer (pc + 2, 2);
176 if ((w2 & 0xfff0) == 0x6fe0 /* mov.l Rs,@(d:16,er6) */
177 && (w2 & 0xf) <= 2) /* Rs is ER0, ER1, or ER2 */
179 int w3 = read_memory_integer (pc + 4, 2);
181 /* ... and d:16 is negative. */
185 else if (w2 == 0x78e0)
187 int w3 = read_memory_integer (pc + 4, 2);
189 if ((w3 & 0xfff0) == 0x6ba0) /* mov.l Rs, @(d:24,er6) */
191 LONGEST disp = read_memory_integer (pc + 6, 4);
193 /* ... and d:24 is negative. */
194 if (disp < 0 && disp > 0xffffff)
204 h8300_skip_prologue (CORE_ADDR start_pc)
209 /* Skip past all push and stm insns. */
212 w = read_memory_unsigned_integer (start_pc, 2);
213 /* First look for push insns. */
214 if (w == 0x0100 || w == 0x0110 || w == 0x0120 || w == 0x0130)
216 w = read_memory_unsigned_integer (start_pc + 2, 2);
222 start_pc += 2 + adjust;
223 w = read_memory_unsigned_integer (start_pc, 2);
230 /* Skip past a move to FP, either word or long sized */
231 w = read_memory_unsigned_integer (start_pc, 2);
234 w = read_memory_unsigned_integer (start_pc + 2, 2);
240 start_pc += 2 + adjust;
241 w = read_memory_unsigned_integer (start_pc, 2);
244 /* Check for loading either a word constant into r5;
245 long versions are handled by the SUBL_SP below. */
249 w = read_memory_unsigned_integer (start_pc, 2);
252 /* Now check for subtracting r5 from sp, word sized only. */
255 start_pc += 2 + adjust;
256 w = read_memory_unsigned_integer (start_pc, 2);
259 /* Check for subs #2 and subs #4. */
260 while (IS_SUB2_SP (w) || IS_SUB4_SP (w))
262 start_pc += 2 + adjust;
263 w = read_memory_unsigned_integer (start_pc, 2);
266 /* Check for a 32bit subtract. */
268 start_pc += 6 + adjust;
270 /* Skip past another possible stm insn for registers R3 to R5 (possibly used
271 for register qualified arguments. */
272 w = read_memory_unsigned_integer (start_pc, 2);
273 /* First look for push insns. */
274 if (w == 0x0110 || w == 0x0120 || w == 0x0130)
276 w = read_memory_unsigned_integer (start_pc + 2, 2);
277 if (IS_PUSH (w) && (w & 0xf) >= 0x3 && (w & 0xf) <= 0x5)
281 /* Check for spilling an argument register to the stack frame.
282 This could also be an initializing store from non-prologue code,
283 but I don't think there's any harm in skipping that. */
286 int spill_size = h8300_is_argument_spill (start_pc);
289 start_pc += spill_size;
296 h8300_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
300 frame_unwind_register (next_frame, E_PC_REGNUM, buf);
301 return extract_typed_address (buf, builtin_type_void_func_ptr);
304 static struct frame_id
305 h8300_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
310 frame_unwind_register (next_frame, E_FP_REGNUM, buf);
311 fp = extract_unsigned_integer (buf, 4);
313 return frame_id_build (fp, frame_pc_unwind (next_frame));
316 struct h8300_frame_cache
323 /* Saved registers. */
324 CORE_ADDR saved_regs[H8300_MAX_NUM_REGS];
327 /* Stack space reserved for local variables. */
333 /* Allocate and initialize a frame cache. */
335 static struct h8300_frame_cache *
336 h8300_alloc_frame_cache (void)
338 struct h8300_frame_cache *cache;
341 cache = FRAME_OBSTACK_ZALLOC (struct h8300_frame_cache);
345 cache->sp_offset = -4;
348 /* Saved registers. We initialize these to -1 since zero is a valid
349 offset (that's where %fp is supposed to be stored). */
350 for (i = 0; i < NUM_REGS; i++)
351 cache->saved_regs[i] = -1;
353 /* Frameless until proven otherwise. */
359 /* Check whether PC points at a code that sets up a new stack frame.
360 If so, it updates CACHE and returns the address of the first
361 instruction after the sequence that sets removes the "hidden"
362 argument from the stack or CURRENT_PC, whichever is smaller.
363 Otherwise, return PC. */
366 h8300_analyze_frame_setup (CORE_ADDR pc, CORE_ADDR current_pc,
367 struct h8300_frame_cache *cache)
372 if (pc >= current_pc)
375 op = read_memory_unsigned_integer (pc, 4);
377 if (op == 0x6df60d76)
379 /* mov.w r6,@-sp; mov.w sp,r6 */
380 cache->saved_regs[E_FP_REGNUM] = 0;
381 cache->sp_offset += 2;
382 op = read_memory_unsigned_integer (pc + 4, 4);
383 if (((op >> 16) & 0xfff0) == 0x7900)
386 cache->locals = -(short) (op & 0xffff);
389 else if ((op >> 16) == 0x1b87)
392 for (cache->locals = 0, pc += 4;
393 read_memory_unsigned_integer (pc, 2) == 0x1b87;
394 pc += 2, cache->locals += 2);
398 else if (op == 0x01006df6)
401 op = read_memory_unsigned_integer (pc + 4, 2);
405 op = read_memory_unsigned_integer (pc + 6, 2);
409 cache->locals = -read_memory_unsigned_integer (pc + 8, 4);
412 else if (op == 0x1b97)
415 for (cache->locals = 0, pc += 6;
416 read_memory_unsigned_integer (pc, 2) == 0x1b97;
417 pc += 2, cache->locals += 2);
426 /* Check whether PC points at code that saves registers on the stack.
427 If so, it updates CACHE and returns the address of the first
428 instruction after the register saves or CURRENT_PC, whichever is
429 smaller. Otherwise, return PC. */
432 h8300_analyze_register_saves (CORE_ADDR pc, CORE_ADDR current_pc,
433 struct h8300_frame_cache *cache)
435 if (cache->locals >= 0)
441 offset = -cache->locals;
442 while (pc < current_pc)
444 op = read_memory_unsigned_integer (pc, 2);
445 if ((op & 0xfff0) == 0x6df0)
449 cache->saved_regs[regno] = offset;
453 else if (op == 0x0100)
455 op = read_memory_unsigned_integer (pc + 2, 2);
456 if ((op & 0xfff0) == 0x6df0)
460 cache->saved_regs[regno] = offset;
467 else if ((op & 0xffcf) == 0x0100)
470 op1 = read_memory_unsigned_integer (pc + 2, 2);
471 if ((op1 & 0xfff0) == 0x6df0)
473 /* stm.l reglist,@-sp */
474 i = ((op & 0x0030) >> 4) + 1;
475 regno = op1 & 0x000f;
476 for (; i > 0; regno++, --i)
478 cache->saved_regs[regno] = offset;
494 /* Do a full analysis of the prologue at PC and update CACHE
495 accordingly. Bail out early if CURRENT_PC is reached. Return the
496 address where the analysis stopped.
498 We handle all cases that can be generated by gcc.
500 For allocating a stack frame:
521 For saving registers:
527 For setting up the PIC register:
529 Future equivalence...
534 h8300_analyze_prologue (CORE_ADDR pc, CORE_ADDR current_pc,
535 struct h8300_frame_cache *cache)
539 pc = h8300_analyze_frame_setup (pc, current_pc, cache);
540 pc = h8300_analyze_register_saves (pc, current_pc, cache);
541 if (pc >= current_pc)
549 static struct h8300_frame_cache *
550 h8300_frame_cache (struct frame_info *next_frame, void **this_cache)
552 struct h8300_frame_cache *cache;
559 cache = h8300_alloc_frame_cache ();
562 /* In principle, for normal frames, %fp holds the frame pointer,
563 which holds the base address for the current stack frame.
564 However, for functions that don't need it, the frame pointer is
565 optional. For these "frameless" functions the frame pointer is
566 actually the frame pointer of the calling frame. Signal
567 trampolines are just a special case of a "frameless" function.
568 They (usually) share their frame pointer with the frame that was
569 in progress when the signal occurred. */
571 frame_unwind_register (next_frame, E_FP_REGNUM, buf);
572 cache->base = extract_unsigned_integer (buf, 4);
573 if (cache->base == 0)
576 /* For normal frames, %pc is stored at 4(%fp). */
577 cache->saved_regs[E_PC_REGNUM] = 4;
579 cache->pc = frame_func_unwind (next_frame);
581 h8300_analyze_prologue (cache->pc, frame_pc_unwind (next_frame), cache);
583 if (cache->locals < 0)
585 /* We didn't find a valid frame, which means that CACHE->base
586 currently holds the frame pointer for our calling frame. If
587 we're at the start of a function, or somewhere half-way its
588 prologue, the function's frame probably hasn't been fully
589 setup yet. Try to reconstruct the base address for the stack
590 frame by looking at the stack pointer. For truly "frameless"
591 functions this might work too. */
593 frame_unwind_register (next_frame, E_SP_REGNUM, buf);
594 cache->base = extract_unsigned_integer (buf, 4) + cache->sp_offset;
597 /* Now that we have the base address for the stack frame we can
598 calculate the value of %sp in the calling frame. */
599 cache->saved_sp = cache->base;
601 /* Adjust all the saved registers such that they contain addresses
602 instead of offsets. */
603 for (i = 0; i < NUM_REGS; i++)
604 if (cache->saved_regs[i] != -1)
605 cache->saved_regs[i] += cache->base;
611 h8300_frame_this_id (struct frame_info *next_frame, void **this_cache,
612 struct frame_id *this_id)
614 struct h8300_frame_cache *cache =
615 h8300_frame_cache (next_frame, this_cache);
617 /* This marks the outermost frame. */
618 if (cache->base == 0)
621 /* See the end of m68k_push_dummy_call. */
622 *this_id = frame_id_build (cache->base, cache->pc);
626 h8300_frame_prev_register (struct frame_info *next_frame, void **this_cache,
627 int regnum, int *optimizedp,
628 enum lval_type *lvalp, CORE_ADDR *addrp,
629 int *realnump, void *valuep)
631 struct h8300_frame_cache *cache =
632 h8300_frame_cache (next_frame, this_cache);
634 gdb_assert (regnum >= 0);
636 if (regnum == E_SP_REGNUM && cache->saved_sp)
644 /* Store the value. */
645 store_unsigned_integer (valuep, 4, cache->saved_sp);
650 if (regnum < NUM_REGS && cache->saved_regs[regnum] != -1)
653 *lvalp = lval_memory;
654 *addrp = cache->saved_regs[regnum];
658 /* Read the value in from memory. */
659 read_memory (*addrp, valuep,
660 register_size (current_gdbarch, regnum));
665 frame_register_unwind (next_frame, regnum,
666 optimizedp, lvalp, addrp, realnump, valuep);
669 static const struct frame_unwind h8300_frame_unwind = {
672 h8300_frame_prev_register
675 static const struct frame_unwind *
676 h8300_frame_sniffer (struct frame_info *next_frame)
678 return &h8300_frame_unwind;
681 /* Function: push_dummy_call
682 Setup the function arguments for calling a function in the inferior.
683 In this discussion, a `word' is 16 bits on the H8/300s, and 32 bits
686 There are actually two ABI's here: -mquickcall (the default) and
687 -mno-quickcall. With -mno-quickcall, all arguments are passed on
688 the stack after the return address, word-aligned. With
689 -mquickcall, GCC tries to use r0 -- r2 to pass registers. Since
690 GCC doesn't indicate in the object file which ABI was used to
691 compile it, GDB only supports the default --- -mquickcall.
693 Here are the rules for -mquickcall, in detail:
695 Each argument, whether scalar or aggregate, is padded to occupy a
696 whole number of words. Arguments smaller than a word are padded at
697 the most significant end; those larger than a word are padded at
698 the least significant end.
700 The initial arguments are passed in r0 -- r2. Earlier arguments go in
701 lower-numbered registers. Multi-word arguments are passed in
702 consecutive registers, with the most significant end in the
703 lower-numbered register.
705 If an argument doesn't fit entirely in the remaining registers, it
706 is passed entirely on the stack. Stack arguments begin just after
707 the return address. Once an argument has overflowed onto the stack
708 this way, all subsequent arguments are passed on the stack.
710 The above rule has odd consequences. For example, on the h8/300s,
711 if a function takes two longs and an int as arguments:
712 - the first long will be passed in r0/r1,
713 - the second long will be passed entirely on the stack, since it
715 - and the int will be passed on the stack, even though it could fit
718 A weird exception: if an argument is larger than a word, but not a
719 whole number of words in length (before padding), it is passed on
720 the stack following the rules for stack arguments above, even if
721 there are sufficient registers available to hold it. Stranger
722 still, the argument registers are still `used up' --- even though
723 there's nothing in them.
725 So, for example, on the h8/300s, if a function expects a three-byte
726 structure and an int, the structure will go on the stack, and the
727 int will go in r2, not r0.
729 If the function returns an aggregate type (struct, union, or class)
730 by value, the caller must allocate space to hold the return value,
731 and pass the callee a pointer to this space as an invisible first
734 For varargs functions, the last fixed argument and all the variable
735 arguments are always passed on the stack. This means that calls to
736 varargs functions don't work properly unless there is a prototype
739 Basically, this ABI is not good, for the following reasons:
740 - You can't call vararg functions properly unless a prototype is in scope.
741 - Structure passing is inconsistent, to no purpose I can see.
742 - It often wastes argument registers, of which there are only three
746 h8300_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
747 struct regcache *regcache, CORE_ADDR bp_addr,
748 int nargs, struct value **args, CORE_ADDR sp,
749 int struct_return, CORE_ADDR struct_addr)
751 int stack_alloc = 0, stack_offset = 0;
752 int wordsize = BINWORD;
753 int reg = E_ARG0_REGNUM;
756 /* First, make sure the stack is properly aligned. */
757 sp = align_down (sp, wordsize);
759 /* Now make sure there's space on the stack for the arguments. We
760 may over-allocate a little here, but that won't hurt anything. */
761 for (argument = 0; argument < nargs; argument++)
762 stack_alloc += align_up (TYPE_LENGTH (value_type (args[argument])),
766 /* Now load as many arguments as possible into registers, and push
767 the rest onto the stack.
768 If we're returning a structure by value, then we must pass a
769 pointer to the buffer for the return value as an invisible first
772 regcache_cooked_write_unsigned (regcache, reg++, struct_addr);
774 for (argument = 0; argument < nargs; argument++)
776 struct type *type = value_type (args[argument]);
777 int len = TYPE_LENGTH (type);
778 char *contents = (char *) value_contents (args[argument]);
780 /* Pad the argument appropriately. */
781 int padded_len = align_up (len, wordsize);
782 char *padded = alloca (padded_len);
784 memset (padded, 0, padded_len);
785 memcpy (len < wordsize ? padded + padded_len - len : padded,
788 /* Could the argument fit in the remaining registers? */
789 if (padded_len <= (E_ARGLAST_REGNUM - reg + 1) * wordsize)
791 /* Are we going to pass it on the stack anyway, for no good
793 if (len > wordsize && len % wordsize)
795 /* I feel so unclean. */
796 write_memory (sp + stack_offset, padded, padded_len);
797 stack_offset += padded_len;
799 /* That's right --- even though we passed the argument
800 on the stack, we consume the registers anyway! Love
802 reg += padded_len / wordsize;
806 /* Heavens to Betsy --- it's really going in registers!
807 It would be nice if we could use write_register_bytes
808 here, but on the h8/300s, there are gaps between
809 the registers in the register file. */
812 for (offset = 0; offset < padded_len; offset += wordsize)
814 ULONGEST word = extract_unsigned_integer (padded + offset,
816 regcache_cooked_write_unsigned (regcache, reg++, word);
822 /* It doesn't fit in registers! Onto the stack it goes. */
823 write_memory (sp + stack_offset, padded, padded_len);
824 stack_offset += padded_len;
826 /* Once one argument has spilled onto the stack, all
827 subsequent arguments go on the stack. */
828 reg = E_ARGLAST_REGNUM + 1;
832 /* Store return address. */
834 write_memory_unsigned_integer (sp, wordsize, bp_addr);
836 /* Update stack pointer. */
837 regcache_cooked_write_unsigned (regcache, E_SP_REGNUM, sp);
842 /* Function: extract_return_value
843 Figure out where in REGBUF the called function has left its return value.
844 Copy that into VALBUF. Be sure to account for CPU type. */
847 h8300_extract_return_value (struct type *type, struct regcache *regcache,
850 int len = TYPE_LENGTH (type);
857 regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &c);
858 store_unsigned_integer (valbuf, len, c);
860 case 4: /* Needs two registers on plain H8/300 */
861 regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &c);
862 store_unsigned_integer (valbuf, 2, c);
863 regcache_cooked_read_unsigned (regcache, E_RET1_REGNUM, &c);
864 store_unsigned_integer ((void *) ((char *) valbuf + 2), 2, c);
866 case 8: /* long long is now 8 bytes. */
867 if (TYPE_CODE (type) == TYPE_CODE_INT)
869 regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &addr);
870 c = read_memory_unsigned_integer ((CORE_ADDR) addr, len);
871 store_unsigned_integer (valbuf, len, c);
875 error ("I don't know how this 8 byte value is returned.");
882 h8300h_extract_return_value (struct type *type, struct regcache *regcache,
885 int len = TYPE_LENGTH (type);
893 regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &c);
894 store_unsigned_integer (valbuf, len, c);
896 case 8: /* long long is now 8 bytes. */
897 if (TYPE_CODE (type) == TYPE_CODE_INT)
899 regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &addr);
900 c = read_memory_unsigned_integer ((CORE_ADDR) addr, len);
901 store_unsigned_integer (valbuf, len, c);
905 error ("I don't know how this 8 byte value is returned.");
912 /* Function: store_return_value
913 Place the appropriate value in the appropriate registers.
914 Primarily used by the RETURN command. */
917 h8300_store_return_value (struct type *type, struct regcache *regcache,
920 int len = TYPE_LENGTH (type);
926 case 2: /* short... */
927 val = extract_unsigned_integer (valbuf, len);
928 regcache_cooked_write_unsigned (regcache, E_RET0_REGNUM, val);
930 case 4: /* long, float */
931 val = extract_unsigned_integer (valbuf, len);
932 regcache_cooked_write_unsigned (regcache, E_RET0_REGNUM,
933 (val >> 16) & 0xffff);
934 regcache_cooked_write_unsigned (regcache, E_RET1_REGNUM, val & 0xffff);
936 case 8: /* long long, double and long double are all defined
937 as 4 byte types so far so this shouldn't happen. */
938 error ("I don't know how to return an 8 byte value.");
944 h8300h_store_return_value (struct type *type, struct regcache *regcache,
947 int len = TYPE_LENGTH (type);
954 case 4: /* long, float */
955 val = extract_unsigned_integer (valbuf, len);
956 regcache_cooked_write_unsigned (regcache, E_RET0_REGNUM, val);
958 case 8: /* long long, double and long double are all defined
959 as 4 byte types so far so this shouldn't happen. */
960 error ("I don't know how to return an 8 byte value.");
965 static struct cmd_list_element *setmachinelist;
968 h8300_register_name (int regno)
970 /* The register names change depending on which h8300 processor
972 static char *register_names[] = {
973 "r0", "r1", "r2", "r3", "r4", "r5", "r6",
974 "sp", "", "pc", "cycles", "tick", "inst",
975 "ccr", /* pseudo register */
978 || regno >= (sizeof (register_names) / sizeof (*register_names)))
979 internal_error (__FILE__, __LINE__,
980 "h8300_register_name: illegal register number %d", regno);
982 return register_names[regno];
986 h8300s_register_name (int regno)
988 static char *register_names[] = {
989 "er0", "er1", "er2", "er3", "er4", "er5", "er6",
990 "sp", "", "pc", "cycles", "", "tick", "inst",
992 "ccr", "exr" /* pseudo registers */
995 || regno >= (sizeof (register_names) / sizeof (*register_names)))
996 internal_error (__FILE__, __LINE__,
997 "h8300s_register_name: illegal register number %d",
1000 return register_names[regno];
1004 h8300sx_register_name (int regno)
1006 static char *register_names[] = {
1007 "er0", "er1", "er2", "er3", "er4", "er5", "er6",
1008 "sp", "", "pc", "cycles", "", "tick", "inst",
1009 "mach", "macl", "sbr", "vbr",
1010 "ccr", "exr" /* pseudo registers */
1013 || regno >= (sizeof (register_names) / sizeof (*register_names)))
1014 internal_error (__FILE__, __LINE__,
1015 "h8300sx_register_name: illegal register number %d",
1018 return register_names[regno];
1022 h8300_print_register (struct gdbarch *gdbarch, struct ui_file *file,
1023 struct frame_info *frame, int regno)
1026 const char *name = gdbarch_register_name (gdbarch, regno);
1028 if (!name || !*name)
1031 rval = get_frame_register_signed (frame, regno);
1033 fprintf_filtered (file, "%-14s ", name);
1034 if ((regno == E_PSEUDO_CCR_REGNUM) || \
1035 (regno == E_PSEUDO_EXR_REGNUM && is_h8300smode (current_gdbarch)))
1037 fprintf_filtered (file, "0x%02x ", (unsigned char) rval);
1038 print_longest (file, 'u', 1, rval);
1042 fprintf_filtered (file, "0x%s ", phex ((ULONGEST) rval, BINWORD));
1043 print_longest (file, 'd', 1, rval);
1045 if (regno == E_PSEUDO_CCR_REGNUM)
1049 unsigned char l = rval & 0xff;
1050 fprintf_filtered (file, "\t");
1051 fprintf_filtered (file, "I-%d ", (l & 0x80) != 0);
1052 fprintf_filtered (file, "UI-%d ", (l & 0x40) != 0);
1053 fprintf_filtered (file, "H-%d ", (l & 0x20) != 0);
1054 fprintf_filtered (file, "U-%d ", (l & 0x10) != 0);
1059 fprintf_filtered (file, "N-%d ", N);
1060 fprintf_filtered (file, "Z-%d ", Z);
1061 fprintf_filtered (file, "V-%d ", V);
1062 fprintf_filtered (file, "C-%d ", C);
1064 fprintf_filtered (file, "u> ");
1066 fprintf_filtered (file, "u<= ");
1068 fprintf_filtered (file, "u>= ");
1070 fprintf_filtered (file, "u< ");
1072 fprintf_filtered (file, "!= ");
1074 fprintf_filtered (file, "== ");
1076 fprintf_filtered (file, ">= ");
1078 fprintf_filtered (file, "< ");
1079 if ((Z | (N ^ V)) == 0)
1080 fprintf_filtered (file, "> ");
1081 if ((Z | (N ^ V)) == 1)
1082 fprintf_filtered (file, "<= ");
1084 else if (regno == E_PSEUDO_EXR_REGNUM && is_h8300smode (current_gdbarch))
1087 unsigned char l = rval & 0xff;
1088 fprintf_filtered (file, "\t");
1089 fprintf_filtered (file, "T-%d - - - ", (l & 0x80) != 0);
1090 fprintf_filtered (file, "I2-%d ", (l & 4) != 0);
1091 fprintf_filtered (file, "I1-%d ", (l & 2) != 0);
1092 fprintf_filtered (file, "I0-%d", (l & 1) != 0);
1094 fprintf_filtered (file, "\n");
1098 h8300_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
1099 struct frame_info *frame, int regno, int cpregs)
1103 for (regno = E_R0_REGNUM; regno <= E_SP_REGNUM; ++regno)
1104 h8300_print_register (gdbarch, file, frame, regno);
1105 h8300_print_register (gdbarch, file, frame, E_PSEUDO_CCR_REGNUM);
1106 h8300_print_register (gdbarch, file, frame, E_PC_REGNUM);
1107 if (is_h8300smode (current_gdbarch))
1109 h8300_print_register (gdbarch, file, frame, E_PSEUDO_EXR_REGNUM);
1110 if (is_h8300sxmode (current_gdbarch))
1112 h8300_print_register (gdbarch, file, frame, E_SBR_REGNUM);
1113 h8300_print_register (gdbarch, file, frame, E_VBR_REGNUM);
1115 h8300_print_register (gdbarch, file, frame, E_MACH_REGNUM);
1116 h8300_print_register (gdbarch, file, frame, E_MACL_REGNUM);
1117 h8300_print_register (gdbarch, file, frame, E_CYCLES_REGNUM);
1118 h8300_print_register (gdbarch, file, frame, E_TICKS_REGNUM);
1119 h8300_print_register (gdbarch, file, frame, E_INSTS_REGNUM);
1123 h8300_print_register (gdbarch, file, frame, E_CYCLES_REGNUM);
1124 h8300_print_register (gdbarch, file, frame, E_TICK_REGNUM);
1125 h8300_print_register (gdbarch, file, frame, E_INST_REGNUM);
1130 if (regno == E_CCR_REGNUM)
1131 h8300_print_register (gdbarch, file, frame, E_PSEUDO_CCR_REGNUM);
1132 else if (regno == E_PSEUDO_EXR_REGNUM
1133 && is_h8300smode (current_gdbarch))
1134 h8300_print_register (gdbarch, file, frame, E_PSEUDO_EXR_REGNUM);
1136 h8300_print_register (gdbarch, file, frame, regno);
1140 static struct type *
1141 h8300_register_type (struct gdbarch *gdbarch, int regno)
1143 if (regno < 0 || regno >= NUM_REGS + NUM_PSEUDO_REGS)
1144 internal_error (__FILE__, __LINE__,
1145 "h8300_register_type: illegal register number %d", regno);
1151 return builtin_type_void_func_ptr;
1154 return builtin_type_void_data_ptr;
1156 if (regno == E_PSEUDO_CCR_REGNUM)
1157 return builtin_type_uint8;
1158 else if (regno == E_PSEUDO_EXR_REGNUM)
1159 return builtin_type_uint8;
1160 else if (is_h8300hmode (current_gdbarch))
1161 return builtin_type_int32;
1163 return builtin_type_int16;
1169 h8300_pseudo_register_read (struct gdbarch *gdbarch,
1170 struct regcache *regcache, int regno, void *buf)
1172 if (regno == E_PSEUDO_CCR_REGNUM)
1173 regcache_raw_read (regcache, E_CCR_REGNUM, buf);
1174 else if (regno == E_PSEUDO_EXR_REGNUM)
1175 regcache_raw_read (regcache, E_EXR_REGNUM, buf);
1177 regcache_raw_read (regcache, regno, buf);
1181 h8300_pseudo_register_write (struct gdbarch *gdbarch,
1182 struct regcache *regcache, int regno,
1185 if (regno == E_PSEUDO_CCR_REGNUM)
1186 regcache_raw_write (regcache, E_CCR_REGNUM, buf);
1187 else if (regno == E_PSEUDO_EXR_REGNUM)
1188 regcache_raw_write (regcache, E_EXR_REGNUM, buf);
1190 regcache_raw_write (regcache, regno, buf);
1194 h8300_dbg_reg_to_regnum (int regno)
1196 if (regno == E_CCR_REGNUM)
1197 return E_PSEUDO_CCR_REGNUM;
1202 h8300s_dbg_reg_to_regnum (int regno)
1204 if (regno == E_CCR_REGNUM)
1205 return E_PSEUDO_CCR_REGNUM;
1206 if (regno == E_EXR_REGNUM)
1207 return E_PSEUDO_EXR_REGNUM;
1212 h8300_extract_struct_value_address (struct regcache *regcache)
1215 regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &addr);
1219 const static unsigned char *
1220 h8300_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
1222 /*static unsigned char breakpoint[] = { 0x7A, 0xFF }; *//* ??? */
1223 static unsigned char breakpoint[] = { 0x01, 0x80 }; /* Sleep */
1225 *lenptr = sizeof (breakpoint);
1230 h8300_push_dummy_code (struct gdbarch *gdbarch,
1231 CORE_ADDR sp, CORE_ADDR funaddr, int using_gcc,
1232 struct value **args, int nargs,
1233 struct type *value_type,
1234 CORE_ADDR *real_pc, CORE_ADDR *bp_addr)
1236 /* Allocate space sufficient for a breakpoint. */
1238 /* Store the address of that breakpoint */
1240 /* h8300 always starts the call at the callee's entry point. */
1246 h8300_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
1247 struct frame_info *frame, const char *args)
1249 fprintf_filtered (file, "\
1250 No floating-point info available for this processor.\n");
1253 static struct gdbarch *
1254 h8300_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1256 struct gdbarch_tdep *tdep = NULL;
1257 struct gdbarch *gdbarch;
1259 arches = gdbarch_list_lookup_by_info (arches, &info);
1261 return arches->gdbarch;
1264 tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
1267 if (info.bfd_arch_info->arch != bfd_arch_h8300)
1270 gdbarch = gdbarch_alloc (&info, 0);
1272 switch (info.bfd_arch_info->mach)
1274 case bfd_mach_h8300:
1275 set_gdbarch_num_regs (gdbarch, 13);
1276 set_gdbarch_num_pseudo_regs (gdbarch, 1);
1277 set_gdbarch_ecoff_reg_to_regnum (gdbarch, h8300_dbg_reg_to_regnum);
1278 set_gdbarch_dwarf_reg_to_regnum (gdbarch, h8300_dbg_reg_to_regnum);
1279 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, h8300_dbg_reg_to_regnum);
1280 set_gdbarch_stab_reg_to_regnum (gdbarch, h8300_dbg_reg_to_regnum);
1281 set_gdbarch_register_name (gdbarch, h8300_register_name);
1282 set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1283 set_gdbarch_addr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1284 set_gdbarch_extract_return_value (gdbarch, h8300_extract_return_value);
1285 set_gdbarch_store_return_value (gdbarch, h8300_store_return_value);
1286 set_gdbarch_print_insn (gdbarch, print_insn_h8300);
1288 case bfd_mach_h8300h:
1289 case bfd_mach_h8300hn:
1290 set_gdbarch_num_regs (gdbarch, 13);
1291 set_gdbarch_num_pseudo_regs (gdbarch, 1);
1292 set_gdbarch_ecoff_reg_to_regnum (gdbarch, h8300_dbg_reg_to_regnum);
1293 set_gdbarch_dwarf_reg_to_regnum (gdbarch, h8300_dbg_reg_to_regnum);
1294 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, h8300_dbg_reg_to_regnum);
1295 set_gdbarch_stab_reg_to_regnum (gdbarch, h8300_dbg_reg_to_regnum);
1296 set_gdbarch_register_name (gdbarch, h8300_register_name);
1297 if (info.bfd_arch_info->mach != bfd_mach_h8300hn)
1299 set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1300 set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1304 set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1305 set_gdbarch_addr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1307 set_gdbarch_extract_return_value (gdbarch, h8300h_extract_return_value);
1308 set_gdbarch_store_return_value (gdbarch, h8300h_store_return_value);
1309 set_gdbarch_print_insn (gdbarch, print_insn_h8300h);
1311 case bfd_mach_h8300s:
1312 case bfd_mach_h8300sn:
1313 set_gdbarch_num_regs (gdbarch, 16);
1314 set_gdbarch_num_pseudo_regs (gdbarch, 2);
1315 set_gdbarch_ecoff_reg_to_regnum (gdbarch, h8300s_dbg_reg_to_regnum);
1316 set_gdbarch_dwarf_reg_to_regnum (gdbarch, h8300s_dbg_reg_to_regnum);
1317 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, h8300s_dbg_reg_to_regnum);
1318 set_gdbarch_stab_reg_to_regnum (gdbarch, h8300s_dbg_reg_to_regnum);
1319 set_gdbarch_register_name (gdbarch, h8300s_register_name);
1320 if (info.bfd_arch_info->mach != bfd_mach_h8300sn)
1322 set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1323 set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1327 set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1328 set_gdbarch_addr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1330 set_gdbarch_extract_return_value (gdbarch, h8300h_extract_return_value);
1331 set_gdbarch_store_return_value (gdbarch, h8300h_store_return_value);
1332 set_gdbarch_print_insn (gdbarch, print_insn_h8300s);
1334 case bfd_mach_h8300sx:
1335 case bfd_mach_h8300sxn:
1336 set_gdbarch_num_regs (gdbarch, 18);
1337 set_gdbarch_num_pseudo_regs (gdbarch, 2);
1338 set_gdbarch_ecoff_reg_to_regnum (gdbarch, h8300s_dbg_reg_to_regnum);
1339 set_gdbarch_dwarf_reg_to_regnum (gdbarch, h8300s_dbg_reg_to_regnum);
1340 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, h8300s_dbg_reg_to_regnum);
1341 set_gdbarch_stab_reg_to_regnum (gdbarch, h8300s_dbg_reg_to_regnum);
1342 set_gdbarch_register_name (gdbarch, h8300sx_register_name);
1343 if (info.bfd_arch_info->mach != bfd_mach_h8300sxn)
1345 set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1346 set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1350 set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1351 set_gdbarch_addr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1353 set_gdbarch_extract_return_value (gdbarch, h8300h_extract_return_value);
1354 set_gdbarch_store_return_value (gdbarch, h8300h_store_return_value);
1355 set_gdbarch_print_insn (gdbarch, print_insn_h8300s);
1359 set_gdbarch_pseudo_register_read (gdbarch, h8300_pseudo_register_read);
1360 set_gdbarch_pseudo_register_write (gdbarch, h8300_pseudo_register_write);
1363 * Basic register fields and methods.
1366 set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);
1367 set_gdbarch_deprecated_fp_regnum (gdbarch, E_FP_REGNUM);
1368 set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);
1369 set_gdbarch_register_type (gdbarch, h8300_register_type);
1370 set_gdbarch_print_registers_info (gdbarch, h8300_print_registers_info);
1371 set_gdbarch_print_float_info (gdbarch, h8300_print_float_info);
1376 set_gdbarch_skip_prologue (gdbarch, h8300_skip_prologue);
1378 /* Frame unwinder. */
1379 set_gdbarch_unwind_dummy_id (gdbarch, h8300_unwind_dummy_id);
1380 set_gdbarch_unwind_pc (gdbarch, h8300_unwind_pc);
1382 /* Hook in the DWARF CFI frame unwinder. */
1383 frame_unwind_append_sniffer (gdbarch, dwarf2_frame_sniffer);
1388 /* Stack grows up. */
1389 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1391 set_gdbarch_deprecated_extract_struct_value_address (gdbarch,
1392 h8300_extract_struct_value_address);
1393 set_gdbarch_deprecated_use_struct_convention (gdbarch,
1394 always_use_struct_convention);
1395 set_gdbarch_breakpoint_from_pc (gdbarch, h8300_breakpoint_from_pc);
1396 set_gdbarch_push_dummy_code (gdbarch, h8300_push_dummy_code);
1397 set_gdbarch_push_dummy_call (gdbarch, h8300_push_dummy_call);
1399 set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1400 set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1401 set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1402 set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1403 set_gdbarch_long_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1405 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
1407 /* Char is unsigned. */
1408 set_gdbarch_char_signed (gdbarch, 0);
1410 frame_unwind_append_sniffer (gdbarch, h8300_frame_sniffer);
1416 extern initialize_file_ftype _initialize_h8300_tdep; /* -Wmissing-prototypes */
1419 _initialize_h8300_tdep (void)
1421 register_gdbarch_init (bfd_arch_h8300, h8300_gdbarch_init);
1425 is_h8300hmode (struct gdbarch *gdbarch)
1427 return gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sx
1428 || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sxn
1429 || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300s
1430 || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sn
1431 || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300h
1432 || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300hn;
1436 is_h8300smode (struct gdbarch *gdbarch)
1438 return gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sx
1439 || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sxn
1440 || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300s
1441 || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sn;
1445 is_h8300sxmode (struct gdbarch *gdbarch)
1447 return gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sx
1448 || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sxn;
1452 is_h8300_normal_mode (struct gdbarch *gdbarch)
1454 return gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sxn
1455 || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sn
1456 || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300hn;