1 /* Target-dependent code for Motorola 68HC11 & 68HC12
2 Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 Contributed by Stephane Carrez, stcarrez@nerim.fr
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #include "gdb_string.h"
34 #include "arch-utils.h"
38 #include "opcode/m68hc11.h"
39 #include "elf/m68hc11.h"
42 /* Macros for setting and testing a bit in a minimal symbol.
43 For 68HC11/68HC12 we have two flags that tell which return
44 type the function is using. This is used for prologue and frame
45 analysis to compute correct stack frame layout.
47 The MSB of the minimal symbol's "info" field is used for this purpose.
48 This field is already being used to store the symbol size, so the
49 assumption is that the symbol size cannot exceed 2^30.
51 MSYMBOL_SET_RTC Actually sets the "RTC" bit.
52 MSYMBOL_SET_RTI Actually sets the "RTI" bit.
53 MSYMBOL_IS_RTC Tests the "RTC" bit in a minimal symbol.
54 MSYMBOL_IS_RTI Tests the "RTC" bit in a minimal symbol.
55 MSYMBOL_SIZE Returns the size of the minimal symbol,
56 i.e. the "info" field with the "special" bit
59 #define MSYMBOL_SET_RTC(msym) \
60 MSYMBOL_INFO (msym) = (char *) (((long) MSYMBOL_INFO (msym)) \
63 #define MSYMBOL_SET_RTI(msym) \
64 MSYMBOL_INFO (msym) = (char *) (((long) MSYMBOL_INFO (msym)) \
67 #define MSYMBOL_IS_RTC(msym) \
68 (((long) MSYMBOL_INFO (msym) & 0x80000000) != 0)
70 #define MSYMBOL_IS_RTI(msym) \
71 (((long) MSYMBOL_INFO (msym) & 0x40000000) != 0)
73 #define MSYMBOL_SIZE(msym) \
74 ((long) MSYMBOL_INFO (msym) & 0x3fffffff)
76 enum insn_return_kind
{
83 /* Register numbers of various important registers.
84 Note that some of these values are "real" register numbers,
85 and correspond to the general registers of the machine,
86 and some are "phony" register numbers which are too large
87 to be actual register numbers as far as the user is concerned
88 but do serve to get the desired values when passed to read_register. */
90 #define HARD_X_REGNUM 0
91 #define HARD_D_REGNUM 1
92 #define HARD_Y_REGNUM 2
93 #define HARD_SP_REGNUM 3
94 #define HARD_PC_REGNUM 4
96 #define HARD_A_REGNUM 5
97 #define HARD_B_REGNUM 6
98 #define HARD_CCR_REGNUM 7
100 /* 68HC12 page number register.
101 Note: to keep a compatibility with gcc register naming, we must
102 not have to rename FP and other soft registers. The page register
103 is a real hard register and must therefore be counted by NUM_REGS.
104 For this it has the same number as Z register (which is not used). */
105 #define HARD_PAGE_REGNUM 8
106 #define M68HC11_LAST_HARD_REG (HARD_PAGE_REGNUM)
108 /* Z is replaced by X or Y by gcc during machine reorg.
109 ??? There is no way to get it and even know whether
110 it's in X or Y or in ZS. */
111 #define SOFT_Z_REGNUM 8
113 /* Soft registers. These registers are special. There are treated
114 like normal hard registers by gcc and gdb (ie, within dwarf2 info).
115 They are physically located in memory. */
116 #define SOFT_FP_REGNUM 9
117 #define SOFT_TMP_REGNUM 10
118 #define SOFT_ZS_REGNUM 11
119 #define SOFT_XY_REGNUM 12
120 #define SOFT_UNUSED_REGNUM 13
121 #define SOFT_D1_REGNUM 14
122 #define SOFT_D32_REGNUM (SOFT_D1_REGNUM+31)
123 #define M68HC11_MAX_SOFT_REGS 32
125 #define M68HC11_NUM_REGS (8)
126 #define M68HC11_NUM_PSEUDO_REGS (M68HC11_MAX_SOFT_REGS+5)
127 #define M68HC11_ALL_REGS (M68HC11_NUM_REGS+M68HC11_NUM_PSEUDO_REGS)
129 #define M68HC11_REG_SIZE (2)
131 #define M68HC12_NUM_REGS (9)
132 #define M68HC12_NUM_PSEUDO_REGS ((M68HC11_MAX_SOFT_REGS+5)+1-1)
133 #define M68HC12_HARD_PC_REGNUM (SOFT_D32_REGNUM+1)
135 struct insn_sequence
;
138 /* Stack pointer correction value. For 68hc11, the stack pointer points
139 to the next push location. An offset of 1 must be applied to obtain
140 the address where the last value is saved. For 68hc12, the stack
141 pointer points to the last value pushed. No offset is necessary. */
142 int stack_correction
;
144 /* Description of instructions in the prologue. */
145 struct insn_sequence
*prologue
;
147 /* True if the page memory bank register is available
149 int use_page_register
;
151 /* ELF flags for ABI. */
155 #define M6811_TDEP gdbarch_tdep (current_gdbarch)
156 #define STACK_CORRECTION (M6811_TDEP->stack_correction)
157 #define USE_PAGE_REGISTER (M6811_TDEP->use_page_register)
159 struct frame_extra_info
164 enum insn_return_kind return_kind
;
167 /* Table of registers for 68HC11. This includes the hard registers
168 and the soft registers used by GCC. */
170 m68hc11_register_names
[] =
172 "x", "d", "y", "sp", "pc", "a", "b",
173 "ccr", "page", "frame","tmp", "zs", "xy", 0,
174 "d1", "d2", "d3", "d4", "d5", "d6", "d7",
175 "d8", "d9", "d10", "d11", "d12", "d13", "d14",
176 "d15", "d16", "d17", "d18", "d19", "d20", "d21",
177 "d22", "d23", "d24", "d25", "d26", "d27", "d28",
178 "d29", "d30", "d31", "d32"
181 struct m68hc11_soft_reg
187 static struct m68hc11_soft_reg soft_regs
[M68HC11_ALL_REGS
];
189 #define M68HC11_FP_ADDR soft_regs[SOFT_FP_REGNUM].addr
191 static int soft_min_addr
;
192 static int soft_max_addr
;
193 static int soft_reg_initialized
= 0;
195 /* Look in the symbol table for the address of a pseudo register
196 in memory. If we don't find it, pretend the register is not used
197 and not available. */
199 m68hc11_get_register_info (struct m68hc11_soft_reg
*reg
, const char *name
)
201 struct minimal_symbol
*msymbol
;
203 msymbol
= lookup_minimal_symbol (name
, NULL
, NULL
);
206 reg
->addr
= SYMBOL_VALUE_ADDRESS (msymbol
);
207 reg
->name
= xstrdup (name
);
209 /* Keep track of the address range for soft registers. */
210 if (reg
->addr
< (CORE_ADDR
) soft_min_addr
)
211 soft_min_addr
= reg
->addr
;
212 if (reg
->addr
> (CORE_ADDR
) soft_max_addr
)
213 soft_max_addr
= reg
->addr
;
222 /* Initialize the table of soft register addresses according
223 to the symbol table. */
225 m68hc11_initialize_register_info (void)
229 if (soft_reg_initialized
)
232 soft_min_addr
= INT_MAX
;
234 for (i
= 0; i
< M68HC11_ALL_REGS
; i
++)
236 soft_regs
[i
].name
= 0;
239 m68hc11_get_register_info (&soft_regs
[SOFT_FP_REGNUM
], "_.frame");
240 m68hc11_get_register_info (&soft_regs
[SOFT_TMP_REGNUM
], "_.tmp");
241 m68hc11_get_register_info (&soft_regs
[SOFT_ZS_REGNUM
], "_.z");
242 soft_regs
[SOFT_Z_REGNUM
] = soft_regs
[SOFT_ZS_REGNUM
];
243 m68hc11_get_register_info (&soft_regs
[SOFT_XY_REGNUM
], "_.xy");
245 for (i
= SOFT_D1_REGNUM
; i
< M68HC11_MAX_SOFT_REGS
; i
++)
249 sprintf (buf
, "_.d%d", i
- SOFT_D1_REGNUM
+ 1);
250 m68hc11_get_register_info (&soft_regs
[i
], buf
);
253 if (soft_regs
[SOFT_FP_REGNUM
].name
== 0)
255 warning ("No frame soft register found in the symbol table.\n");
256 warning ("Stack backtrace will not work.\n");
258 soft_reg_initialized
= 1;
261 /* Given an address in memory, return the soft register number if
262 that address corresponds to a soft register. Returns -1 if not. */
264 m68hc11_which_soft_register (CORE_ADDR addr
)
268 if (addr
< soft_min_addr
|| addr
> soft_max_addr
)
271 for (i
= SOFT_FP_REGNUM
; i
< M68HC11_ALL_REGS
; i
++)
273 if (soft_regs
[i
].name
&& soft_regs
[i
].addr
== addr
)
279 /* Fetch a pseudo register. The 68hc11 soft registers are treated like
280 pseudo registers. They are located in memory. Translate the register
281 fetch into a memory read. */
283 m68hc11_pseudo_register_read (struct gdbarch
*gdbarch
,
284 struct regcache
*regcache
,
285 int regno
, void *buf
)
287 /* The PC is a pseudo reg only for 68HC12 with the memory bank
289 if (regno
== M68HC12_HARD_PC_REGNUM
)
291 const int regsize
= TYPE_LENGTH (builtin_type_uint32
);
292 CORE_ADDR pc
= read_register (HARD_PC_REGNUM
);
293 int page
= read_register (HARD_PAGE_REGNUM
);
295 if (pc
>= 0x8000 && pc
< 0xc000)
301 store_unsigned_integer (buf
, regsize
, pc
);
305 m68hc11_initialize_register_info ();
307 /* Fetch a soft register: translate into a memory read. */
308 if (soft_regs
[regno
].name
)
310 target_read_memory (soft_regs
[regno
].addr
, buf
, 2);
318 /* Store a pseudo register. Translate the register store
319 into a memory write. */
321 m68hc11_pseudo_register_write (struct gdbarch
*gdbarch
,
322 struct regcache
*regcache
,
323 int regno
, const void *buf
)
325 /* The PC is a pseudo reg only for 68HC12 with the memory bank
327 if (regno
== M68HC12_HARD_PC_REGNUM
)
329 const int regsize
= TYPE_LENGTH (builtin_type_uint32
);
330 char *tmp
= alloca (regsize
);
333 memcpy (tmp
, buf
, regsize
);
334 pc
= extract_unsigned_integer (tmp
, regsize
);
338 write_register (HARD_PAGE_REGNUM
, (pc
>> 14) & 0x0ff);
340 write_register (HARD_PC_REGNUM
, pc
+ 0x8000);
343 write_register (HARD_PC_REGNUM
, pc
);
347 m68hc11_initialize_register_info ();
349 /* Store a soft register: translate into a memory write. */
350 if (soft_regs
[regno
].name
)
352 const int regsize
= 2;
353 char *tmp
= alloca (regsize
);
354 memcpy (tmp
, buf
, regsize
);
355 target_write_memory (soft_regs
[regno
].addr
, tmp
, regsize
);
360 m68hc11_register_name (int reg_nr
)
362 if (reg_nr
== M68HC12_HARD_PC_REGNUM
&& USE_PAGE_REGISTER
)
364 if (reg_nr
== HARD_PC_REGNUM
&& USE_PAGE_REGISTER
)
369 if (reg_nr
>= M68HC11_ALL_REGS
)
372 /* If we don't know the address of a soft register, pretend it
374 if (reg_nr
> M68HC11_LAST_HARD_REG
&& soft_regs
[reg_nr
].name
== 0)
376 return m68hc11_register_names
[reg_nr
];
379 static const unsigned char *
380 m68hc11_breakpoint_from_pc (CORE_ADDR
*pcptr
, int *lenptr
)
382 static unsigned char breakpoint
[] = {0x0};
384 *lenptr
= sizeof (breakpoint
);
388 /* Immediately after a function call, return the saved pc before the frame
392 m68hc11_saved_pc_after_call (struct frame_info
*frame
)
396 addr
= read_register (HARD_SP_REGNUM
) + STACK_CORRECTION
;
398 return read_memory_integer (addr
, 2) & 0x0FFFF;
402 m68hc11_frame_saved_pc (struct frame_info
*frame
)
404 return frame
->extra_info
->return_pc
;
408 m68hc11_frame_args_address (struct frame_info
*frame
)
412 addr
= frame
->frame
+ frame
->extra_info
->size
+ STACK_CORRECTION
+ 2;
413 if (frame
->extra_info
->return_kind
== RETURN_RTC
)
415 else if (frame
->extra_info
->return_kind
== RETURN_RTI
)
422 m68hc11_frame_locals_address (struct frame_info
*frame
)
427 /* Discard from the stack the innermost frame, restoring all saved
431 m68hc11_pop_frame (void)
433 register struct frame_info
*frame
= get_current_frame ();
434 register CORE_ADDR fp
, sp
;
437 if (DEPRECATED_PC_IN_CALL_DUMMY (frame
->pc
, frame
->frame
, frame
->frame
))
438 generic_pop_dummy_frame ();
441 fp
= get_frame_base (frame
);
442 FRAME_INIT_SAVED_REGS (frame
);
444 /* Copy regs from where they were saved in the frame. */
445 for (regnum
= 0; regnum
< M68HC11_ALL_REGS
; regnum
++)
446 if (frame
->saved_regs
[regnum
])
447 write_register (regnum
,
448 read_memory_integer (frame
->saved_regs
[regnum
], 2));
450 write_register (HARD_PC_REGNUM
, frame
->extra_info
->return_pc
);
451 sp
= (fp
+ frame
->extra_info
->size
+ 2) & 0x0ffff;
452 write_register (HARD_SP_REGNUM
, sp
);
454 flush_cached_frames ();
458 /* 68HC11 & 68HC12 prologue analysis.
463 /* 68HC11 opcodes. */
464 #undef M6811_OP_PAGE2
465 #define M6811_OP_PAGE2 (0x18)
466 #define M6811_OP_LDX (0xde)
467 #define M6811_OP_PSHX (0x3c)
468 #define M6811_OP_STS (0x9f)
469 #define M6811_OP_TSX (0x30)
470 #define M6811_OP_XGDX (0x8f)
471 #define M6811_OP_ADDD (0xc3)
472 #define M6811_OP_TXS (0x35)
473 #define M6811_OP_DES (0x34)
475 /* 68HC12 opcodes. */
476 #define M6812_OP_PAGE2 (0x18)
477 #define M6812_OP_MOVW (0x01)
478 #define M6812_PB_PSHW (0xae)
479 #define M6812_OP_STS (0x7f)
480 #define M6812_OP_LEAS (0x1b)
481 #define M6812_OP_PSHX (0x34)
482 #define M6812_OP_PSHY (0x35)
484 /* Operand extraction. */
485 #define OP_DIRECT (0x100) /* 8-byte direct addressing. */
486 #define OP_IMM_LOW (0x200) /* Low part of 16-bit constant/address. */
487 #define OP_IMM_HIGH (0x300) /* High part of 16-bit constant/address. */
488 #define OP_PBYTE (0x400) /* 68HC12 indexed operand. */
490 /* Identification of the sequence. */
494 P_SAVE_REG
, /* Save a register on the stack. */
495 P_SET_FRAME
, /* Setup the frame pointer. */
496 P_LOCAL_1
, /* Allocate 1 byte for locals. */
497 P_LOCAL_2
, /* Allocate 2 bytes for locals. */
498 P_LOCAL_N
/* Allocate N bytes for locals. */
501 struct insn_sequence
{
502 enum m6811_seq_type type
;
504 unsigned short code
[MAX_CODES
];
507 /* Sequence of instructions in the 68HC11 function prologue. */
508 static struct insn_sequence m6811_prologue
[] = {
509 /* Sequences to save a soft-register. */
510 { P_SAVE_REG
, 3, { M6811_OP_LDX
, OP_DIRECT
,
512 { P_SAVE_REG
, 5, { M6811_OP_PAGE2
, M6811_OP_LDX
, OP_DIRECT
,
513 M6811_OP_PAGE2
, M6811_OP_PSHX
} },
515 /* Sequences to allocate local variables. */
516 { P_LOCAL_N
, 7, { M6811_OP_TSX
,
518 M6811_OP_ADDD
, OP_IMM_HIGH
, OP_IMM_LOW
,
521 { P_LOCAL_N
, 11, { M6811_OP_PAGE2
, M6811_OP_TSX
,
522 M6811_OP_PAGE2
, M6811_OP_XGDX
,
523 M6811_OP_ADDD
, OP_IMM_HIGH
, OP_IMM_LOW
,
524 M6811_OP_PAGE2
, M6811_OP_XGDX
,
525 M6811_OP_PAGE2
, M6811_OP_TXS
} },
526 { P_LOCAL_1
, 1, { M6811_OP_DES
} },
527 { P_LOCAL_2
, 1, { M6811_OP_PSHX
} },
528 { P_LOCAL_2
, 2, { M6811_OP_PAGE2
, M6811_OP_PSHX
} },
530 /* Initialize the frame pointer. */
531 { P_SET_FRAME
, 2, { M6811_OP_STS
, OP_DIRECT
} },
536 /* Sequence of instructions in the 68HC12 function prologue. */
537 static struct insn_sequence m6812_prologue
[] = {
538 { P_SAVE_REG
, 5, { M6812_OP_PAGE2
, M6812_OP_MOVW
, M6812_PB_PSHW
,
539 OP_IMM_HIGH
, OP_IMM_LOW
} },
540 { P_SET_FRAME
, 3, { M6812_OP_STS
, OP_IMM_HIGH
, OP_IMM_LOW
} },
541 { P_LOCAL_N
, 2, { M6812_OP_LEAS
, OP_PBYTE
} },
542 { P_LOCAL_2
, 1, { M6812_OP_PSHX
} },
543 { P_LOCAL_2
, 1, { M6812_OP_PSHY
} },
548 /* Analyze the sequence of instructions starting at the given address.
549 Returns a pointer to the sequence when it is recognized and
550 the optional value (constant/address) associated with it.
551 Advance the pc for the next sequence. */
552 static struct insn_sequence
*
553 m68hc11_analyze_instruction (struct insn_sequence
*seq
, CORE_ADDR
*pc
,
556 unsigned char buffer
[MAX_CODES
];
563 for (; seq
->type
!= P_LAST
; seq
++)
566 for (j
= 0; j
< seq
->length
; j
++)
570 buffer
[bufsize
] = read_memory_unsigned_integer (*pc
+ bufsize
,
574 /* Continue while we match the opcode. */
575 if (seq
->code
[j
] == buffer
[j
])
578 if ((seq
->code
[j
] & 0xf00) == 0)
581 /* Extract a sequence parameter (address or constant). */
582 switch (seq
->code
[j
])
585 cur_val
= (CORE_ADDR
) buffer
[j
];
589 cur_val
= cur_val
& 0x0ff;
590 cur_val
|= (buffer
[j
] << 8);
595 cur_val
|= buffer
[j
];
599 if ((buffer
[j
] & 0xE0) == 0x80)
601 v
= buffer
[j
] & 0x1f;
605 else if ((buffer
[j
] & 0xfe) == 0xf0)
607 v
= read_memory_unsigned_integer (*pc
+ j
+ 1, 1);
612 else if (buffer
[j
] == 0xf2)
614 v
= read_memory_unsigned_integer (*pc
+ j
+ 1, 2);
622 /* We have a full match. */
623 if (j
== seq
->length
)
633 /* Return the instruction that the function at the PC is using. */
634 static enum insn_return_kind
635 m68hc11_get_return_insn (CORE_ADDR pc
)
637 struct minimal_symbol
*sym
;
639 /* A flag indicating that this is a STO_M68HC12_FAR or STO_M68HC12_INTERRUPT
640 function is stored by elfread.c in the high bit of the info field.
641 Use this to decide which instruction the function uses to return. */
642 sym
= lookup_minimal_symbol_by_pc (pc
);
646 if (MSYMBOL_IS_RTC (sym
))
648 else if (MSYMBOL_IS_RTI (sym
))
655 /* Analyze the function prologue to find some information
657 - the PC of the first line (for m68hc11_skip_prologue)
658 - the offset of the previous frame saved address (from current frame)
659 - the soft registers which are pushed. */
661 m68hc11_guess_from_prologue (CORE_ADDR pc
, CORE_ADDR fp
,
662 CORE_ADDR
*first_line
,
663 int *frame_offset
, CORE_ADDR
*pushed_regs
)
668 int found_frame_point
;
672 struct insn_sequence
*seq_table
;
674 first_pc
= get_pc_function_start (pc
);
677 m68hc11_initialize_register_info ();
685 seq_table
= gdbarch_tdep (current_gdbarch
)->prologue
;
687 /* The 68hc11 stack is as follows:
703 +-----------+ <--- current frame
706 With most processors (like 68K) the previous frame can be computed
707 easily because it is always at a fixed offset (see link/unlink).
708 That is, locals are accessed with negative offsets, arguments are
709 accessed with positive ones. Since 68hc11 only supports offsets
710 in the range [0..255], the frame is defined at the bottom of
711 locals (see picture).
713 The purpose of the analysis made here is to find out the size
714 of locals in this function. An alternative to this is to use
715 DWARF2 info. This would be better but I don't know how to
716 access dwarf2 debug from this function.
718 Walk from the function entry point to the point where we save
719 the frame. While walking instructions, compute the size of bytes
720 which are pushed. This gives us the index to access the previous
723 We limit the search to 128 bytes so that the algorithm is bounded
724 in case of random and wrong code. We also stop and abort if
725 we find an instruction which is not supposed to appear in the
726 prologue (as generated by gcc 2.95, 2.96).
730 found_frame_point
= 0;
732 save_addr
= fp
+ STACK_CORRECTION
;
733 while (!done
&& pc
+ 2 < func_end
)
735 struct insn_sequence
*seq
;
738 seq
= m68hc11_analyze_instruction (seq_table
, &pc
, &val
);
742 if (seq
->type
== P_SAVE_REG
)
744 if (found_frame_point
)
746 saved_reg
= m68hc11_which_soft_register (val
);
752 pushed_regs
[saved_reg
] = save_addr
;
759 else if (seq
->type
== P_SET_FRAME
)
761 found_frame_point
= 1;
762 *frame_offset
= size
;
764 else if (seq
->type
== P_LOCAL_1
)
768 else if (seq
->type
== P_LOCAL_2
)
772 else if (seq
->type
== P_LOCAL_N
)
774 /* Stack pointer is decremented for the allocation. */
776 size
-= (int) (val
) | 0xffff0000;
785 m68hc11_skip_prologue (CORE_ADDR pc
)
787 CORE_ADDR func_addr
, func_end
;
788 struct symtab_and_line sal
;
791 /* If we have line debugging information, then the end of the
792 prologue should be the first assembly instruction of the
793 first source line. */
794 if (find_pc_partial_function (pc
, NULL
, &func_addr
, &func_end
))
796 sal
= find_pc_line (func_addr
, 0);
797 if (sal
.end
&& sal
.end
< func_end
)
801 m68hc11_guess_from_prologue (pc
, 0, &pc
, &frame_offset
, 0);
805 /* Given a GDB frame, determine the address of the calling function's frame.
806 This will be used to create a new GDB frame struct, and then
807 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
811 m68hc11_frame_chain (struct frame_info
*frame
)
815 if (DEPRECATED_PC_IN_CALL_DUMMY (frame
->pc
, frame
->frame
, frame
->frame
))
816 return frame
->frame
; /* dummy frame same as caller's frame */
818 if (frame
->extra_info
->return_pc
== 0
819 || inside_entry_file (frame
->extra_info
->return_pc
))
820 return (CORE_ADDR
) 0;
822 if (frame
->frame
== 0)
824 return (CORE_ADDR
) 0;
827 addr
= frame
->frame
+ frame
->extra_info
->size
+ STACK_CORRECTION
- 2;
828 addr
= read_memory_unsigned_integer (addr
, 2) & 0x0FFFF;
832 /* Put here the code to store, into a struct frame_saved_regs, the
833 addresses of the saved registers of frame described by FRAME_INFO.
834 This includes special registers such as pc and fp saved in special
835 ways in the stack frame. sp is even more special: the address we
836 return for it IS the sp for the next frame. */
838 m68hc11_frame_init_saved_regs (struct frame_info
*fi
)
843 if (fi
->saved_regs
== NULL
)
844 frame_saved_regs_zalloc (fi
);
846 memset (fi
->saved_regs
, 0, sizeof (fi
->saved_regs
));
849 fi
->extra_info
->return_kind
= m68hc11_get_return_insn (pc
);
850 m68hc11_guess_from_prologue (pc
, fi
->frame
, &pc
, &fi
->extra_info
->size
,
853 addr
= fi
->frame
+ fi
->extra_info
->size
+ STACK_CORRECTION
;
854 if (soft_regs
[SOFT_FP_REGNUM
].name
)
855 fi
->saved_regs
[SOFT_FP_REGNUM
] = addr
- 2;
857 /* Take into account how the function was called/returns. */
858 if (fi
->extra_info
->return_kind
== RETURN_RTC
)
860 fi
->saved_regs
[HARD_PAGE_REGNUM
] = addr
;
863 else if (fi
->extra_info
->return_kind
== RETURN_RTI
)
865 fi
->saved_regs
[HARD_CCR_REGNUM
] = addr
;
866 fi
->saved_regs
[HARD_D_REGNUM
] = addr
+ 1;
867 fi
->saved_regs
[HARD_X_REGNUM
] = addr
+ 3;
868 fi
->saved_regs
[HARD_Y_REGNUM
] = addr
+ 5;
871 fi
->saved_regs
[HARD_SP_REGNUM
] = addr
;
872 fi
->saved_regs
[HARD_PC_REGNUM
] = fi
->saved_regs
[HARD_SP_REGNUM
];
876 m68hc11_init_extra_frame_info (int fromleaf
, struct frame_info
*fi
)
880 fi
->extra_info
= (struct frame_extra_info
*)
881 frame_obstack_alloc (sizeof (struct frame_extra_info
));
884 fi
->pc
= FRAME_SAVED_PC (fi
->next
);
886 m68hc11_frame_init_saved_regs (fi
);
890 fi
->extra_info
->return_kind
= m68hc11_get_return_insn (fi
->pc
);
891 fi
->extra_info
->return_pc
= m68hc11_saved_pc_after_call (fi
);
895 addr
= fi
->saved_regs
[HARD_PC_REGNUM
];
896 addr
= read_memory_unsigned_integer (addr
, 2) & 0x0ffff;
898 /* Take into account the 68HC12 specific call (PC + page). */
899 if (fi
->extra_info
->return_kind
== RETURN_RTC
900 && addr
>= 0x08000 && addr
< 0x0c000
901 && USE_PAGE_REGISTER
)
903 CORE_ADDR page_addr
= fi
->saved_regs
[HARD_PAGE_REGNUM
];
905 unsigned page
= read_memory_unsigned_integer (page_addr
, 1);
907 addr
+= ((page
& 0x0ff) << 14);
910 fi
->extra_info
->return_pc
= addr
;
914 /* Same as 'info reg' but prints the registers in a different way. */
916 show_regs (char *args
, int from_tty
)
918 int ccr
= read_register (HARD_CCR_REGNUM
);
922 printf_filtered ("PC=%04x SP=%04x FP=%04x CCR=%02x %c%c%c%c%c%c%c%c\n",
923 (int) read_register (HARD_PC_REGNUM
),
924 (int) read_register (HARD_SP_REGNUM
),
925 (int) read_register (SOFT_FP_REGNUM
),
927 ccr
& M6811_S_BIT
? 'S' : '-',
928 ccr
& M6811_X_BIT
? 'X' : '-',
929 ccr
& M6811_H_BIT
? 'H' : '-',
930 ccr
& M6811_I_BIT
? 'I' : '-',
931 ccr
& M6811_N_BIT
? 'N' : '-',
932 ccr
& M6811_Z_BIT
? 'Z' : '-',
933 ccr
& M6811_V_BIT
? 'V' : '-',
934 ccr
& M6811_C_BIT
? 'C' : '-');
936 printf_filtered ("D=%04x IX=%04x IY=%04x",
937 (int) read_register (HARD_D_REGNUM
),
938 (int) read_register (HARD_X_REGNUM
),
939 (int) read_register (HARD_Y_REGNUM
));
941 if (USE_PAGE_REGISTER
)
943 printf_filtered (" Page=%02x",
944 (int) read_register (HARD_PAGE_REGNUM
));
946 printf_filtered ("\n");
949 for (i
= SOFT_D1_REGNUM
; i
< M68HC11_ALL_REGS
; i
++)
951 /* Skip registers which are not defined in the symbol table. */
952 if (soft_regs
[i
].name
== 0)
955 printf_filtered ("D%d=%04x",
956 i
- SOFT_D1_REGNUM
+ 1,
957 (int) read_register (i
));
960 printf_filtered ("\n");
962 printf_filtered (" ");
964 if (nr
&& (nr
% 8) != 7)
965 printf_filtered ("\n");
969 m68hc11_stack_align (CORE_ADDR addr
)
971 return ((addr
+ 1) & -2);
975 m68hc11_push_arguments (int nargs
,
979 CORE_ADDR struct_addr
)
983 int first_stack_argnum
;
990 first_stack_argnum
= 0;
993 /* The struct is allocated on the stack and gdb used the stack
994 pointer for the address of that struct. We must apply the
995 stack offset on the address. */
996 write_register (HARD_D_REGNUM
, struct_addr
+ STACK_CORRECTION
);
1000 type
= VALUE_TYPE (args
[0]);
1001 len
= TYPE_LENGTH (type
);
1003 /* First argument is passed in D and X registers. */
1006 LONGEST v
= extract_unsigned_integer (VALUE_CONTENTS (args
[0]), len
);
1007 first_stack_argnum
= 1;
1008 write_register (HARD_D_REGNUM
, v
);
1012 write_register (HARD_X_REGNUM
, v
);
1016 for (argnum
= first_stack_argnum
; argnum
< nargs
; argnum
++)
1018 type
= VALUE_TYPE (args
[argnum
]);
1019 stack_alloc
+= (TYPE_LENGTH (type
) + 1) & -2;
1023 stack_offset
= STACK_CORRECTION
;
1024 for (argnum
= first_stack_argnum
; argnum
< nargs
; argnum
++)
1026 type
= VALUE_TYPE (args
[argnum
]);
1027 len
= TYPE_LENGTH (type
);
1029 val
= (char*) VALUE_CONTENTS (args
[argnum
]);
1030 write_memory (sp
+ stack_offset
, val
, len
);
1031 stack_offset
+= len
;
1034 static char zero
= 0;
1036 write_memory (sp
+ stack_offset
, &zero
, 1);
1044 /* Return a location where we can set a breakpoint that will be hit
1045 when an inferior function call returns. */
1047 m68hc11_call_dummy_address (void)
1049 return entry_point_address ();
1052 static struct type
*
1053 m68hc11_register_virtual_type (int reg_nr
)
1057 case HARD_PAGE_REGNUM
:
1060 case HARD_CCR_REGNUM
:
1061 return builtin_type_uint8
;
1063 case M68HC12_HARD_PC_REGNUM
:
1064 return builtin_type_uint32
;
1067 return builtin_type_uint16
;
1072 m68hc11_store_struct_return (CORE_ADDR addr
, CORE_ADDR sp
)
1074 /* The struct address computed by gdb is on the stack.
1075 It uses the stack pointer so we must apply the stack
1076 correction offset. */
1077 write_register (HARD_D_REGNUM
, addr
+ STACK_CORRECTION
);
1081 m68hc11_store_return_value (struct type
*type
, char *valbuf
)
1085 len
= TYPE_LENGTH (type
);
1087 /* First argument is passed in D and X registers. */
1090 LONGEST v
= extract_unsigned_integer (valbuf
, len
);
1092 write_register (HARD_D_REGNUM
, v
);
1096 write_register (HARD_X_REGNUM
, v
);
1100 error ("return of value > 4 is not supported.");
1104 /* Given a return value in `regbuf' with a type `type',
1105 extract and copy its value into `valbuf'. */
1108 m68hc11_extract_return_value (struct type
*type
,
1112 int len
= TYPE_LENGTH (type
);
1117 memcpy (valbuf
, ®buf
[HARD_D_REGNUM
* 2 + 1], len
);
1121 memcpy (valbuf
, ®buf
[HARD_D_REGNUM
* 2], len
);
1125 memcpy (&valbuf
[0], ®buf
[HARD_X_REGNUM
* 2 + 1], 1);
1126 memcpy (&valbuf
[1], ®buf
[HARD_D_REGNUM
* 2], 2);
1130 memcpy (&valbuf
[0], ®buf
[HARD_X_REGNUM
* 2], 2);
1131 memcpy (&valbuf
[2], ®buf
[HARD_D_REGNUM
* 2], 2);
1135 error ("bad size for return value");
1139 /* Should call_function allocate stack space for a struct return? */
1141 m68hc11_use_struct_convention (int gcc_p
, struct type
*type
)
1143 return (TYPE_CODE (type
) == TYPE_CODE_STRUCT
1144 || TYPE_CODE (type
) == TYPE_CODE_UNION
1145 || TYPE_LENGTH (type
) > 4);
1149 m68hc11_return_value_on_stack (struct type
*type
)
1151 return TYPE_LENGTH (type
) > 4;
1154 /* Extract from an array REGBUF containing the (raw) register state
1155 the address in which a function should return its structure value,
1156 as a CORE_ADDR (or an expression that can be used as one). */
1158 m68hc11_extract_struct_value_address (char *regbuf
)
1160 return extract_address (®buf
[HARD_D_REGNUM
* 2],
1161 REGISTER_RAW_SIZE (HARD_D_REGNUM
));
1164 /* Function: push_return_address (pc)
1165 Set up the return address for the inferior function call.
1166 Needed for targets where we don't actually execute a JSR/BSR instruction */
1169 m68hc11_push_return_address (CORE_ADDR pc
, CORE_ADDR sp
)
1173 pc
= CALL_DUMMY_ADDRESS ();
1175 store_unsigned_integer (valbuf
, 2, pc
);
1176 write_memory (sp
+ STACK_CORRECTION
, valbuf
, 2);
1180 /* Test whether the ELF symbol corresponds to a function using rtc or
1184 m68hc11_elf_make_msymbol_special (asymbol
*sym
, struct minimal_symbol
*msym
)
1186 unsigned char flags
;
1188 flags
= ((elf_symbol_type
*)sym
)->internal_elf_sym
.st_other
;
1189 if (flags
& STO_M68HC12_FAR
)
1190 MSYMBOL_SET_RTC (msym
);
1191 if (flags
& STO_M68HC12_INTERRUPT
)
1192 MSYMBOL_SET_RTI (msym
);
1196 gdb_print_insn_m68hc11 (bfd_vma memaddr
, disassemble_info
*info
)
1198 if (TARGET_ARCHITECTURE
->arch
== bfd_arch_m68hc11
)
1199 return print_insn_m68hc11 (memaddr
, info
);
1201 return print_insn_m68hc12 (memaddr
, info
);
1204 static struct gdbarch
*
1205 m68hc11_gdbarch_init (struct gdbarch_info info
,
1206 struct gdbarch_list
*arches
)
1208 static LONGEST m68hc11_call_dummy_words
[] =
1210 struct gdbarch
*gdbarch
;
1211 struct gdbarch_tdep
*tdep
;
1214 soft_reg_initialized
= 0;
1216 /* Extract the elf_flags if available. */
1217 if (info
.abfd
!= NULL
1218 && bfd_get_flavour (info
.abfd
) == bfd_target_elf_flavour
)
1219 elf_flags
= elf_elfheader (info
.abfd
)->e_flags
;
1223 /* try to find a pre-existing architecture */
1224 for (arches
= gdbarch_list_lookup_by_info (arches
, &info
);
1226 arches
= gdbarch_list_lookup_by_info (arches
->next
, &info
))
1228 if (gdbarch_tdep (arches
->gdbarch
)->elf_flags
!= elf_flags
)
1231 return arches
->gdbarch
;
1234 /* Need a new architecture. Fill in a target specific vector. */
1235 tdep
= (struct gdbarch_tdep
*) xmalloc (sizeof (struct gdbarch_tdep
));
1236 gdbarch
= gdbarch_alloc (&info
, tdep
);
1237 tdep
->elf_flags
= elf_flags
;
1239 switch (info
.bfd_arch_info
->arch
)
1241 case bfd_arch_m68hc11
:
1242 tdep
->stack_correction
= 1;
1243 tdep
->use_page_register
= 0;
1244 tdep
->prologue
= m6811_prologue
;
1245 set_gdbarch_addr_bit (gdbarch
, 16);
1246 set_gdbarch_num_pseudo_regs (gdbarch
, M68HC11_NUM_PSEUDO_REGS
);
1247 set_gdbarch_pc_regnum (gdbarch
, HARD_PC_REGNUM
);
1248 set_gdbarch_num_regs (gdbarch
, M68HC11_NUM_REGS
);
1251 case bfd_arch_m68hc12
:
1252 tdep
->stack_correction
= 0;
1253 tdep
->use_page_register
= elf_flags
& E_M68HC12_BANKS
;
1254 tdep
->prologue
= m6812_prologue
;
1255 set_gdbarch_addr_bit (gdbarch
, elf_flags
& E_M68HC12_BANKS
? 32 : 16);
1256 set_gdbarch_num_pseudo_regs (gdbarch
,
1257 elf_flags
& E_M68HC12_BANKS
1258 ? M68HC12_NUM_PSEUDO_REGS
1259 : M68HC11_NUM_PSEUDO_REGS
);
1260 set_gdbarch_pc_regnum (gdbarch
, elf_flags
& E_M68HC12_BANKS
1261 ? M68HC12_HARD_PC_REGNUM
: HARD_PC_REGNUM
);
1262 set_gdbarch_num_regs (gdbarch
, elf_flags
& E_M68HC12_BANKS
1263 ? M68HC12_NUM_REGS
: M68HC11_NUM_REGS
);
1270 /* Initially set everything according to the ABI.
1271 Use 16-bit integers since it will be the case for most
1272 programs. The size of these types should normally be set
1273 according to the dwarf2 debug information. */
1274 set_gdbarch_short_bit (gdbarch
, 16);
1275 set_gdbarch_int_bit (gdbarch
, elf_flags
& E_M68HC11_I32
? 32 : 16);
1276 set_gdbarch_float_bit (gdbarch
, 32);
1277 set_gdbarch_double_bit (gdbarch
, elf_flags
& E_M68HC11_F64
? 64 : 32);
1278 set_gdbarch_long_double_bit (gdbarch
, elf_flags
& E_M68HC11_F64
? 64 : 32);
1279 set_gdbarch_long_bit (gdbarch
, 32);
1280 set_gdbarch_ptr_bit (gdbarch
, 16);
1281 set_gdbarch_long_long_bit (gdbarch
, 64);
1283 /* Set register info. */
1284 set_gdbarch_fp0_regnum (gdbarch
, -1);
1285 set_gdbarch_max_register_raw_size (gdbarch
, 2);
1286 set_gdbarch_max_register_virtual_size (gdbarch
, 2);
1287 set_gdbarch_frame_init_saved_regs (gdbarch
, m68hc11_frame_init_saved_regs
);
1288 set_gdbarch_frame_args_skip (gdbarch
, 0);
1290 set_gdbarch_read_pc (gdbarch
, generic_target_read_pc
);
1291 set_gdbarch_write_pc (gdbarch
, generic_target_write_pc
);
1292 set_gdbarch_read_fp (gdbarch
, generic_target_read_fp
);
1293 set_gdbarch_read_sp (gdbarch
, generic_target_read_sp
);
1294 set_gdbarch_write_sp (gdbarch
, generic_target_write_sp
);
1296 set_gdbarch_sp_regnum (gdbarch
, HARD_SP_REGNUM
);
1297 set_gdbarch_fp_regnum (gdbarch
, SOFT_FP_REGNUM
);
1298 set_gdbarch_register_name (gdbarch
, m68hc11_register_name
);
1299 set_gdbarch_register_size (gdbarch
, 2);
1300 set_gdbarch_register_bytes (gdbarch
, M68HC11_ALL_REGS
* 2);
1301 set_gdbarch_register_virtual_type (gdbarch
, m68hc11_register_virtual_type
);
1302 set_gdbarch_pseudo_register_read (gdbarch
, m68hc11_pseudo_register_read
);
1303 set_gdbarch_pseudo_register_write (gdbarch
, m68hc11_pseudo_register_write
);
1305 set_gdbarch_call_dummy_length (gdbarch
, 0);
1306 set_gdbarch_call_dummy_address (gdbarch
, m68hc11_call_dummy_address
);
1307 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch
, 1); /*???*/
1308 set_gdbarch_call_dummy_breakpoint_offset (gdbarch
, 0);
1309 set_gdbarch_call_dummy_start_offset (gdbarch
, 0);
1310 set_gdbarch_call_dummy_words (gdbarch
, m68hc11_call_dummy_words
);
1311 set_gdbarch_sizeof_call_dummy_words (gdbarch
,
1312 sizeof (m68hc11_call_dummy_words
));
1313 set_gdbarch_call_dummy_p (gdbarch
, 1);
1314 set_gdbarch_call_dummy_stack_adjust_p (gdbarch
, 0);
1315 set_gdbarch_get_saved_register (gdbarch
, deprecated_generic_get_saved_register
);
1316 set_gdbarch_fix_call_dummy (gdbarch
, generic_fix_call_dummy
);
1317 set_gdbarch_deprecated_extract_return_value (gdbarch
, m68hc11_extract_return_value
);
1318 set_gdbarch_push_arguments (gdbarch
, m68hc11_push_arguments
);
1319 set_gdbarch_push_dummy_frame (gdbarch
, generic_push_dummy_frame
);
1320 set_gdbarch_push_return_address (gdbarch
, m68hc11_push_return_address
);
1321 set_gdbarch_return_value_on_stack (gdbarch
, m68hc11_return_value_on_stack
);
1323 set_gdbarch_store_struct_return (gdbarch
, m68hc11_store_struct_return
);
1324 set_gdbarch_deprecated_store_return_value (gdbarch
, m68hc11_store_return_value
);
1325 set_gdbarch_deprecated_extract_struct_value_address (gdbarch
, m68hc11_extract_struct_value_address
);
1326 set_gdbarch_register_convertible (gdbarch
, generic_register_convertible_not
);
1329 set_gdbarch_frame_chain (gdbarch
, m68hc11_frame_chain
);
1330 set_gdbarch_frame_chain_valid (gdbarch
, generic_file_frame_chain_valid
);
1331 set_gdbarch_frame_saved_pc (gdbarch
, m68hc11_frame_saved_pc
);
1332 set_gdbarch_frame_args_address (gdbarch
, m68hc11_frame_args_address
);
1333 set_gdbarch_frame_locals_address (gdbarch
, m68hc11_frame_locals_address
);
1334 set_gdbarch_saved_pc_after_call (gdbarch
, m68hc11_saved_pc_after_call
);
1335 set_gdbarch_frame_num_args (gdbarch
, frame_num_args_unknown
);
1337 set_gdbarch_frame_chain_valid (gdbarch
, func_frame_chain_valid
);
1338 set_gdbarch_get_saved_register (gdbarch
, deprecated_generic_get_saved_register
);
1340 set_gdbarch_store_struct_return (gdbarch
, m68hc11_store_struct_return
);
1341 set_gdbarch_deprecated_store_return_value (gdbarch
, m68hc11_store_return_value
);
1342 set_gdbarch_deprecated_extract_struct_value_address
1343 (gdbarch
, m68hc11_extract_struct_value_address
);
1344 set_gdbarch_use_struct_convention (gdbarch
, m68hc11_use_struct_convention
);
1345 set_gdbarch_init_extra_frame_info (gdbarch
, m68hc11_init_extra_frame_info
);
1346 set_gdbarch_pop_frame (gdbarch
, m68hc11_pop_frame
);
1347 set_gdbarch_skip_prologue (gdbarch
, m68hc11_skip_prologue
);
1348 set_gdbarch_inner_than (gdbarch
, core_addr_lessthan
);
1349 set_gdbarch_decr_pc_after_break (gdbarch
, 0);
1350 set_gdbarch_function_start_offset (gdbarch
, 0);
1351 set_gdbarch_breakpoint_from_pc (gdbarch
, m68hc11_breakpoint_from_pc
);
1352 set_gdbarch_stack_align (gdbarch
, m68hc11_stack_align
);
1353 set_gdbarch_print_insn (gdbarch
, gdb_print_insn_m68hc11
);
1355 /* Minsymbol frobbing. */
1356 set_gdbarch_elf_make_msymbol_special (gdbarch
,
1357 m68hc11_elf_make_msymbol_special
);
1359 set_gdbarch_believe_pcc_promotion (gdbarch
, 1);
1365 _initialize_m68hc11_tdep (void)
1367 register_gdbarch_init (bfd_arch_m68hc11
, m68hc11_gdbarch_init
);
1368 register_gdbarch_init (bfd_arch_m68hc12
, m68hc11_gdbarch_init
);
1370 add_com ("regs", class_vars
, show_regs
, "Print all registers");