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