1 /* Target-dependent code for Motorola 68HC11
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
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. */
22 /* FIXME: This is from tm-m68hc1.h */
24 #define GDB_TARGET_IS_M6811
26 /* Define the bit, byte, and word ordering of the machine. */
28 #define TARGET_BYTE_ORDER BIG_ENDIAN
30 /* Offset from address of function to start of its code.
31 Zero on most machines. */
33 #define FUNCTION_START_OFFSET 0
35 #ifdef __STDC__ /* Forward decls for prototypes */
37 struct frame_saved_regs;
42 /* Advance PC across any function entry prologue instructions
43 to reach some "real" code. */
45 extern CORE_ADDR m68hc11_skip_prologue ();
46 #define SKIP_PROLOGUE(ip) \
47 m68hc11_skip_prologue (ip)
50 /* Stack grows downward. */
52 #define INNER_THAN(lhs,rhs) ((lhs) < (rhs))
54 /* For a breakpoint, use "test". This is also the breakpoint
55 instruction on the 68HC12. */
56 #define BREAKPOINT {0x0}
58 /* If your kernel resets the pc after the trap happens you may need to
59 define this before including this file. */
60 #define DECR_PC_AFTER_BREAK 0
62 extern char *m68hc11_register_names[];
63 #define REGISTER_NAME(i) m68hc11_register_names[i]
65 #define REGISTER_SIZE 2
67 /* Register numbers of various important registers.
68 Note that some of these values are "real" register numbers,
69 and correspond to the general registers of the machine,
70 and some are "phony" register numbers which are too large
71 to be actual register numbers as far as the user is concerned
72 but do serve to get the desired values when passed to read_register. */
88 #define ZD32_REGNUM (ZD1_REGNUM+31)
90 #define NUM_REGS (ZD32_REGNUM+1)
92 #include "opcode/m68hc11.h"
94 /* Say how much memory is needed to store a copy of the register set */
95 #define REGISTER_BYTES ((NUM_REGS)*2)
97 /* Index within `registers' of the first byte of the space for
100 #define REGISTER_BYTE(N) ((N) * 2)
102 /* Number of bytes of storage in the actual machine representation
105 #define REGISTER_RAW_SIZE(N) (2)
107 /* Number of bytes of storage in the program's representation
110 #define REGISTER_VIRTUAL_SIZE(N) (2)
112 /* Largest value REGISTER_RAW_SIZE can have. */
114 #define MAX_REGISTER_RAW_SIZE 8
116 /* Largest value REGISTER_VIRTUAL_SIZE can have. */
118 #define MAX_REGISTER_VIRTUAL_SIZE 8
120 /* Return the GDB type object for the "standard" data type
121 of data in register N. */
123 #define REGISTER_VIRTUAL_TYPE(N) builtin_type_uint16
125 /* Store the address of the place in which to copy the structure the
126 subroutine will return. This is called from call_function.
128 We store structs through a pointer passed in D */
130 #define STORE_STRUCT_RETURN(ADDR, SP) \
131 { write_register (D_REGNUM, (ADDR)); }
134 /* Write into appropriate registers a function return value
135 of type TYPE, given in virtual format.
137 Things always get returned in D/X */
139 #define STORE_RETURN_VALUE(TYPE,VALBUF) \
140 write_register_bytes (REGISTER_BYTE (D_REGNUM), VALBUF, TYPE_LENGTH (TYPE))
143 /* Extract from an array REGBUF containing the (raw) register state
144 the address in which a function should return its structure value,
145 as a CORE_ADDR (or an expression that can be used as one). */
147 #define EXTRACT_STRUCT_VALUE_ADDRESS(REGBUF) (*(CORE_ADDR *)(REGBUF))
150 /* Define other aspects of the stack frame.
151 we keep a copy of the worked out return pc lying around, since it
152 is a useful bit of info */
154 #define EXTRA_FRAME_INFO \
156 CORE_ADDR return_pc; \
161 /* There's a mess in stack frame creation. See comments in blockframe.c
162 near reference to INIT_FRAME_PC_FIRST. */
164 #define INIT_FRAME_PC(fromleaf, prev) /* nada */
166 #define INIT_FRAME_PC_FIRST(fromleaf, prev) \
167 (prev)->pc = ((fromleaf) ? SAVED_PC_AFTER_CALL ((prev)->next) : \
168 (prev)->next ? FRAME_SAVED_PC ((prev)->next) : read_pc ());
170 #define INIT_EXTRA_FRAME_INFO(fromleaf, fi) \
171 m68hc11_init_extra_frame_info (fromleaf, fi)
173 extern void m68hc11_init_extra_frame_info (int fromleaf,
174 struct frame_info * fi);
176 /* A macro that tells us whether the function invocation represented
177 by FI does not have a frame on the stack associated with it. If it
178 does not, FRAMELESS is set to 1, else 0. */
180 #define FRAMELESS_FUNCTION_INVOCATION(FI) \
181 frameless_look_for_prologue (FI)
183 #define FRAME_CHAIN(FRAME) m68hc11_frame_chain (FRAME)
184 #define FRAME_CHAIN_VALID(chain,frame) \
185 ((chain) != 0 && (frame) != 0)
186 #define FRAME_SAVED_PC(FRAME) ((FRAME)->return_pc)
187 #define FRAME_ARGS_ADDRESS(fi) (fi)->frame
188 #define FRAME_LOCALS_ADDRESS(fi) (fi)->frame
190 #define SAVED_PC_AFTER_CALL(frame) m68hc11_saved_pc_after_call (frame)
192 /* Set VAL to the number of args passed to frame described by FI.
193 Can set VAL to -1, meaning no way to tell. */
194 /* We can't tell how many args there are */
196 #define FRAME_NUM_ARGS(fi) (-1)
198 /* Return number of bytes at start of arglist that are not really args. */
200 #define FRAME_ARGS_SKIP 0
203 /* Put here the code to store, into a struct frame_saved_regs,
204 the addresses of the saved registers of frame described by FRAME_INFO.
205 This includes special registers such as pc and fp saved in special
206 ways in the stack frame. sp is even more special:
207 the address we return for it IS the sp for the next frame. */
209 #define FRAME_FIND_SAVED_REGS(frame_info, frame_saved_regs) \
210 m68hc11_frame_find_saved_regs (frame_info, &(frame_saved_regs))
212 extern void m68hc11_frame_find_saved_regs (struct frame_info *,
213 struct frame_saved_regs *);
215 #define CALL_DUMMY { 0 }
216 #define PUSH_DUMMY_FRAME
217 #define CALL_DUMMY_START_OFFSET 0
218 #define CALL_DUMMY_BREAKPOINT_OFFSET (0)
220 extern CORE_ADDR m68hc11_call_dummy_address (void);
221 #define CALL_DUMMY_ADDRESS() m68hc11_call_dummy_address ()
223 #define FIX_CALL_DUMMY(dummyname, pc, fun, nargs, args, type, gcc_p) \
224 sp = m68hc11_fix_call_dummy (dummyname, pc, fun, nargs, args, type, gcc_p)
226 extern CORE_ADDR m68hc11_fix_call_dummy (char *, CORE_ADDR, CORE_ADDR,
227 int, struct value **,
229 #define PUSH_ARGUMENTS(nargs, args, sp, struct_return, struct_addr) \
230 sp = m68hc11_push_arguments ((nargs), (args), (sp), \
231 (struct_return), (struct_addr))
232 extern CORE_ADDR m68hc11_push_arguments (int, struct value **,
233 CORE_ADDR, int, CORE_ADDR);
236 /* Extract from an array REGBUF containing the (raw) register state
237 a function return value of type TYPE, and copy that, in virtual format,
240 #define EXTRACT_RETURN_VALUE(TYPE,REGBUF,VALBUF) \
241 m68hc11_extract_return_value(TYPE, REGBUF, VALBUF)
242 extern void m68hc11_extract_return_value (struct type *, char *, char *);
245 /* Discard from the stack the innermost frame,
246 restoring all saved registers. */
247 #define POP_FRAME m68hc11_pop_frame();
248 extern void m68hc11_pop_frame (void);
251 /* Number of bits in the appropriate type. */
253 #define TARGET_INT_BIT (2 * TARGET_CHAR_BIT)
254 #define TARGET_PTR_BIT (2 * TARGET_CHAR_BIT)
255 #define TARGET_DOUBLE_BIT (4 * TARGET_CHAR_BIT)
256 #define TARGET_LONG_DOUBLE_BIT (8 * TARGET_CHAR_BIT)
264 #include "gdbtypes.h"
267 #include "gdb_string.h"
269 #include "inferior.h"
272 #include "objfiles.h"
274 /* NOTE: This port is not finished. Several operations are not implemented
275 and will raise an error. Most of these functions concern the calling
276 of a function by GDB itself (command 'call') and retrieving data pushed
279 void m68hc11_frame_find_saved_regs (struct frame_info *fi,
280 struct frame_saved_regs *fsr);
281 static void m68hc11_pop_dummy_frame (struct frame_info *fi);
283 /* Table of registers for 68HC11. This includes the hard registers
284 and the pseudo hard registers used by GCC. */
286 m68hc11_register_names[] =
288 "x", "d", "y", "sp", "pc", "a", "b",
289 "ccr", "z", "frame","tmp", "zs", "xy",
290 "ZD1", "ZD2", "ZD3", "ZD4", "ZD5", "ZD6", "ZD7",
291 "ZD8", "ZD9", "ZD10", "ZD11", "ZD12", "ZD13", "ZD14",
292 "ZD15", "ZD16", "ZD17", "ZD18", "ZD19", "ZD20", "ZD21",
293 "ZD22", "ZD23", "ZD24", "ZD25", "ZD26", "ZD27", "ZD28",
294 "ZD29", "ZD30", "ZD31", "ZD32"
297 static int reg_last = 32 * 2 + 6;
298 static int frame_index = 6;
300 /* Raise an error for operations which are not yet provided. */
302 m68hc11_not_yet (const char *operation)
304 error ("Operation '%s' is not yet implemented\n", operation);
307 /* Immediately after a function call, return the saved pc before the frame
308 is setup. For sun3's, we check for the common case of being inside of a
309 system call, and if so, we know that Sun pushes the call # on the stack
310 prior to doing the trap. */
313 m68hc11_saved_pc_after_call (struct frame_info *frame)
315 unsigned addr = frame->frame + 1 + 2;
317 addr = read_register (SP_REGNUM) + 1;
319 return read_memory_integer (addr, 2) & 0x0FFFF;
322 /* Discard from the stack the innermost frame, restoring all saved
326 m68hc11_pop_frame (void)
328 m68hc11_not_yet ("m68hc11_pop_frame");
331 /* Analyze the function prologue to find some information
333 - the PC of the first line (for m68hc11_skip_prologue)
334 - the offset of the previous frame saved address (from current frame)
335 - the soft registers which are pushed. */
337 m68hc11_guess_from_prologue (CORE_ADDR pc, CORE_ADDR* first_line,
338 int* frame_offset, int* pushed_regs)
341 unsigned char op0, op1, op2;
345 int found_frame_point;
350 first_pc = get_pc_function_start (pc);
361 #define OP_PAGE2 (0x18)
362 #define OP_LDX (0xde)
363 #define OP_LDY (0xde)
364 #define OP_PSHX (0x3c)
365 #define OP_PSHY (0x3c)
366 #define OP_STS (0x9f)
367 #define OP_TSX (0x30)
368 #define OP_TSY (0x30)
369 #define OP_XGDX (0x8f)
370 #define OP_XGDY (0x8f)
371 #define OP_ADDD (0xc3)
372 #define OP_TXS (0x35)
373 #define OP_TYS (0x35)
375 /* The 68hc11 stack is as follows:
391 +-----------+ <--- current frame
394 With most processors (like 68K) the previous frame can be computed
395 easily because it is always at a fixed offset (see link/unlink).
396 That is, locals are accessed with negative offsets, arguments are
397 accessed with positive ones. Since 68hc11 only supports offsets
398 in the range [0..255], the frame is defined at the bottom of
399 locals (see picture).
401 The purpose of the analysis made here is to find out the size
402 of locals in this function. An alternative to this is to use
403 DWARF2 info. This would be better but I don't know how to
404 access dwarf2 debug from this function.
406 Walk from the function entry point to the point where we save
407 the frame. While walking instructions, compute the size of bytes
408 which are pushed. This gives us the index to access the previous
411 We limit the search to 128 bytes so that the algorithm is bounded
412 in case of random and wrong code. We also stop and abort if
413 we find an instruction which is not supposed to appear in the
414 prologue (as generated by gcc 2.95, 2.96).
419 found_frame_point = 0;
420 while (pc + 2 < func_end)
422 op0 = read_memory_unsigned_integer (pc, 1);
423 op1 = read_memory_unsigned_integer (pc + 1, 1);
424 op2 = read_memory_unsigned_integer (pc + 2, 1);
427 if (op0 == OP_LDX && op1 == frame_index)
433 else if (op0 == OP_PAGE2 && op1 == OP_LDY && op2 == frame_index)
439 else if (op0 == OP_PSHX)
446 else if (op0 == OP_PAGE2 && op1 == OP_PSHX)
453 else if (op0 == OP_STS && op1 == frame_index)
455 found_frame_point = 1;
459 else if (op0 == OP_TSX && op1 == OP_XGDX)
464 else if (op0 == OP_PAGE2 && op1 == OP_TSY && op2 == OP_PAGE2)
466 op0 = read_memory_unsigned_integer (pc + 3, 1);
473 else if (add_sp_mode && op0 == OP_ADDD)
475 sp_adjust = read_memory_unsigned_integer (pc + 1, 2);
476 if (sp_adjust & 0x8000)
477 sp_adjust |= 0xffff0000L;
479 sp_adjust = -sp_adjust;
483 else if (add_sp_mode == (1 | 4) && op0 == OP_XGDX
490 else if (add_sp_mode == (2 | 4) && op0 == OP_PAGE2
491 && op1 == OP_XGDY && op2 == OP_PAGE2)
493 op0 = read_memory_unsigned_integer (pc + 3, 1);
507 if (found_frame_point == 0)
513 *frame_offset = size;
516 /* Now, look forward to see how many registers are pushed on the stack.
517 We look only for soft registers so there must be a first LDX *REG
521 while (pc + 2 < func_end)
523 op0 = read_memory_unsigned_integer (pc, 1);
524 op1 = read_memory_unsigned_integer (pc + 1, 1);
525 op2 = read_memory_unsigned_integer (pc + 2, 1);
526 if (op0 == OP_LDX && op1 > frame_index && op1 <= reg_last)
531 else if (op0 == OP_PAGE2 && op1 == OP_LDY
532 && op2 > frame_index && op2 < reg_last)
537 else if (op0 == OP_PSHX)
539 /* If there was no load, this is a push for a function call. */
547 else if (op0 == OP_PAGE2 && op1 == OP_PSHY)
561 *pushed_regs = reg_saved;
567 m68hc11_skip_prologue (CORE_ADDR pc)
569 CORE_ADDR func_addr, func_end;
570 struct symtab_and_line sal;
574 /* If we have line debugging information, then the end of the. */
575 /* prologue should be the first assembly instruction of the
576 first source line. */
577 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
579 sal = find_pc_line (func_addr, 0);
580 if (sal.end && sal.end < func_end)
584 m68hc11_guess_from_prologue (pc, &pc, &frame_offset, &pushed_args);
588 /* Given a GDB frame, determine the address of the calling function's frame.
589 This will be used to create a new GDB frame struct, and then
590 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
594 m68hc11_frame_chain (struct frame_info *frame)
598 if (frame->return_pc == 0 || inside_entry_file(frame->return_pc))
601 if (frame->frame == 0)
603 return (CORE_ADDR) 0;
606 addr = frame->frame + frame->size + 1 - 2;
607 addr = read_memory_unsigned_integer (addr, 2) & 0x0FFFF;
616 /* Put here the code to store, into a struct frame_saved_regs, the
617 addresses of the saved registers of frame described by FRAME_INFO.
618 This includes special registers such as pc and fp saved in special
619 ways in the stack frame. sp is even more special: the address we
620 return for it IS the sp for the next frame. */
622 m68hc11_frame_find_saved_regs (struct frame_info *fi,
623 struct frame_saved_regs *fsr)
629 memset (fsr, 0, sizeof (*fsr));
630 m68hc11_guess_from_prologue (pc, &pc, &fi->size, &saved);
634 m68hc11_init_extra_frame_info (int fromleaf, struct frame_info *fi)
637 struct frame_saved_regs dummy;
639 m68hc11_frame_find_saved_regs (fi, &dummy);
643 fi->return_pc = m68hc11_saved_pc_after_call (fi);
647 addr = fi->frame + fi->size + 1;
648 fi->return_pc = read_memory_unsigned_integer (addr, 2) & 0x0ffff;
651 printf ("Pc@0x%04x, FR 0x%04x, size %d, read ret @0x%04x -> 0x%04x\n",
660 /* Same as 'info reg' but prints the registers in a different way. */
662 show_regs (char *args, int from_tty)
664 int ccr = read_register (PSW_REGNUM);
667 printf_filtered ("PC=%04x SP=%04x FP=%04x CCR=%02x %c%c%c%c%c%c%c%c\n",
668 read_register (PC_REGNUM),
669 read_register (SP_REGNUM),
670 read_register (FP_REGNUM),
672 ccr & M6811_S_BIT ? 'S' : '-',
673 ccr & M6811_X_BIT ? 'X' : '-',
674 ccr & M6811_H_BIT ? 'H' : '-',
675 ccr & M6811_I_BIT ? 'I' : '-',
676 ccr & M6811_N_BIT ? 'N' : '-',
677 ccr & M6811_Z_BIT ? 'Z' : '-',
678 ccr & M6811_V_BIT ? 'V' : '-',
679 ccr & M6811_C_BIT ? 'C' : '-');
681 printf_filtered ("D=%04x IX=%04x IY=%04x\n",
682 read_register (D_REGNUM),
683 read_register (X_REGNUM),
684 read_register (Y_REGNUM));
685 for (i = ZD1_REGNUM; i <= ZD32_REGNUM; i++)
687 printf_filtered ("ZD%d=%04x",
690 if (((i - ZD1_REGNUM) % 8) == 7)
691 printf_filtered ("\n");
693 printf_filtered (" ");
698 m68hc11_fix_call_dummy (char *dummyname,
706 m68hc11_not_yet ("m68hc11_fix_call_dummy");
711 m68hc11_pop_dummy_frame (struct frame_info *fi)
713 m68hc11_not_yet ("m68hc11_pop_dummy_frame");
718 m68hc11_push_arguments (int nargs,
722 CORE_ADDR struct_addr)
724 m68hc11_not_yet ("m68hc11_push_arguments");
730 m68hc11_call_dummy_address (void)
732 m68hc11_not_yet ("m68hc11_call_dummy_address");
736 /* Given a return value in `regbuf' with a type `valtype',
737 extract and copy its value into `valbuf'. */
740 m68hc11_extract_return_value (struct type *valtype,
744 m68hc11_not_yet ("m68hc11_extract_return_value");
748 _initialize_m68hc11_tdep (void)
750 tm_print_insn = print_insn_m68hc11;
752 add_com ("regs", class_vars, show_regs, "Print all registers");