* m68hc11.h (E_M68HC12_BANKS, E_M68HC11_I32, E_M68HC11_F64,
[deliverable/binutils-gdb.git] / gdb / m68hc11-tdep.c
CommitLineData
908f682f 1/* Target-dependent code for Motorola 68HC11 & 68HC12
70ed8774 2 Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
ffe1f3ee 3 Contributed by Stephane Carrez, stcarrez@nerim.fr
78073dd8
AC
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
78073dd8 21
82c230c2
SC
22#include "defs.h"
23#include "frame.h"
82c230c2
SC
24#include "symtab.h"
25#include "gdbtypes.h"
26#include "gdbcmd.h"
27#include "gdbcore.h"
28#include "gdb_string.h"
29#include "value.h"
30#include "inferior.h"
31#include "dis-asm.h"
32#include "symfile.h"
33#include "objfiles.h"
34#include "arch-utils.h"
4e052eda 35#include "regcache.h"
78073dd8 36
82c230c2
SC
37#include "target.h"
38#include "opcode/m68hc11.h"
78073dd8
AC
39
40/* Register numbers of various important registers.
41 Note that some of these values are "real" register numbers,
42 and correspond to the general registers of the machine,
43 and some are "phony" register numbers which are too large
44 to be actual register numbers as far as the user is concerned
45 but do serve to get the desired values when passed to read_register. */
46
82c230c2
SC
47#define HARD_X_REGNUM 0
48#define HARD_D_REGNUM 1
49#define HARD_Y_REGNUM 2
50#define HARD_SP_REGNUM 3
51#define HARD_PC_REGNUM 4
52
53#define HARD_A_REGNUM 5
54#define HARD_B_REGNUM 6
55#define HARD_CCR_REGNUM 7
56#define M68HC11_LAST_HARD_REG (HARD_CCR_REGNUM)
57
58/* Z is replaced by X or Y by gcc during machine reorg.
59 ??? There is no way to get it and even know whether
60 it's in X or Y or in ZS. */
61#define SOFT_Z_REGNUM 8
62
63/* Soft registers. These registers are special. There are treated
64 like normal hard registers by gcc and gdb (ie, within dwarf2 info).
65 They are physically located in memory. */
66#define SOFT_FP_REGNUM 9
67#define SOFT_TMP_REGNUM 10
68#define SOFT_ZS_REGNUM 11
69#define SOFT_XY_REGNUM 12
f91a8b6b
SC
70#define SOFT_UNUSED_REGNUM 13
71#define SOFT_D1_REGNUM 14
82c230c2
SC
72#define SOFT_D32_REGNUM (SOFT_D1_REGNUM+31)
73#define M68HC11_MAX_SOFT_REGS 32
74
75#define M68HC11_NUM_REGS (8)
76#define M68HC11_NUM_PSEUDO_REGS (M68HC11_MAX_SOFT_REGS+5)
77#define M68HC11_ALL_REGS (M68HC11_NUM_REGS+M68HC11_NUM_PSEUDO_REGS)
78
79#define M68HC11_REG_SIZE (2)
80
908f682f 81struct insn_sequence;
82c230c2
SC
82struct gdbarch_tdep
83 {
5d1a66bd
SC
84 /* Stack pointer correction value. For 68hc11, the stack pointer points
85 to the next push location. An offset of 1 must be applied to obtain
86 the address where the last value is saved. For 68hc12, the stack
87 pointer points to the last value pushed. No offset is necessary. */
88 int stack_correction;
908f682f
SC
89
90 /* Description of instructions in the prologue. */
91 struct insn_sequence *prologue;
82c230c2
SC
92 };
93
5d1a66bd
SC
94#define M6811_TDEP gdbarch_tdep (current_gdbarch)
95#define STACK_CORRECTION (M6811_TDEP->stack_correction)
96
82c230c2
SC
97struct frame_extra_info
98{
99 int frame_reg;
100 CORE_ADDR return_pc;
101 CORE_ADDR dummy;
102 int frameless;
103 int size;
104};
78073dd8 105
82c230c2
SC
106/* Table of registers for 68HC11. This includes the hard registers
107 and the soft registers used by GCC. */
108static char *
109m68hc11_register_names[] =
110{
111 "x", "d", "y", "sp", "pc", "a", "b",
f91a8b6b 112 "ccr", "z", "frame","tmp", "zs", "xy", 0,
82c230c2
SC
113 "d1", "d2", "d3", "d4", "d5", "d6", "d7",
114 "d8", "d9", "d10", "d11", "d12", "d13", "d14",
115 "d15", "d16", "d17", "d18", "d19", "d20", "d21",
116 "d22", "d23", "d24", "d25", "d26", "d27", "d28",
117 "d29", "d30", "d31", "d32"
118};
78073dd8 119
82c230c2
SC
120struct m68hc11_soft_reg
121{
122 const char *name;
123 CORE_ADDR addr;
124};
78073dd8 125
82c230c2 126static struct m68hc11_soft_reg soft_regs[M68HC11_ALL_REGS];
78073dd8 127
82c230c2 128#define M68HC11_FP_ADDR soft_regs[SOFT_FP_REGNUM].addr
78073dd8 129
82c230c2
SC
130static int soft_min_addr;
131static int soft_max_addr;
132static int soft_reg_initialized = 0;
78073dd8 133
82c230c2
SC
134/* Look in the symbol table for the address of a pseudo register
135 in memory. If we don't find it, pretend the register is not used
136 and not available. */
137static void
138m68hc11_get_register_info (struct m68hc11_soft_reg *reg, const char *name)
139{
140 struct minimal_symbol *msymbol;
78073dd8 141
82c230c2
SC
142 msymbol = lookup_minimal_symbol (name, NULL, NULL);
143 if (msymbol)
144 {
145 reg->addr = SYMBOL_VALUE_ADDRESS (msymbol);
146 reg->name = xstrdup (name);
147
148 /* Keep track of the address range for soft registers. */
149 if (reg->addr < (CORE_ADDR) soft_min_addr)
150 soft_min_addr = reg->addr;
151 if (reg->addr > (CORE_ADDR) soft_max_addr)
152 soft_max_addr = reg->addr;
153 }
154 else
155 {
156 reg->name = 0;
157 reg->addr = 0;
158 }
159}
78073dd8 160
82c230c2
SC
161/* Initialize the table of soft register addresses according
162 to the symbol table. */
163 static void
164m68hc11_initialize_register_info (void)
165{
166 int i;
78073dd8 167
82c230c2
SC
168 if (soft_reg_initialized)
169 return;
170
171 soft_min_addr = INT_MAX;
172 soft_max_addr = 0;
173 for (i = 0; i < M68HC11_ALL_REGS; i++)
174 {
175 soft_regs[i].name = 0;
176 }
177
178 m68hc11_get_register_info (&soft_regs[SOFT_FP_REGNUM], "_.frame");
179 m68hc11_get_register_info (&soft_regs[SOFT_TMP_REGNUM], "_.tmp");
180 m68hc11_get_register_info (&soft_regs[SOFT_ZS_REGNUM], "_.z");
181 soft_regs[SOFT_Z_REGNUM] = soft_regs[SOFT_ZS_REGNUM];
182 m68hc11_get_register_info (&soft_regs[SOFT_XY_REGNUM], "_.xy");
78073dd8 183
82c230c2
SC
184 for (i = SOFT_D1_REGNUM; i < M68HC11_MAX_SOFT_REGS; i++)
185 {
186 char buf[10];
78073dd8 187
82c230c2
SC
188 sprintf (buf, "_.d%d", i - SOFT_D1_REGNUM + 1);
189 m68hc11_get_register_info (&soft_regs[i], buf);
190 }
78073dd8 191
82c230c2
SC
192 if (soft_regs[SOFT_FP_REGNUM].name == 0)
193 {
194 warning ("No frame soft register found in the symbol table.\n");
195 warning ("Stack backtrace will not work.\n");
196 }
197 soft_reg_initialized = 1;
198}
78073dd8 199
82c230c2
SC
200/* Given an address in memory, return the soft register number if
201 that address corresponds to a soft register. Returns -1 if not. */
202static int
203m68hc11_which_soft_register (CORE_ADDR addr)
204{
205 int i;
206
207 if (addr < soft_min_addr || addr > soft_max_addr)
208 return -1;
209
210 for (i = SOFT_FP_REGNUM; i < M68HC11_ALL_REGS; i++)
211 {
212 if (soft_regs[i].name && soft_regs[i].addr == addr)
213 return i;
214 }
215 return -1;
216}
78073dd8 217
82c230c2
SC
218/* Fetch a pseudo register. The 68hc11 soft registers are treated like
219 pseudo registers. They are located in memory. Translate the register
220 fetch into a memory read. */
46ce284d
AC
221static void
222m68hc11_pseudo_register_read (struct gdbarch *gdbarch,
223 struct regcache *regcache,
224 int regno, void *buf)
82c230c2 225{
82c230c2
SC
226 m68hc11_initialize_register_info ();
227
228 /* Fetch a soft register: translate into a memory read. */
229 if (soft_regs[regno].name)
230 {
231 target_read_memory (soft_regs[regno].addr, buf, 2);
232 }
233 else
234 {
235 memset (buf, 0, 2);
236 }
82c230c2 237}
78073dd8 238
82c230c2
SC
239/* Store a pseudo register. Translate the register store
240 into a memory write. */
241static void
46ce284d
AC
242m68hc11_pseudo_register_write (struct gdbarch *gdbarch,
243 struct regcache *regcache,
244 int regno, const void *buf)
82c230c2
SC
245{
246 m68hc11_initialize_register_info ();
78073dd8 247
82c230c2
SC
248 /* Store a soft register: translate into a memory write. */
249 if (soft_regs[regno].name)
250 {
46ce284d
AC
251 const int regsize = 2;
252 char *tmp = alloca (regsize);
253 memcpy (tmp, buf, regsize);
254 target_write_memory (soft_regs[regno].addr, tmp, regsize);
82c230c2
SC
255 }
256}
78073dd8 257
fa88f677 258static const char *
82c230c2 259m68hc11_register_name (int reg_nr)
78073dd8 260{
82c230c2
SC
261 if (reg_nr < 0)
262 return NULL;
263 if (reg_nr >= M68HC11_ALL_REGS)
264 return NULL;
265
266 /* If we don't know the address of a soft register, pretend it
267 does not exist. */
268 if (reg_nr > M68HC11_LAST_HARD_REG && soft_regs[reg_nr].name == 0)
269 return NULL;
270 return m68hc11_register_names[reg_nr];
271}
78073dd8 272
f4f9705a 273static const unsigned char *
82c230c2 274m68hc11_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
78073dd8 275{
82c230c2
SC
276 static unsigned char breakpoint[] = {0x0};
277
278 *lenptr = sizeof (breakpoint);
279 return breakpoint;
78073dd8
AC
280}
281
282/* Immediately after a function call, return the saved pc before the frame
82c230c2 283 is setup. */
78073dd8 284
82c230c2 285static CORE_ADDR
78073dd8
AC
286m68hc11_saved_pc_after_call (struct frame_info *frame)
287{
82c230c2
SC
288 CORE_ADDR addr;
289
5d1a66bd 290 addr = read_register (HARD_SP_REGNUM) + STACK_CORRECTION;
78073dd8
AC
291 addr &= 0x0ffff;
292 return read_memory_integer (addr, 2) & 0x0FFFF;
293}
294
82c230c2
SC
295static CORE_ADDR
296m68hc11_frame_saved_pc (struct frame_info *frame)
297{
298 return frame->extra_info->return_pc;
299}
300
301static CORE_ADDR
302m68hc11_frame_args_address (struct frame_info *frame)
303{
908f682f 304 return frame->frame + frame->extra_info->size + STACK_CORRECTION + 2;
82c230c2
SC
305}
306
307static CORE_ADDR
308m68hc11_frame_locals_address (struct frame_info *frame)
309{
310 return frame->frame;
311}
312
78073dd8
AC
313/* Discard from the stack the innermost frame, restoring all saved
314 registers. */
315
82c230c2 316static void
fba45db2 317m68hc11_pop_frame (void)
78073dd8 318{
82c230c2
SC
319 register struct frame_info *frame = get_current_frame ();
320 register CORE_ADDR fp, sp;
321 register int regnum;
322
323 if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
324 generic_pop_dummy_frame ();
325 else
326 {
327 fp = FRAME_FP (frame);
328 FRAME_INIT_SAVED_REGS (frame);
329
330 /* Copy regs from where they were saved in the frame. */
331 for (regnum = 0; regnum < M68HC11_ALL_REGS; regnum++)
332 if (frame->saved_regs[regnum])
333 write_register (regnum,
334 read_memory_integer (frame->saved_regs[regnum], 2));
335
336 write_register (HARD_PC_REGNUM, frame->extra_info->return_pc);
b4fa4770 337 sp = (fp + frame->extra_info->size + 2) & 0x0ffff;
82c230c2
SC
338 write_register (HARD_SP_REGNUM, sp);
339 }
340 flush_cached_frames ();
78073dd8
AC
341}
342
908f682f
SC
343\f
344/* 68HC11 & 68HC12 prologue analysis.
345
346 */
347#define MAX_CODES 12
348
349/* 68HC11 opcodes. */
350#undef M6811_OP_PAGE2
351#define M6811_OP_PAGE2 (0x18)
352#define M6811_OP_LDX (0xde)
353#define M6811_OP_PSHX (0x3c)
354#define M6811_OP_STS (0x9f)
355#define M6811_OP_TSX (0x30)
356#define M6811_OP_XGDX (0x8f)
357#define M6811_OP_ADDD (0xc3)
358#define M6811_OP_TXS (0x35)
359#define M6811_OP_DES (0x34)
360
361/* 68HC12 opcodes. */
362#define M6812_OP_PAGE2 (0x18)
363#define M6812_OP_MOVW (0x01)
364#define M6812_PB_PSHW (0xae)
365#define M6812_OP_STS (0x7f)
366#define M6812_OP_LEAS (0x1b)
ffe1f3ee
SC
367#define M6812_OP_PSHX (0x34)
368#define M6812_OP_PSHY (0x35)
908f682f
SC
369
370/* Operand extraction. */
371#define OP_DIRECT (0x100) /* 8-byte direct addressing. */
372#define OP_IMM_LOW (0x200) /* Low part of 16-bit constant/address. */
373#define OP_IMM_HIGH (0x300) /* High part of 16-bit constant/address. */
374#define OP_PBYTE (0x400) /* 68HC12 indexed operand. */
375
376/* Identification of the sequence. */
377enum m6811_seq_type
378{
379 P_LAST = 0,
380 P_SAVE_REG, /* Save a register on the stack. */
381 P_SET_FRAME, /* Setup the frame pointer. */
382 P_LOCAL_1, /* Allocate 1 byte for locals. */
383 P_LOCAL_2, /* Allocate 2 bytes for locals. */
384 P_LOCAL_N /* Allocate N bytes for locals. */
385};
386
387struct insn_sequence {
388 enum m6811_seq_type type;
389 unsigned length;
390 unsigned short code[MAX_CODES];
391};
392
393/* Sequence of instructions in the 68HC11 function prologue. */
394static struct insn_sequence m6811_prologue[] = {
395 /* Sequences to save a soft-register. */
396 { P_SAVE_REG, 3, { M6811_OP_LDX, OP_DIRECT,
397 M6811_OP_PSHX } },
398 { P_SAVE_REG, 5, { M6811_OP_PAGE2, M6811_OP_LDX, OP_DIRECT,
399 M6811_OP_PAGE2, M6811_OP_PSHX } },
400
401 /* Sequences to allocate local variables. */
402 { P_LOCAL_N, 7, { M6811_OP_TSX,
403 M6811_OP_XGDX,
404 M6811_OP_ADDD, OP_IMM_HIGH, OP_IMM_LOW,
405 M6811_OP_XGDX,
406 M6811_OP_TXS } },
407 { P_LOCAL_N, 11, { M6811_OP_PAGE2, M6811_OP_TSX,
408 M6811_OP_PAGE2, M6811_OP_XGDX,
409 M6811_OP_ADDD, OP_IMM_HIGH, OP_IMM_LOW,
410 M6811_OP_PAGE2, M6811_OP_XGDX,
411 M6811_OP_PAGE2, M6811_OP_TXS } },
412 { P_LOCAL_1, 1, { M6811_OP_DES } },
413 { P_LOCAL_2, 1, { M6811_OP_PSHX } },
414 { P_LOCAL_2, 2, { M6811_OP_PAGE2, M6811_OP_PSHX } },
415
416 /* Initialize the frame pointer. */
417 { P_SET_FRAME, 2, { M6811_OP_STS, OP_DIRECT } },
418 { P_LAST, 0, { 0 } }
419};
420
421
422/* Sequence of instructions in the 68HC12 function prologue. */
423static struct insn_sequence m6812_prologue[] = {
424 { P_SAVE_REG, 5, { M6812_OP_PAGE2, M6812_OP_MOVW, M6812_PB_PSHW,
425 OP_IMM_HIGH, OP_IMM_LOW } },
426 { P_SET_FRAME, 3, { M6812_OP_STS, OP_IMM_HIGH, OP_IMM_LOW } },
427 { P_LOCAL_N, 2, { M6812_OP_LEAS, OP_PBYTE } },
ffe1f3ee
SC
428 { P_LOCAL_2, 1, { M6812_OP_PSHX } },
429 { P_LOCAL_2, 1, { M6812_OP_PSHY } },
908f682f
SC
430 { P_LAST, 0 }
431};
432
433
434/* Analyze the sequence of instructions starting at the given address.
435 Returns a pointer to the sequence when it is recognized and
436 the optional value (constant/address) associated with it.
437 Advance the pc for the next sequence. */
438static struct insn_sequence *
439m68hc11_analyze_instruction (struct insn_sequence *seq, CORE_ADDR *pc,
440 CORE_ADDR *val)
441{
442 unsigned char buffer[MAX_CODES];
443 unsigned bufsize;
444 unsigned j;
445 CORE_ADDR cur_val;
446 short v = 0;
447
448 bufsize = 0;
449 for (; seq->type != P_LAST; seq++)
450 {
451 cur_val = 0;
452 for (j = 0; j < seq->length; j++)
453 {
454 if (bufsize < j + 1)
455 {
456 buffer[bufsize] = read_memory_unsigned_integer (*pc + bufsize,
457 1);
458 bufsize++;
459 }
460 /* Continue while we match the opcode. */
461 if (seq->code[j] == buffer[j])
462 continue;
463
464 if ((seq->code[j] & 0xf00) == 0)
465 break;
466
467 /* Extract a sequence parameter (address or constant). */
468 switch (seq->code[j])
469 {
470 case OP_DIRECT:
471 cur_val = (CORE_ADDR) buffer[j];
472 break;
473
474 case OP_IMM_HIGH:
475 cur_val = cur_val & 0x0ff;
476 cur_val |= (buffer[j] << 8);
477 break;
478
479 case OP_IMM_LOW:
480 cur_val &= 0x0ff00;
481 cur_val |= buffer[j];
482 break;
483
484 case OP_PBYTE:
485 if ((buffer[j] & 0xE0) == 0x80)
486 {
487 v = buffer[j] & 0x1f;
488 if (v & 0x10)
489 v |= 0xfff0;
490 }
491 else if ((buffer[j] & 0xfe) == 0xf0)
492 {
493 v = read_memory_unsigned_integer (*pc + j + 1, 1);
494 if (buffer[j] & 1)
495 v |= 0xff00;
b4fa4770 496 *pc = *pc + 1;
908f682f
SC
497 }
498 else if (buffer[j] == 0xf2)
499 {
500 v = read_memory_unsigned_integer (*pc + j + 1, 2);
b4fa4770 501 *pc = *pc + 2;
908f682f
SC
502 }
503 cur_val = v;
504 break;
505 }
506 }
507
508 /* We have a full match. */
509 if (j == seq->length)
510 {
511 *val = cur_val;
512 *pc = *pc + j;
513 return seq;
514 }
515 }
516 return 0;
517}
518
78073dd8
AC
519/* Analyze the function prologue to find some information
520 about the function:
521 - the PC of the first line (for m68hc11_skip_prologue)
522 - the offset of the previous frame saved address (from current frame)
523 - the soft registers which are pushed. */
524static void
82c230c2
SC
525m68hc11_guess_from_prologue (CORE_ADDR pc, CORE_ADDR fp,
526 CORE_ADDR *first_line,
527 int *frame_offset, CORE_ADDR *pushed_regs)
78073dd8 528{
82c230c2 529 CORE_ADDR save_addr;
78073dd8 530 CORE_ADDR func_end;
78073dd8
AC
531 int size;
532 int found_frame_point;
82c230c2 533 int saved_reg;
78073dd8 534 CORE_ADDR first_pc;
908f682f
SC
535 int done = 0;
536 struct insn_sequence *seq_table;
78073dd8
AC
537
538 first_pc = get_pc_function_start (pc);
539 size = 0;
540
82c230c2 541 m68hc11_initialize_register_info ();
78073dd8
AC
542 if (first_pc == 0)
543 {
544 *frame_offset = 0;
78073dd8
AC
545 *first_line = pc;
546 return;
547 }
548
908f682f
SC
549 seq_table = gdbarch_tdep (current_gdbarch)->prologue;
550
78073dd8
AC
551 /* The 68hc11 stack is as follows:
552
553
554 | |
555 +-----------+
556 | |
557 | args |
558 | |
559 +-----------+
560 | PC-return |
561 +-----------+
562 | Old frame |
563 +-----------+
564 | |
565 | Locals |
566 | |
567 +-----------+ <--- current frame
568 | |
569
570 With most processors (like 68K) the previous frame can be computed
571 easily because it is always at a fixed offset (see link/unlink).
572 That is, locals are accessed with negative offsets, arguments are
573 accessed with positive ones. Since 68hc11 only supports offsets
574 in the range [0..255], the frame is defined at the bottom of
575 locals (see picture).
576
577 The purpose of the analysis made here is to find out the size
578 of locals in this function. An alternative to this is to use
579 DWARF2 info. This would be better but I don't know how to
580 access dwarf2 debug from this function.
581
582 Walk from the function entry point to the point where we save
583 the frame. While walking instructions, compute the size of bytes
584 which are pushed. This gives us the index to access the previous
585 frame.
586
587 We limit the search to 128 bytes so that the algorithm is bounded
588 in case of random and wrong code. We also stop and abort if
589 we find an instruction which is not supposed to appear in the
590 prologue (as generated by gcc 2.95, 2.96).
591 */
592 pc = first_pc;
593 func_end = pc + 128;
78073dd8 594 found_frame_point = 0;
908f682f 595 *frame_offset = 0;
b4fa4770 596 save_addr = fp + STACK_CORRECTION;
908f682f 597 while (!done && pc + 2 < func_end)
78073dd8 598 {
908f682f
SC
599 struct insn_sequence *seq;
600 CORE_ADDR val;
78073dd8 601
908f682f
SC
602 seq = m68hc11_analyze_instruction (seq_table, &pc, &val);
603 if (seq == 0)
604 break;
78073dd8 605
908f682f 606 if (seq->type == P_SAVE_REG)
78073dd8 607 {
908f682f
SC
608 if (found_frame_point)
609 {
610 saved_reg = m68hc11_which_soft_register (val);
611 if (saved_reg < 0)
612 break;
78073dd8 613
908f682f
SC
614 save_addr -= 2;
615 if (pushed_regs)
616 pushed_regs[saved_reg] = save_addr;
617 }
618 else
619 {
620 size += 2;
621 }
78073dd8 622 }
908f682f 623 else if (seq->type == P_SET_FRAME)
78073dd8
AC
624 {
625 found_frame_point = 1;
908f682f 626 *frame_offset = size;
78073dd8 627 }
908f682f 628 else if (seq->type == P_LOCAL_1)
78073dd8 629 {
6148eca7
SC
630 size += 1;
631 }
908f682f 632 else if (seq->type == P_LOCAL_2)
78073dd8 633 {
908f682f 634 size += 2;
78073dd8 635 }
908f682f 636 else if (seq->type == P_LOCAL_N)
78073dd8 637 {
908f682f
SC
638 /* Stack pointer is decremented for the allocation. */
639 if (val & 0x8000)
640 size -= (int) (val) | 0xffff0000;
641 else
642 size -= val;
78073dd8
AC
643 }
644 }
78073dd8
AC
645 *first_line = pc;
646}
647
82c230c2 648static CORE_ADDR
78073dd8
AC
649m68hc11_skip_prologue (CORE_ADDR pc)
650{
651 CORE_ADDR func_addr, func_end;
652 struct symtab_and_line sal;
653 int frame_offset;
78073dd8 654
82c230c2
SC
655 /* If we have line debugging information, then the end of the
656 prologue should be the first assembly instruction of the
78073dd8
AC
657 first source line. */
658 if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
659 {
660 sal = find_pc_line (func_addr, 0);
661 if (sal.end && sal.end < func_end)
662 return sal.end;
663 }
664
82c230c2 665 m68hc11_guess_from_prologue (pc, 0, &pc, &frame_offset, 0);
78073dd8
AC
666 return pc;
667}
668
669/* Given a GDB frame, determine the address of the calling function's frame.
670 This will be used to create a new GDB frame struct, and then
671 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
672*/
673
82c230c2 674static CORE_ADDR
78073dd8
AC
675m68hc11_frame_chain (struct frame_info *frame)
676{
82c230c2 677 CORE_ADDR addr;
78073dd8 678
6148eca7
SC
679 if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
680 return frame->frame; /* dummy frame same as caller's frame */
681
82c230c2
SC
682 if (frame->extra_info->return_pc == 0
683 || inside_entry_file (frame->extra_info->return_pc))
684 return (CORE_ADDR) 0;
78073dd8
AC
685
686 if (frame->frame == 0)
687 {
688 return (CORE_ADDR) 0;
689 }
690
5d1a66bd 691 addr = frame->frame + frame->extra_info->size + STACK_CORRECTION - 2;
78073dd8 692 addr = read_memory_unsigned_integer (addr, 2) & 0x0FFFF;
78073dd8
AC
693 return addr;
694}
695
696/* Put here the code to store, into a struct frame_saved_regs, the
697 addresses of the saved registers of frame described by FRAME_INFO.
698 This includes special registers such as pc and fp saved in special
699 ways in the stack frame. sp is even more special: the address we
700 return for it IS the sp for the next frame. */
82c230c2
SC
701static void
702m68hc11_frame_init_saved_regs (struct frame_info *fi)
78073dd8
AC
703{
704 CORE_ADDR pc;
184651e3
SC
705 CORE_ADDR addr;
706
82c230c2
SC
707 if (fi->saved_regs == NULL)
708 frame_saved_regs_zalloc (fi);
709 else
710 memset (fi->saved_regs, 0, sizeof (fi->saved_regs));
711
78073dd8 712 pc = fi->pc;
82c230c2
SC
713 m68hc11_guess_from_prologue (pc, fi->frame, &pc, &fi->extra_info->size,
714 fi->saved_regs);
715
5d1a66bd 716 addr = fi->frame + fi->extra_info->size + STACK_CORRECTION;
908f682f
SC
717 if (soft_regs[SOFT_FP_REGNUM].name)
718 fi->saved_regs[SOFT_FP_REGNUM] = addr - 2;
184651e3 719 fi->saved_regs[HARD_SP_REGNUM] = addr;
82c230c2 720 fi->saved_regs[HARD_PC_REGNUM] = fi->saved_regs[HARD_SP_REGNUM];
78073dd8
AC
721}
722
82c230c2 723static void
78073dd8
AC
724m68hc11_init_extra_frame_info (int fromleaf, struct frame_info *fi)
725{
82c230c2 726 CORE_ADDR addr;
78073dd8 727
82c230c2
SC
728 fi->extra_info = (struct frame_extra_info *)
729 frame_obstack_alloc (sizeof (struct frame_extra_info));
730
731 if (fi->next)
732 fi->pc = FRAME_SAVED_PC (fi->next);
733
734 m68hc11_frame_init_saved_regs (fi);
78073dd8
AC
735
736 if (fromleaf)
737 {
82c230c2 738 fi->extra_info->return_pc = m68hc11_saved_pc_after_call (fi);
78073dd8
AC
739 }
740 else
741 {
5d1a66bd 742 addr = fi->frame + fi->extra_info->size + STACK_CORRECTION;
82c230c2
SC
743 addr = read_memory_unsigned_integer (addr, 2) & 0x0ffff;
744 fi->extra_info->return_pc = addr;
78073dd8
AC
745#if 0
746 printf ("Pc@0x%04x, FR 0x%04x, size %d, read ret @0x%04x -> 0x%04x\n",
747 fi->pc,
748 fi->frame, fi->size,
749 addr & 0x0ffff,
750 fi->return_pc);
751#endif
752 }
753}
754
755/* Same as 'info reg' but prints the registers in a different way. */
756static void
757show_regs (char *args, int from_tty)
758{
82c230c2 759 int ccr = read_register (HARD_CCR_REGNUM);
78073dd8 760 int i;
82c230c2
SC
761 int nr;
762
78073dd8 763 printf_filtered ("PC=%04x SP=%04x FP=%04x CCR=%02x %c%c%c%c%c%c%c%c\n",
82c230c2
SC
764 (int) read_register (HARD_PC_REGNUM),
765 (int) read_register (HARD_SP_REGNUM),
766 (int) read_register (SOFT_FP_REGNUM),
78073dd8
AC
767 ccr,
768 ccr & M6811_S_BIT ? 'S' : '-',
769 ccr & M6811_X_BIT ? 'X' : '-',
770 ccr & M6811_H_BIT ? 'H' : '-',
771 ccr & M6811_I_BIT ? 'I' : '-',
772 ccr & M6811_N_BIT ? 'N' : '-',
773 ccr & M6811_Z_BIT ? 'Z' : '-',
774 ccr & M6811_V_BIT ? 'V' : '-',
775 ccr & M6811_C_BIT ? 'C' : '-');
776
777 printf_filtered ("D=%04x IX=%04x IY=%04x\n",
82c230c2
SC
778 (int) read_register (HARD_D_REGNUM),
779 (int) read_register (HARD_X_REGNUM),
780 (int) read_register (HARD_Y_REGNUM));
781
782 nr = 0;
783 for (i = SOFT_D1_REGNUM; i < M68HC11_ALL_REGS; i++)
78073dd8 784 {
82c230c2
SC
785 /* Skip registers which are not defined in the symbol table. */
786 if (soft_regs[i].name == 0)
787 continue;
788
789 printf_filtered ("D%d=%04x",
790 i - SOFT_D1_REGNUM + 1,
791 (int) read_register (i));
792 nr++;
793 if ((nr % 8) == 7)
78073dd8
AC
794 printf_filtered ("\n");
795 else
796 printf_filtered (" ");
797 }
82c230c2
SC
798 if (nr && (nr % 8) != 7)
799 printf_filtered ("\n");
78073dd8
AC
800}
801
22df305e
SC
802static CORE_ADDR
803m68hc11_stack_align (CORE_ADDR addr)
804{
805 return ((addr + 1) & -2);
806}
807
82c230c2 808static CORE_ADDR
78073dd8 809m68hc11_push_arguments (int nargs,
ea7c478f 810 struct value **args,
78073dd8
AC
811 CORE_ADDR sp,
812 int struct_return,
813 CORE_ADDR struct_addr)
814{
82c230c2
SC
815 int stack_alloc;
816 int argnum;
817 int first_stack_argnum;
818 int stack_offset;
819 struct type *type;
820 char *val;
821 int len;
822
823 stack_alloc = 0;
824 first_stack_argnum = 0;
825 if (struct_return)
826 {
184651e3
SC
827 /* The struct is allocated on the stack and gdb used the stack
828 pointer for the address of that struct. We must apply the
829 stack offset on the address. */
5d1a66bd 830 write_register (HARD_D_REGNUM, struct_addr + STACK_CORRECTION);
82c230c2
SC
831 }
832 else if (nargs > 0)
833 {
834 type = VALUE_TYPE (args[0]);
835 len = TYPE_LENGTH (type);
836
837 /* First argument is passed in D and X registers. */
838 if (len <= 4)
839 {
840 LONGEST v = extract_unsigned_integer (VALUE_CONTENTS (args[0]), len);
841 first_stack_argnum = 1;
842 write_register (HARD_D_REGNUM, v);
843 if (len > 2)
844 {
845 v >>= 16;
846 write_register (HARD_X_REGNUM, v);
847 }
848 }
849 }
850 for (argnum = first_stack_argnum; argnum < nargs; argnum++)
851 {
852 type = VALUE_TYPE (args[argnum]);
22df305e 853 stack_alloc += (TYPE_LENGTH (type) + 1) & -2;
82c230c2
SC
854 }
855 sp -= stack_alloc;
856
5d1a66bd 857 stack_offset = STACK_CORRECTION;
82c230c2
SC
858 for (argnum = first_stack_argnum; argnum < nargs; argnum++)
859 {
860 type = VALUE_TYPE (args[argnum]);
861 len = TYPE_LENGTH (type);
862
863 val = (char*) VALUE_CONTENTS (args[argnum]);
864 write_memory (sp + stack_offset, val, len);
865 stack_offset += len;
22df305e
SC
866 if (len & 1)
867 {
868 static char zero = 0;
869
870 write_memory (sp + stack_offset, &zero, 1);
871 stack_offset++;
872 }
82c230c2
SC
873 }
874 return sp;
78073dd8
AC
875}
876
877
82c230c2
SC
878/* Return a location where we can set a breakpoint that will be hit
879 when an inferior function call returns. */
78073dd8 880CORE_ADDR
fba45db2 881m68hc11_call_dummy_address (void)
78073dd8 882{
22df305e 883 return entry_point_address ();
78073dd8
AC
884}
885
82c230c2
SC
886static struct type *
887m68hc11_register_virtual_type (int reg_nr)
888{
889 return builtin_type_uint16;
890}
891
892static void
893m68hc11_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
894{
184651e3
SC
895 /* The struct address computed by gdb is on the stack.
896 It uses the stack pointer so we must apply the stack
897 correction offset. */
5d1a66bd 898 write_register (HARD_D_REGNUM, addr + STACK_CORRECTION);
82c230c2
SC
899}
900
901static void
902m68hc11_store_return_value (struct type *type, char *valbuf)
903{
22df305e
SC
904 int len;
905
906 len = TYPE_LENGTH (type);
907
908 /* First argument is passed in D and X registers. */
909 if (len <= 4)
910 {
911 LONGEST v = extract_unsigned_integer (valbuf, len);
912
913 write_register (HARD_D_REGNUM, v);
914 if (len > 2)
915 {
916 v >>= 16;
917 write_register (HARD_X_REGNUM, v);
918 }
919 }
920 else
921 error ("return of value > 4 is not supported.");
82c230c2
SC
922}
923
924
925/* Given a return value in `regbuf' with a type `type',
78073dd8
AC
926 extract and copy its value into `valbuf'. */
927
82c230c2
SC
928static void
929m68hc11_extract_return_value (struct type *type,
78073dd8
AC
930 char *regbuf,
931 char *valbuf)
932{
82c230c2
SC
933 int len = TYPE_LENGTH (type);
934
22df305e 935 switch (len)
82c230c2 936 {
22df305e
SC
937 case 1:
938 memcpy (valbuf, &regbuf[HARD_D_REGNUM * 2 + 1], len);
939 break;
940
941 case 2:
942 memcpy (valbuf, &regbuf[HARD_D_REGNUM * 2], len);
943 break;
944
945 case 3:
946 memcpy (&valbuf[0], &regbuf[HARD_X_REGNUM * 2 + 1], 1);
947 memcpy (&valbuf[1], &regbuf[HARD_D_REGNUM * 2], 2);
948 break;
949
950 case 4:
951 memcpy (&valbuf[0], &regbuf[HARD_X_REGNUM * 2], 2);
952 memcpy (&valbuf[2], &regbuf[HARD_D_REGNUM * 2], 2);
953 break;
954
955 default:
82c230c2
SC
956 error ("bad size for return value");
957 }
958}
959
960/* Should call_function allocate stack space for a struct return? */
961static int
962m68hc11_use_struct_convention (int gcc_p, struct type *type)
963{
22df305e
SC
964 return (TYPE_CODE (type) == TYPE_CODE_STRUCT
965 || TYPE_CODE (type) == TYPE_CODE_UNION
966 || TYPE_LENGTH (type) > 4);
82c230c2
SC
967}
968
969static int
970m68hc11_return_value_on_stack (struct type *type)
971{
22df305e 972 return TYPE_LENGTH (type) > 4;
82c230c2
SC
973}
974
975/* Extract from an array REGBUF containing the (raw) register state
976 the address in which a function should return its structure value,
977 as a CORE_ADDR (or an expression that can be used as one). */
978static CORE_ADDR
979m68hc11_extract_struct_value_address (char *regbuf)
980{
981 return extract_address (&regbuf[HARD_D_REGNUM * 2],
982 REGISTER_RAW_SIZE (HARD_D_REGNUM));
983}
984
985/* Function: push_return_address (pc)
986 Set up the return address for the inferior function call.
987 Needed for targets where we don't actually execute a JSR/BSR instruction */
988
989static CORE_ADDR
990m68hc11_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
991{
992 char valbuf[2];
993
22df305e 994 pc = CALL_DUMMY_ADDRESS ();
82c230c2
SC
995 sp -= 2;
996 store_unsigned_integer (valbuf, 2, pc);
5d1a66bd 997 write_memory (sp + STACK_CORRECTION, valbuf, 2);
82c230c2
SC
998 return sp;
999}
1000
1001/* Index within `registers' of the first byte of the space for
1002 register N. */
1003static int
1004m68hc11_register_byte (int reg_nr)
1005{
1006 return (reg_nr * M68HC11_REG_SIZE);
1007}
1008
1009static int
1010m68hc11_register_raw_size (int reg_nr)
1011{
1012 return M68HC11_REG_SIZE;
1013}
1014
ea3881d9
SC
1015static int
1016gdb_print_insn_m68hc11 (bfd_vma memaddr, disassemble_info *info)
1017{
1018 if (TARGET_ARCHITECTURE->arch == bfd_arch_m68hc11)
1019 return print_insn_m68hc11 (memaddr, info);
1020 else
1021 return print_insn_m68hc12 (memaddr, info);
1022}
1023
82c230c2
SC
1024static struct gdbarch *
1025m68hc11_gdbarch_init (struct gdbarch_info info,
1026 struct gdbarch_list *arches)
1027{
1028 static LONGEST m68hc11_call_dummy_words[] =
1029 {0};
1030 struct gdbarch *gdbarch;
1031 struct gdbarch_tdep *tdep;
82c230c2
SC
1032
1033 soft_reg_initialized = 0;
1034
1035 /* try to find a pre-existing architecture */
1036 for (arches = gdbarch_list_lookup_by_info (arches, &info);
1037 arches != NULL;
1038 arches = gdbarch_list_lookup_by_info (arches->next, &info))
1039 {
82c230c2
SC
1040 return arches->gdbarch;
1041 }
1042
1043 /* Need a new architecture. Fill in a target specific vector. */
1044 tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
1045 gdbarch = gdbarch_alloc (&info, tdep);
ed99b3d0 1046
5d1a66bd
SC
1047 switch (info.bfd_arch_info->arch)
1048 {
1049 case bfd_arch_m68hc11:
1050 tdep->stack_correction = 1;
908f682f 1051 tdep->prologue = m6811_prologue;
5d1a66bd 1052 break;
82c230c2 1053
5d1a66bd
SC
1054 case bfd_arch_m68hc12:
1055 tdep->stack_correction = 0;
908f682f 1056 tdep->prologue = m6812_prologue;
5d1a66bd
SC
1057 break;
1058
1059 default:
1060 break;
1061 }
7d32ba20
SC
1062
1063 /* Initially set everything according to the ABI.
1064 Use 16-bit integers since it will be the case for most
1065 programs. The size of these types should normally be set
1066 according to the dwarf2 debug information. */
82c230c2 1067 set_gdbarch_short_bit (gdbarch, 16);
7d32ba20 1068 set_gdbarch_int_bit (gdbarch, 16);
82c230c2
SC
1069 set_gdbarch_float_bit (gdbarch, 32);
1070 set_gdbarch_double_bit (gdbarch, 64);
1071 set_gdbarch_long_double_bit (gdbarch, 64);
1072 set_gdbarch_long_bit (gdbarch, 32);
1073 set_gdbarch_ptr_bit (gdbarch, 16);
1074 set_gdbarch_long_long_bit (gdbarch, 64);
1075
1076 /* Set register info. */
1077 set_gdbarch_fp0_regnum (gdbarch, -1);
1078 set_gdbarch_max_register_raw_size (gdbarch, 2);
1079 set_gdbarch_max_register_virtual_size (gdbarch, 2);
1080 set_gdbarch_register_raw_size (gdbarch, m68hc11_register_raw_size);
1081 set_gdbarch_register_virtual_size (gdbarch, m68hc11_register_raw_size);
1082 set_gdbarch_register_byte (gdbarch, m68hc11_register_byte);
1083 set_gdbarch_frame_init_saved_regs (gdbarch, m68hc11_frame_init_saved_regs);
1084 set_gdbarch_frame_args_skip (gdbarch, 0);
1085
1086 set_gdbarch_read_pc (gdbarch, generic_target_read_pc);
1087 set_gdbarch_write_pc (gdbarch, generic_target_write_pc);
1088 set_gdbarch_read_fp (gdbarch, generic_target_read_fp);
82c230c2
SC
1089 set_gdbarch_read_sp (gdbarch, generic_target_read_sp);
1090 set_gdbarch_write_sp (gdbarch, generic_target_write_sp);
1091
1092 set_gdbarch_num_regs (gdbarch, M68HC11_NUM_REGS);
1093 set_gdbarch_num_pseudo_regs (gdbarch, M68HC11_NUM_PSEUDO_REGS);
1094 set_gdbarch_sp_regnum (gdbarch, HARD_SP_REGNUM);
1095 set_gdbarch_fp_regnum (gdbarch, SOFT_FP_REGNUM);
1096 set_gdbarch_pc_regnum (gdbarch, HARD_PC_REGNUM);
1097 set_gdbarch_register_name (gdbarch, m68hc11_register_name);
1098 set_gdbarch_register_size (gdbarch, 2);
1099 set_gdbarch_register_bytes (gdbarch, M68HC11_ALL_REGS * 2);
1100 set_gdbarch_register_virtual_type (gdbarch, m68hc11_register_virtual_type);
46ce284d
AC
1101 set_gdbarch_pseudo_register_read (gdbarch, m68hc11_pseudo_register_read);
1102 set_gdbarch_pseudo_register_write (gdbarch, m68hc11_pseudo_register_write);
82c230c2
SC
1103
1104 set_gdbarch_use_generic_dummy_frames (gdbarch, 1);
1105 set_gdbarch_call_dummy_length (gdbarch, 0);
1106 set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
1107 set_gdbarch_call_dummy_address (gdbarch, m68hc11_call_dummy_address);
1108 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1); /*???*/
1109 set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0);
1110 set_gdbarch_call_dummy_start_offset (gdbarch, 0);
1111 set_gdbarch_pc_in_call_dummy (gdbarch, generic_pc_in_call_dummy);
1112 set_gdbarch_call_dummy_words (gdbarch, m68hc11_call_dummy_words);
1113 set_gdbarch_sizeof_call_dummy_words (gdbarch,
1114 sizeof (m68hc11_call_dummy_words));
1115 set_gdbarch_call_dummy_p (gdbarch, 1);
1116 set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
1117 set_gdbarch_get_saved_register (gdbarch, generic_get_saved_register);
1118 set_gdbarch_fix_call_dummy (gdbarch, generic_fix_call_dummy);
26e9b323 1119 set_gdbarch_deprecated_extract_return_value (gdbarch, m68hc11_extract_return_value);
82c230c2
SC
1120 set_gdbarch_push_arguments (gdbarch, m68hc11_push_arguments);
1121 set_gdbarch_push_dummy_frame (gdbarch, generic_push_dummy_frame);
1122 set_gdbarch_push_return_address (gdbarch, m68hc11_push_return_address);
1123 set_gdbarch_return_value_on_stack (gdbarch, m68hc11_return_value_on_stack);
1124
1125 set_gdbarch_store_struct_return (gdbarch, m68hc11_store_struct_return);
1126 set_gdbarch_store_return_value (gdbarch, m68hc11_store_return_value);
26e9b323 1127 set_gdbarch_deprecated_extract_struct_value_address (gdbarch, m68hc11_extract_struct_value_address);
82c230c2
SC
1128 set_gdbarch_register_convertible (gdbarch, generic_register_convertible_not);
1129
1130
1131 set_gdbarch_frame_chain (gdbarch, m68hc11_frame_chain);
1132 set_gdbarch_frame_chain_valid (gdbarch, generic_file_frame_chain_valid);
1133 set_gdbarch_frame_saved_pc (gdbarch, m68hc11_frame_saved_pc);
1134 set_gdbarch_frame_args_address (gdbarch, m68hc11_frame_args_address);
1135 set_gdbarch_frame_locals_address (gdbarch, m68hc11_frame_locals_address);
1136 set_gdbarch_saved_pc_after_call (gdbarch, m68hc11_saved_pc_after_call);
1137 set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
1138
1139 set_gdbarch_frame_chain_valid (gdbarch, func_frame_chain_valid);
1140 set_gdbarch_get_saved_register (gdbarch, generic_get_saved_register);
1141
1142 set_gdbarch_store_struct_return (gdbarch, m68hc11_store_struct_return);
1143 set_gdbarch_store_return_value (gdbarch, m68hc11_store_return_value);
26e9b323 1144 set_gdbarch_deprecated_extract_struct_value_address
82c230c2
SC
1145 (gdbarch, m68hc11_extract_struct_value_address);
1146 set_gdbarch_use_struct_convention (gdbarch, m68hc11_use_struct_convention);
1147 set_gdbarch_init_extra_frame_info (gdbarch, m68hc11_init_extra_frame_info);
1148 set_gdbarch_pop_frame (gdbarch, m68hc11_pop_frame);
1149 set_gdbarch_skip_prologue (gdbarch, m68hc11_skip_prologue);
1150 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1151 set_gdbarch_decr_pc_after_break (gdbarch, 0);
1152 set_gdbarch_function_start_offset (gdbarch, 0);
1153 set_gdbarch_breakpoint_from_pc (gdbarch, m68hc11_breakpoint_from_pc);
22df305e 1154 set_gdbarch_stack_align (gdbarch, m68hc11_stack_align);
70ed8774 1155 set_gdbarch_print_insn (gdbarch, gdb_print_insn_m68hc11);
82c230c2
SC
1156
1157 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
82c230c2
SC
1158
1159 return gdbarch;
78073dd8
AC
1160}
1161
1162void
fba45db2 1163_initialize_m68hc11_tdep (void)
78073dd8 1164{
82c230c2 1165 register_gdbarch_init (bfd_arch_m68hc11, m68hc11_gdbarch_init);
ea3881d9 1166 register_gdbarch_init (bfd_arch_m68hc12, m68hc11_gdbarch_init);
78073dd8
AC
1167
1168 add_com ("regs", class_vars, show_regs, "Print all registers");
1169}
1170
This page took 0.214753 seconds and 4 git commands to generate.