* h8300-tdep.c: Add definitions E_RET0_REGNUM and E_RET1_REGNUM to
[external/binutils.git] / gdb / h8300-tdep.c
1 /* Target-machine dependent code for Hitachi H8/300, for GDB.
2
3    Copyright 1988, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998,
4    1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23 /*
24    Contributed by Steve Chamberlain
25    sac@cygnus.com
26  */
27
28 #include "defs.h"
29 #include "value.h"
30 #include "inferior.h"
31 #include "symfile.h"
32 #include "arch-utils.h"
33 #include "regcache.h"
34 #include "gdbcore.h"
35 #include "objfiles.h"
36 #include "gdbcmd.h"
37 #include "gdb_assert.h"
38
39 /* Extra info which is saved in each frame_info. */
40 struct frame_extra_info
41 {
42   CORE_ADDR from_pc;
43 };
44
45 enum
46 {
47   h8300_reg_size = 2,
48   h8300h_reg_size = 4,
49   h8300_max_reg_size = 4,
50 };
51 #define BINWORD (h8300hmode ? h8300h_reg_size : h8300_reg_size)
52
53 enum gdb_regnum
54 {
55   E_R0_REGNUM, E_ER0_REGNUM = E_R0_REGNUM, E_ARG0_REGNUM = E_R0_REGNUM,
56                                            E_RET0_REGNUM = E_R0_REGNUM,
57   E_R1_REGNUM, E_ER1_REGNUM = E_R1_REGNUM, E_RET1_REGNUM = E_R1_REGNUM,
58   E_R2_REGNUM, E_ER2_REGNUM = E_R2_REGNUM, E_ARGLAST_REGNUM = E_R2_REGNUM,
59   E_R3_REGNUM, E_ER3_REGNUM = E_R3_REGNUM,
60   E_R4_REGNUM, E_ER4_REGNUM = E_R4_REGNUM,
61   E_R5_REGNUM, E_ER5_REGNUM = E_R5_REGNUM,
62   E_R6_REGNUM, E_ER6_REGNUM = E_R6_REGNUM, E_FP_REGNUM = E_R6_REGNUM,
63   E_SP_REGNUM,
64   E_CCR_REGNUM,
65   E_PC_REGNUM,
66   E_CYCLES_REGNUM,
67   E_TICK_REGNUM, E_EXR_REGNUM = E_TICK_REGNUM,
68   E_INST_REGNUM, E_TICKS_REGNUM = E_INST_REGNUM,
69   E_INSTS_REGNUM,
70   E_MACH_REGNUM,
71   E_MACL_REGNUM,
72   E_SBR_REGNUM,
73   E_VBR_REGNUM
74 };
75
76 #define UNSIGNED_SHORT(X) ((X) & 0xffff)
77
78 #define IS_PUSH(x) ((x & 0xfff0)==0x6df0)
79 #define IS_PUSH_FP(x) (x == 0x6df6)
80 #define IS_MOVE_FP(x) (x == 0x0d76 || x == 0x0ff6)
81 #define IS_MOV_SP_FP(x) (x == 0x0d76 || x == 0x0ff6)
82 #define IS_SUB2_SP(x) (x==0x1b87)
83 #define IS_SUB4_SP(x) (x==0x1b97)
84 #define IS_SUBL_SP(x) (x==0x7a37)
85 #define IS_MOVK_R5(x) (x==0x7905)
86 #define IS_SUB_R5SP(x) (x==0x1957)
87
88 /* If the instruction at PC is an argument register spill, return its
89    length.  Otherwise, return zero.
90
91    An argument register spill is an instruction that moves an argument
92    from the register in which it was passed to the stack slot in which
93    it really lives.  It is a byte, word, or longword move from an
94    argument register to a negative offset from the frame pointer.  */
95
96 static int
97 h8300_is_argument_spill (CORE_ADDR pc)
98 {
99   int w = read_memory_unsigned_integer (pc, 2);
100
101   if ((w & 0xfff0) == 0x6ee0                  /* mov.b Rs,@(d:16,er6) */
102       && 8 <= (w & 0xf) && (w & 0xf) <= 10)   /* Rs is R0L, R1L, or R2L  */
103     {
104       int w2 = read_memory_integer (pc + 2, 2);
105
106       /* ... and d:16 is negative.  */
107       if (w2 < 0)
108         return 4;
109     }
110   else if (w == 0x7860)
111     {
112       int w2 = read_memory_integer (pc + 2, 2);
113
114       if ((w2 & 0xfff0) == 0x6aa0)              /* mov.b Rs, @(d:24,er6) */
115         {
116           LONGEST disp = read_memory_integer (pc + 4, 4);
117
118           /* ... and d:24 is negative.  */
119           if (disp < 0 && disp > 0xffffff)
120             return 8;
121         }
122     }
123   else if ((w & 0xfff0) == 0x6fe0             /* mov.w Rs,@(d:16,er6) */
124            && (w & 0xf) <= 2)                 /* Rs is R0, R1, or R2 */
125     {
126       int w2 = read_memory_integer (pc + 2, 2);
127
128       /* ... and d:16 is negative.  */
129       if (w2 < 0)
130         return 4;
131     }
132   else if (w == 0x78e0)
133     {
134       int w2 = read_memory_integer (pc + 2, 2);
135
136       if ((w2 & 0xfff0) == 0x6ba0)              /* mov.b Rs, @(d:24,er6) */
137         {
138           LONGEST disp = read_memory_integer (pc + 4, 4);
139
140           /* ... and d:24 is negative.  */
141           if (disp < 0 && disp > 0xffffff)
142             return 8;
143         }
144     }
145   else if (w == 0x0100)
146     {
147       int w2 = read_memory_integer (pc + 2, 2);
148
149       if ((w2 & 0xfff0) == 0x6fe0             /* mov.l Rs,@(d:16,er6) */
150           && (w2 & 0xf) <= 2)                /* Rs is ER0, ER1, or ER2 */
151         {
152           int w3 = read_memory_integer (pc + 4, 2);
153
154           /* ... and d:16 is negative.  */
155           if (w3 < 0)
156             return 6;
157         }
158       else if (w2 == 0x78e0)
159         {
160           int w3 = read_memory_integer (pc + 4, 2);
161
162           if ((w3 & 0xfff0) == 0x6ba0)          /* mov.l Rs, @(d:24,er6) */
163             {
164               LONGEST disp = read_memory_integer (pc + 6, 4);
165
166               /* ... and d:24 is negative.  */
167               if (disp < 0 && disp > 0xffffff)
168                 return 10;
169             }
170         }
171     }
172
173   return 0;
174 }
175
176 static CORE_ADDR
177 h8300_skip_prologue (CORE_ADDR start_pc)
178 {
179   short int w;
180   int adjust = 0;
181
182   /* Skip past all push and stm insns.  */
183   while (1)
184     {
185       w = read_memory_unsigned_integer (start_pc, 2);
186       /* First look for push insns.  */
187       if (w == 0x0100 || w == 0x0110 || w == 0x0120 || w == 0x0130)
188         {
189           w = read_memory_unsigned_integer (start_pc + 2, 2);
190           adjust = 2;
191         }
192
193       if (IS_PUSH (w))
194         {
195           start_pc += 2 + adjust;
196           w = read_memory_unsigned_integer (start_pc, 2);
197           continue;
198         }
199       adjust = 0;
200       break;
201     }
202
203   /* Skip past a move to FP, either word or long sized */
204   w = read_memory_unsigned_integer (start_pc, 2);
205   if (w == 0x0100)
206     {
207       w = read_memory_unsigned_integer (start_pc + 2, 2);
208       adjust += 2;
209     }
210
211   if (IS_MOVE_FP (w))
212     {
213       start_pc += 2 + adjust;
214       w = read_memory_unsigned_integer (start_pc, 2);
215     }
216
217   /* Check for loading either a word constant into r5;
218      long versions are handled by the SUBL_SP below.  */
219   if (IS_MOVK_R5 (w))
220     {
221       start_pc += 2;
222       w = read_memory_unsigned_integer (start_pc, 2);
223     }
224
225   /* Now check for subtracting r5 from sp, word sized only.  */
226   if (IS_SUB_R5SP (w))
227     {
228       start_pc += 2 + adjust;
229       w = read_memory_unsigned_integer (start_pc, 2);
230     }
231
232   /* Check for subs #2 and subs #4. */
233   while (IS_SUB2_SP (w) || IS_SUB4_SP (w))
234     {
235       start_pc += 2 + adjust;
236       w = read_memory_unsigned_integer (start_pc, 2);
237     }
238
239   /* Check for a 32bit subtract.  */
240   if (IS_SUBL_SP (w))
241     start_pc += 6 + adjust;
242
243   /* Check for spilling an argument register to the stack frame.
244      This could also be an initializing store from non-prologue code,
245      but I don't think there's any harm in skipping that.  */
246   for (;;)
247     {
248       int spill_size = h8300_is_argument_spill (start_pc);
249       if (spill_size == 0)
250         break;
251       start_pc += spill_size;
252     }
253
254   return start_pc;
255 }
256
257 static int
258 gdb_print_insn_h8300 (bfd_vma memaddr, disassemble_info * info)
259 {
260   if (h8300smode)
261     return print_insn_h8300s (memaddr, info);
262   else if (h8300hmode)
263     return print_insn_h8300h (memaddr, info);
264   else
265     return print_insn_h8300 (memaddr, info);
266 }
267
268 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
269    is not the address of a valid instruction, the address of the next
270    instruction beyond ADDR otherwise.  *PWORD1 receives the first word
271    of the instruction. */
272
273 static CORE_ADDR
274 h8300_next_prologue_insn (CORE_ADDR addr, 
275                           CORE_ADDR lim, 
276                           unsigned short* pword1)
277 {
278   char buf[2];
279   if (addr < lim + 8)
280     {
281       read_memory (addr, buf, 2);
282       *pword1 = extract_signed_integer (buf, 2);
283
284       return addr + 2;
285     }
286   return 0;
287 }
288
289 /* Examine the prologue of a function.  `ip' points to the first instruction.
290    `limit' is the limit of the prologue (e.g. the addr of the first
291    linenumber, or perhaps the program counter if we're stepping through).
292    `frame_sp' is the stack pointer value in use in this frame.
293    `fsr' is a pointer to a frame_saved_regs structure into which we put
294    info about the registers saved by this frame.
295    `fi' is a struct frame_info pointer; we fill in various fields in it
296    to reflect the offsets of the arg pointer and the locals pointer.  */
297
298 /* Any function with a frame looks like this
299    SECOND ARG
300    FIRST ARG
301    RET PC
302    SAVED R2
303    SAVED R3
304    SAVED FP   <-FP POINTS HERE
305    LOCALS0
306    LOCALS1    <-SP POINTS HERE
307  */
308
309 static CORE_ADDR
310 h8300_examine_prologue (register CORE_ADDR ip, register CORE_ADDR limit,
311                         CORE_ADDR after_prolog_fp, CORE_ADDR *fsr,
312                         struct frame_info *fi)
313 {
314   register CORE_ADDR next_ip;
315   int r;
316   int have_fp = 0;
317   unsigned short insn_word;
318   /* Number of things pushed onto stack, starts at 2/4, 'cause the
319      PC is already there */
320   unsigned int reg_save_depth = BINWORD;
321
322   unsigned int auto_depth = 0;  /* Number of bytes of autos */
323
324   char in_frame[11];            /* One for each reg */
325
326   int adjust = 0;
327
328   memset (in_frame, 1, 11);
329   for (r = 0; r < 8; r++)
330     {
331       fsr[r] = 0;
332     }
333   if (after_prolog_fp == 0)
334     {
335       after_prolog_fp = read_register (E_SP_REGNUM);
336     }
337
338   /* If the PC isn't valid, quit now.  */
339   if (ip == 0 || ip & (h8300hmode ? ~0xffffff : ~0xffff))
340     return 0;
341
342   next_ip = h8300_next_prologue_insn (ip, limit, &insn_word);
343
344   if (insn_word == 0x0100)
345     {
346       insn_word = read_memory_unsigned_integer (ip + 2, 2);
347       adjust = 2;
348     }
349
350   /* Skip over any fp push instructions */
351   fsr[E_FP_REGNUM] = after_prolog_fp;
352   while (next_ip && IS_PUSH_FP (insn_word))
353     {
354       ip = next_ip + adjust;
355
356       in_frame[insn_word & 0x7] = reg_save_depth;
357       next_ip = h8300_next_prologue_insn (ip, limit, &insn_word);
358       reg_save_depth += 2 + adjust;
359     }
360
361   /* Is this a move into the fp */
362   if (next_ip && IS_MOV_SP_FP (insn_word))
363     {
364       ip = next_ip;
365       next_ip = h8300_next_prologue_insn (ip, limit, &insn_word);
366       have_fp = 1;
367     }
368
369   /* Skip over any stack adjustment, happens either with a number of
370      sub#2,sp or a mov #x,r5 sub r5,sp */
371
372   if (next_ip && (IS_SUB2_SP (insn_word) || IS_SUB4_SP (insn_word)))
373     {
374       while (next_ip && (IS_SUB2_SP (insn_word) || IS_SUB4_SP (insn_word)))
375         {
376           auto_depth += IS_SUB2_SP (insn_word) ? 2 : 4;
377           ip = next_ip;
378           next_ip = h8300_next_prologue_insn (ip, limit, &insn_word);
379         }
380     }
381   else
382     {
383       if (next_ip && IS_MOVK_R5 (insn_word))
384         {
385           ip = next_ip;
386           next_ip = h8300_next_prologue_insn (ip, limit, &insn_word);
387           auto_depth += insn_word;
388
389           next_ip = h8300_next_prologue_insn (next_ip, limit, &insn_word);
390           auto_depth += insn_word;
391         }
392       if (next_ip && IS_SUBL_SP (insn_word))
393         {
394           ip = next_ip;
395           auto_depth += read_memory_unsigned_integer (ip, 4);
396           ip += 4;
397
398           next_ip = h8300_next_prologue_insn (ip, limit, &insn_word);
399         }
400     }
401
402   /* Now examine the push insns to determine where everything lives
403      on the stack.  */
404   while (1)
405     {
406       adjust = 0;
407       if (!next_ip)
408         break;
409
410       if (insn_word == 0x0100)
411         {
412           ip = next_ip;
413           next_ip = h8300_next_prologue_insn (ip, limit, &insn_word);
414           adjust = 2;
415         }
416
417       if (IS_PUSH (insn_word))
418         {
419           auto_depth += 2 + adjust;
420           fsr[insn_word & 0x7] = after_prolog_fp - auto_depth;
421           ip = next_ip;
422           next_ip = h8300_next_prologue_insn (ip, limit, &insn_word);
423           continue;
424         }
425
426       /* Now check for push multiple insns.  */
427       if (insn_word == 0x0110 || insn_word == 0x0120 || insn_word == 0x0130)
428         {
429           int count = ((insn_word >> 4) & 0xf) + 1;
430           int start, i;
431
432           ip = next_ip;
433           next_ip = h8300_next_prologue_insn (ip, limit, &insn_word);
434           start = insn_word & 0x7;
435
436           for (i = start; i < start + count; i++)
437             {
438               auto_depth += 4;
439               fsr[i] = after_prolog_fp - auto_depth;
440             }
441         }
442       break;
443     }
444
445   /* The PC is at a known place */
446   get_frame_extra_info (fi)->from_pc =
447     read_memory_unsigned_integer (after_prolog_fp + BINWORD, BINWORD);
448
449   /* Rememeber any others too */
450   in_frame[E_PC_REGNUM] = 0;
451
452   if (have_fp)
453     /* We keep the old FP in the SP spot */
454     fsr[E_SP_REGNUM] = read_memory_unsigned_integer (fsr[E_FP_REGNUM], 
455                                                      BINWORD);
456   else
457     fsr[E_SP_REGNUM] = after_prolog_fp + auto_depth;
458
459   return (ip);
460 }
461
462 static void
463 h8300_frame_init_saved_regs (struct frame_info *fi)
464 {
465   CORE_ADDR func_addr, func_end;
466
467   if (!get_frame_saved_regs (fi))
468     {
469       frame_saved_regs_zalloc (fi);
470
471       /* Find the beginning of this function, so we can analyze its
472          prologue. */
473       if (find_pc_partial_function (get_frame_pc (fi), NULL, 
474                                     &func_addr, &func_end))
475         {
476           struct symtab_and_line sal = find_pc_line (func_addr, 0);
477           CORE_ADDR limit = (sal.end && sal.end < get_frame_pc (fi)) 
478             ? sal.end : get_frame_pc (fi);
479           /* This will fill in fields in fi. */
480           h8300_examine_prologue (func_addr, limit, get_frame_base (fi),
481                                   get_frame_saved_regs (fi), fi);
482         }
483       /* Else we're out of luck (can't debug completely stripped code). 
484          FIXME. */
485     }
486 }
487
488 /* Given a GDB frame, determine the address of the calling function's
489    frame.  This will be used to create a new GDB frame struct, and
490    then DEPRECATED_INIT_EXTRA_FRAME_INFO and DEPRECATED_INIT_FRAME_PC
491    will be called for the new frame.
492
493    For us, the frame address is its stack pointer value, so we look up
494    the function prologue to determine the caller's sp value, and
495    return it.  */
496
497 static CORE_ADDR
498 h8300_frame_chain (struct frame_info *thisframe)
499 {
500   if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (thisframe),
501                                    get_frame_base (thisframe),
502                                    get_frame_base (thisframe)))
503     {                           /* initialize the from_pc now */
504       get_frame_extra_info (thisframe)->from_pc =
505         deprecated_read_register_dummy (get_frame_pc (thisframe),
506                                         get_frame_base (thisframe),
507                                         E_PC_REGNUM);
508       return get_frame_base (thisframe);
509     }
510   return get_frame_saved_regs (thisframe)[E_SP_REGNUM];
511 }
512
513 /* Return the saved PC from this frame.
514
515    If the frame has a memory copy of SRP_REGNUM, use that.  If not,
516    just use the register SRP_REGNUM itself.  */
517
518 static CORE_ADDR
519 h8300_frame_saved_pc (struct frame_info *frame)
520 {
521   if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame),
522                                    get_frame_base (frame),
523                                    get_frame_base (frame)))
524     return deprecated_read_register_dummy (get_frame_pc (frame),
525                                            get_frame_base (frame),
526                                            E_PC_REGNUM);
527   else
528     return get_frame_extra_info (frame)->from_pc;
529 }
530
531 static void
532 h8300_init_extra_frame_info (int fromleaf, struct frame_info *fi)
533 {
534   if (!get_frame_extra_info (fi))
535     {
536       frame_extra_info_zalloc (fi, sizeof (struct frame_extra_info));
537       get_frame_extra_info (fi)->from_pc = 0;
538       
539       if (!get_frame_pc (fi))
540         {
541           if (get_next_frame (fi))
542             deprecated_update_frame_pc_hack (fi, h8300_frame_saved_pc (get_next_frame (fi)));
543         }
544       h8300_frame_init_saved_regs (fi);
545     }
546 }
547
548 /* Round N up or down to the nearest multiple of UNIT.
549    Evaluate N only once, UNIT several times.
550    UNIT must be a power of two.  */
551 #define round_up(n, unit)   (((n) + (unit) - 1) & -(unit))
552 #define round_down(n, unit) ((n) & -(unit))
553
554 /* Function: push_arguments
555    Setup the function arguments for calling a function in the inferior.
556    In this discussion, a `word' is 16 bits on the H8/300s, and 32 bits
557    on the H8/300H.
558
559    There are actually two ABI's here: -mquickcall (the default) and
560    -mno-quickcall.  With -mno-quickcall, all arguments are passed on
561    the stack after the return address, word-aligned.  With
562    -mquickcall, GCC tries to use r0 -- r2 to pass registers.  Since
563    GCC doesn't indicate in the object file which ABI was used to
564    compile it, GDB only supports the default --- -mquickcall.
565
566    Here are the rules for -mquickcall, in detail:
567
568    Each argument, whether scalar or aggregate, is padded to occupy a
569    whole number of words.  Arguments smaller than a word are padded at
570    the most significant end; those larger than a word are padded at
571    the least significant end.
572
573    The initial arguments are passed in r0 -- r2.  Earlier arguments go in
574    lower-numbered registers.  Multi-word arguments are passed in
575    consecutive registers, with the most significant end in the
576    lower-numbered register.
577
578    If an argument doesn't fit entirely in the remaining registers, it
579    is passed entirely on the stack.  Stack arguments begin just after
580    the return address.  Once an argument has overflowed onto the stack
581    this way, all subsequent arguments are passed on the stack.
582
583    The above rule has odd consequences.  For example, on the h8/300s,
584    if a function takes two longs and an int as arguments:
585    - the first long will be passed in r0/r1,
586    - the second long will be passed entirely on the stack, since it
587      doesn't fit in r2,
588    - and the int will be passed on the stack, even though it could fit
589      in r2.
590
591    A weird exception: if an argument is larger than a word, but not a
592    whole number of words in length (before padding), it is passed on
593    the stack following the rules for stack arguments above, even if
594    there are sufficient registers available to hold it.  Stranger
595    still, the argument registers are still `used up' --- even though
596    there's nothing in them.
597
598    So, for example, on the h8/300s, if a function expects a three-byte
599    structure and an int, the structure will go on the stack, and the
600    int will go in r2, not r0.
601   
602    If the function returns an aggregate type (struct, union, or class)
603    by value, the caller must allocate space to hold the return value,
604    and pass the callee a pointer to this space as an invisible first
605    argument, in R0.
606
607    For varargs functions, the last fixed argument and all the variable
608    arguments are always passed on the stack.  This means that calls to
609    varargs functions don't work properly unless there is a prototype
610    in scope.
611
612    Basically, this ABI is not good, for the following reasons:
613    - You can't call vararg functions properly unless a prototype is in scope.
614    - Structure passing is inconsistent, to no purpose I can see.
615    - It often wastes argument registers, of which there are only three
616      to begin with.  */
617
618 static CORE_ADDR
619 h8300_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
620                       int struct_return, CORE_ADDR struct_addr)
621 {
622   int stack_align, stack_alloc, stack_offset;
623   int wordsize = BINWORD;
624   int reg;
625   int argument;
626
627   /* First, make sure the stack is properly aligned.  */
628   sp = round_down (sp, wordsize);
629
630   /* Now make sure there's space on the stack for the arguments.  We
631      may over-allocate a little here, but that won't hurt anything.  */
632   stack_alloc = 0;
633   for (argument = 0; argument < nargs; argument++)
634     stack_alloc += round_up (TYPE_LENGTH (VALUE_TYPE (args[argument])),
635                              wordsize);
636   sp -= stack_alloc;
637
638   /* Now load as many arguments as possible into registers, and push
639      the rest onto the stack.  */
640   reg = E_ARG0_REGNUM;
641   stack_offset = 0;
642
643   /* If we're returning a structure by value, then we must pass a
644      pointer to the buffer for the return value as an invisible first
645      argument.  */
646   if (struct_return)
647     write_register (reg++, struct_addr);
648
649   for (argument = 0; argument < nargs; argument++)
650     {
651       struct type *type = VALUE_TYPE (args[argument]);
652       int len = TYPE_LENGTH (type);
653       char *contents = (char *) VALUE_CONTENTS (args[argument]);
654
655       /* Pad the argument appropriately.  */
656       int padded_len = round_up (len, wordsize);
657       char *padded = alloca (padded_len);
658
659       memset (padded, 0, padded_len);
660       memcpy (len < wordsize ? padded + padded_len - len : padded,
661               contents, len);
662
663       /* Could the argument fit in the remaining registers?  */
664       if (padded_len <= (E_ARGLAST_REGNUM - reg + 1) * wordsize)
665         {
666           /* Are we going to pass it on the stack anyway, for no good
667              reason?  */
668           if (len > wordsize && len % wordsize)
669             {
670               /* I feel so unclean.  */
671               write_memory (sp + stack_offset, padded, padded_len);
672               stack_offset += padded_len;
673
674               /* That's right --- even though we passed the argument
675                  on the stack, we consume the registers anyway!  Love
676                  me, love my dog.  */
677               reg += padded_len / wordsize;
678             }
679           else
680             {
681               /* Heavens to Betsy --- it's really going in registers!
682                  It would be nice if we could use write_register_bytes
683                  here, but on the h8/300s, there are gaps between
684                  the registers in the register file.  */
685               int offset;
686
687               for (offset = 0; offset < padded_len; offset += wordsize)
688                 {
689                   ULONGEST word = extract_unsigned_integer (padded + offset, 
690                                                             wordsize);
691                   write_register (reg++, word);
692                 }
693             }
694         }
695       else
696         {
697           /* It doesn't fit in registers!  Onto the stack it goes.  */
698           write_memory (sp + stack_offset, padded, padded_len);
699           stack_offset += padded_len;
700
701           /* Once one argument has spilled onto the stack, all
702              subsequent arguments go on the stack.  */
703           reg = E_ARGLAST_REGNUM + 1;
704         }
705     }
706
707   return sp;
708 }
709
710 /* Function: push_return_address
711    Setup the return address for a dummy frame, as called by
712    call_function_by_hand.  Only necessary when you are using an
713    empty CALL_DUMMY, ie. the target will not actually be executing
714    a JSR/BSR instruction.  */
715
716 static CORE_ADDR
717 h8300_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
718 {
719   unsigned char buf[4];
720   int wordsize = BINWORD;
721
722   sp -= wordsize;
723   store_unsigned_integer (buf, wordsize, CALL_DUMMY_ADDRESS ());
724   write_memory (sp, buf, wordsize);
725   return sp;
726 }
727
728 /* Function: h8300_pop_frame
729    Restore the machine to the state it had before the current frame 
730    was created.  Usually used either by the "RETURN" command, or by
731    call_function_by_hand after the dummy_frame is finished. */
732
733 static void
734 h8300_pop_frame (void)
735 {
736   unsigned regno;
737   struct frame_info *frame = get_current_frame ();
738
739   if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame),
740                                    get_frame_base (frame),
741                                    get_frame_base (frame)))
742     {
743       generic_pop_dummy_frame ();
744     }
745   else
746     {
747       for (regno = 0; regno < 8; regno++)
748         {
749           /* Don't forget E_SP_REGNUM is a frame_saved_regs struct is the
750              actual value we want, not the address of the value we want.  */
751           if (get_frame_saved_regs (frame)[regno] && regno != E_SP_REGNUM)
752             write_register (regno,
753                             read_memory_integer 
754                             (get_frame_saved_regs (frame)[regno], BINWORD));
755           else if (get_frame_saved_regs (frame)[regno] && regno == E_SP_REGNUM)
756             write_register (regno, get_frame_base (frame) + 2 * BINWORD);
757         }
758
759       /* Don't forget to update the PC too!  */
760       write_register (E_PC_REGNUM, get_frame_extra_info (frame)->from_pc);
761     }
762   flush_cached_frames ();
763 }
764
765 /* Function: extract_return_value
766    Figure out where in REGBUF the called function has left its return value.
767    Copy that into VALBUF.  Be sure to account for CPU type.   */
768
769 static void
770 h8300_extract_return_value (struct type *type, struct regcache *regcache,
771                             void *valbuf)
772 {
773   int len = TYPE_LENGTH (type);
774   ULONGEST c;
775
776   switch (len)
777     {
778       case 1:
779       case 2:
780         regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &c);
781         store_unsigned_integer (valbuf, len, c);
782         break;
783       case 4:   /* Needs two registers on plain H8/300 */
784         regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &c);
785         store_unsigned_integer (valbuf, 2, c);
786         regcache_cooked_read_unsigned (regcache, E_RET1_REGNUM, &c);
787         store_unsigned_integer ((void*)((char *)valbuf + 2), 2, c);
788         break;
789       case 8:           /* long long, double and long double are all defined
790                            as 4 byte types so far so this shouldn't happen. */
791         error ("I don't know how a 8 byte value is returned.");
792         break;
793     }
794 }
795
796 static void
797 h8300h_extract_return_value (struct type *type, struct regcache *regcache,
798                             void *valbuf)
799 {
800   int len = TYPE_LENGTH (type);
801   ULONGEST c;
802
803   switch (len)
804     {
805       case 1:
806       case 2:
807       case 4:
808         regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &c);
809         store_unsigned_integer (valbuf, len, c);
810         break;
811       case 8:           /* long long, double and long double are all defined
812                            as 4 byte types so far so this shouldn't happen. */
813         error ("I don't know how a 8 byte value is returned.");
814         break;
815     }
816 }
817
818
819 /* Function: store_return_value
820    Place the appropriate value in the appropriate registers.
821    Primarily used by the RETURN command.  */
822
823 static void
824 h8300_store_return_value (struct type *type, struct regcache *regcache,
825                           const void *valbuf)
826 {
827   int len = TYPE_LENGTH (type);
828   ULONGEST val;
829
830   switch (len)
831     {
832       case 1:
833       case 2:
834         val = extract_unsigned_integer (valbuf, len);
835         regcache_cooked_write_unsigned (regcache, E_RET0_REGNUM, val);
836         break;
837       case 4:                   /* long, float */
838         val = extract_unsigned_integer (valbuf, len);
839         regcache_cooked_write_unsigned (regcache, E_RET0_REGNUM,
840                                         (val >> 16) &0xffff);
841         regcache_cooked_write_unsigned (regcache, E_RET1_REGNUM, val & 0xffff);
842         break;
843       case 8:           /* long long, double and long double are all defined
844                              as 4 byte types so far so this shouldn't happen. */
845         error ("I don't know how to return a 8 byte value.");
846         break;
847     }
848 }
849
850 static void
851 h8300h_store_return_value (struct type *type, struct regcache *regcache,
852                            const void *valbuf)
853 {
854   int len = TYPE_LENGTH (type);
855   ULONGEST val;
856
857   switch (len)
858     {
859       case 1:
860       case 2:
861       case 4:                   /* long, float */
862         val = extract_unsigned_integer (valbuf, len);
863         regcache_cooked_write_unsigned (regcache, E_RET0_REGNUM, val);
864         break;
865       case 8:           /* long long, double and long double are all defined
866                              as 4 byte types so far so this shouldn't happen. */
867         error ("I don't know how to return a 8 byte value.");
868         break;
869     }
870 }
871
872 static struct cmd_list_element *setmachinelist;
873
874 static const char *
875 h8300_register_name (int regno)
876 {
877   /* The register names change depending on which h8300 processor
878      type is selected. */
879   static char *register_names[] = {
880     "r0", "r1", "r2", "r3", "r4", "r5", "r6",
881     "sp", "ccr","pc","cycles", "tick", "inst", ""
882   };
883   if (regno < 0
884       || regno >= (sizeof (register_names) / sizeof (*register_names)))
885     internal_error (__FILE__, __LINE__,
886                     "h8300_register_name: illegal register number %d", regno);
887   else
888     return register_names[regno];
889 }
890
891 static const char *
892 h8300s_register_name (int regno)
893 {
894   static char *register_names[] = {
895     "er0", "er1", "er2", "er3", "er4", "er5", "er6",
896     "sp", "ccr", "pc", "cycles", "exr", "tick", "inst"
897   };
898   if (regno < 0
899       || regno >= (sizeof (register_names) / sizeof (*register_names)))
900     internal_error (__FILE__, __LINE__,
901                     "h8300s_register_name: illegal register number %d", regno);
902   else
903     return register_names[regno];
904 }
905
906 static const char *
907 h8300sx_register_name (int regno)
908 {
909   static char *register_names[] = {
910     "er0", "er1", "er2", "er3", "er4", "er5", "er6",
911     "sp", "ccr", "pc", "cycles", "exr", "tick", "inst",
912     "mach", "macl", "sbr", "vbr"
913   };
914   if (regno < 0
915       || regno >= (sizeof (register_names) / sizeof (*register_names)))
916     internal_error (__FILE__, __LINE__,
917                     "h8300sx_register_name: illegal register number %d", regno);
918   else
919     return register_names[regno];
920 }
921
922 static void
923 h8300_print_register (struct gdbarch *gdbarch, struct ui_file *file,
924                       struct frame_info *frame, int regno)
925 {
926   LONGEST rval;
927   const char *name = gdbarch_register_name (gdbarch, regno);
928
929   if (!name || !*name)
930     return;
931
932   frame_read_signed_register (frame, regno, &rval);
933
934   fprintf_filtered (file, "%-14s ", name);
935   if (regno == E_CCR_REGNUM || (regno == E_EXR_REGNUM && h8300smode))
936     {
937       fprintf_filtered (file, "0x%02x        ", (unsigned char)rval);
938       print_longest (file, 'u', 1, rval);
939     }
940   else
941     {
942       fprintf_filtered (file, "0x%s  ", phex ((ULONGEST)rval, BINWORD));
943       print_longest (file, 'd', 1, rval);
944     }
945   if (regno == E_CCR_REGNUM)
946     {
947       /* CCR register */
948       int C, Z, N, V;
949       unsigned char l = rval & 0xff;
950       fprintf_filtered (file, "\t");
951       fprintf_filtered (file, "I-%d ", (l & 0x80) != 0);
952       fprintf_filtered (file, "UI-%d ", (l & 0x40) != 0);
953       fprintf_filtered (file, "H-%d ", (l & 0x20) != 0);
954       fprintf_filtered (file, "U-%d ", (l & 0x10) != 0);
955       N = (l & 0x8) != 0;
956       Z = (l & 0x4) != 0;
957       V = (l & 0x2) != 0;
958       C = (l & 0x1) != 0;
959       fprintf_filtered (file, "N-%d ", N);
960       fprintf_filtered (file, "Z-%d ", Z);
961       fprintf_filtered (file, "V-%d ", V);
962       fprintf_filtered (file, "C-%d ", C);
963       if ((C | Z) == 0)
964         fprintf_filtered (file, "u> ");
965       if ((C | Z) == 1)
966         fprintf_filtered (file, "u<= ");
967       if ((C == 0))
968         fprintf_filtered (file, "u>= ");
969       if (C == 1)
970         fprintf_filtered (file, "u< ");
971       if (Z == 0)
972         fprintf_filtered (file, "!= ");
973       if (Z == 1)
974         fprintf_filtered (file, "== ");
975       if ((N ^ V) == 0)
976         fprintf_filtered (file, ">= ");
977       if ((N ^ V) == 1)
978         fprintf_filtered (file, "< ");
979       if ((Z | (N ^ V)) == 0)
980         fprintf_filtered (file, "> ");
981       if ((Z | (N ^ V)) == 1)
982         fprintf_filtered (file, "<= ");
983     }
984   else if (regno == E_EXR_REGNUM && h8300smode)
985     {
986       /* EXR register */
987       unsigned char l = rval & 0xff;
988       fprintf_filtered (file, "\t");
989       fprintf_filtered (file, "T-%d - - - ", (l & 0x80) != 0);
990       fprintf_filtered (file, "I2-%d ", (l & 4) != 0);
991       fprintf_filtered (file, "I1-%d ", (l & 2) != 0);
992       fprintf_filtered (file, "I0-%d", (l & 1) != 0);
993     }
994   fprintf_filtered (file, "\n");
995 }
996
997 static void
998 h8300_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
999                             struct frame_info *frame, int regno, int cpregs)
1000 {
1001   if (regno < 0)
1002     for (regno = 0; regno < NUM_REGS; ++regno)
1003       h8300_print_register (gdbarch, file, frame, regno);
1004   else
1005     h8300_print_register (gdbarch, file, frame, regno);
1006 }
1007
1008 static CORE_ADDR
1009 h8300_saved_pc_after_call (struct frame_info *ignore)
1010 {
1011   return read_memory_unsigned_integer (read_register (E_SP_REGNUM), BINWORD);
1012 }
1013
1014 static struct type *
1015 h8300_register_type (struct gdbarch *gdbarch, int regno)
1016 {
1017   if (regno < 0 || regno >= NUM_REGS)
1018     internal_error (__FILE__, __LINE__,
1019                     "h8300_register_type: illegal register number %d",
1020                     regno);
1021   else
1022     {
1023       switch (regno)
1024         {
1025           case E_PC_REGNUM:
1026             return builtin_type_void_func_ptr;
1027           case E_SP_REGNUM:
1028           case E_FP_REGNUM:
1029             return builtin_type_void_data_ptr;
1030           case E_CCR_REGNUM:
1031             return builtin_type_uint8;
1032           case E_EXR_REGNUM:
1033             if (h8300smode)
1034               return builtin_type_uint8;
1035             /*FALLTHRU*/
1036           default:
1037             return h8300hmode ? builtin_type_int32
1038                               : builtin_type_int16;
1039         }
1040     }
1041 }
1042
1043 static CORE_ADDR
1044 h8300_extract_struct_value_address (struct regcache *regcache)
1045 {
1046   ULONGEST addr;
1047   regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &addr);
1048   return addr;
1049 }
1050
1051 const static unsigned char *
1052 h8300_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
1053 {
1054   /*static unsigned char breakpoint[] = { 0x7A, 0xFF };*/       /* ??? */
1055   static unsigned char breakpoint[] = { 0x01, 0x80 };           /* Sleep */
1056
1057   *lenptr = sizeof (breakpoint);
1058   return breakpoint;
1059 }
1060
1061 static CORE_ADDR
1062 h8300_push_dummy_code (struct gdbarch *gdbarch,
1063                        CORE_ADDR sp, CORE_ADDR funaddr, int using_gcc,
1064                        struct value **args, int nargs,
1065                        struct type *value_type,
1066                        CORE_ADDR *real_pc, CORE_ADDR *bp_addr)
1067 {
1068   /* Allocate space sufficient for a breakpoint.  */
1069   sp = (sp - 2) & ~1;
1070   /* Store the address of that breakpoint */
1071   *bp_addr = sp;
1072   /* h8300 always starts the call at the callee's entry point.  */
1073   *real_pc = funaddr;
1074   return sp;
1075 }
1076
1077 static void
1078 h8300_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
1079                         struct frame_info *frame, const char *args)
1080 {
1081   fprintf_filtered (file, "\
1082 No floating-point info available for this processor.\n");
1083 }
1084
1085 static struct gdbarch *
1086 h8300_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1087 {
1088   struct gdbarch_tdep *tdep = NULL;
1089   struct gdbarch *gdbarch;
1090
1091   arches = gdbarch_list_lookup_by_info (arches, &info);
1092   if (arches != NULL)
1093     return arches->gdbarch;
1094
1095 #if 0
1096   tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
1097 #endif
1098
1099   if (info.bfd_arch_info->arch != bfd_arch_h8300)
1100     return NULL;
1101
1102   gdbarch = gdbarch_alloc (&info, 0);
1103
1104   switch (info.bfd_arch_info->mach)
1105     {
1106     case bfd_mach_h8300:
1107       h8300sxmode = 0;
1108       h8300smode = 0;
1109       h8300hmode = 0;
1110       set_gdbarch_num_regs (gdbarch, 13);
1111       set_gdbarch_register_name (gdbarch, h8300_register_name);
1112       set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1113       set_gdbarch_addr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1114       set_gdbarch_extract_return_value (gdbarch, h8300_extract_return_value);
1115       set_gdbarch_store_return_value (gdbarch, h8300_store_return_value);
1116       break;
1117     case bfd_mach_h8300h:
1118     case bfd_mach_h8300hn:
1119       h8300sxmode = 0;
1120       h8300smode = 0;
1121       h8300hmode = 1;
1122       set_gdbarch_num_regs (gdbarch, 13);
1123       set_gdbarch_register_name (gdbarch, h8300_register_name);
1124       set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1125       set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1126       set_gdbarch_extract_return_value (gdbarch, h8300h_extract_return_value);
1127       set_gdbarch_store_return_value (gdbarch, h8300h_store_return_value);
1128       break;
1129     case bfd_mach_h8300s:
1130     case bfd_mach_h8300sn:
1131       h8300sxmode = 0;
1132       h8300smode = 1;
1133       h8300hmode = 1;
1134       set_gdbarch_num_regs (gdbarch, 14);
1135       set_gdbarch_register_name (gdbarch, h8300s_register_name);
1136       set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1137       set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1138       set_gdbarch_extract_return_value (gdbarch, h8300h_extract_return_value);
1139       set_gdbarch_store_return_value (gdbarch, h8300h_store_return_value);
1140       break;
1141     case bfd_mach_h8300sx:
1142     case bfd_mach_h8300sxn:
1143       h8300sxmode = 1;
1144       h8300smode = 1;
1145       h8300hmode = 1;
1146       set_gdbarch_num_regs (gdbarch, 18);
1147       set_gdbarch_register_name (gdbarch, h8300sx_register_name);
1148       set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1149       set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1150       set_gdbarch_extract_return_value (gdbarch, h8300h_extract_return_value);
1151       set_gdbarch_store_return_value (gdbarch, h8300h_store_return_value);
1152       break;
1153     }
1154
1155   /* NOTE: cagney/2002-12-06: This can be deleted when this arch is
1156      ready to unwind the PC first (see frame.c:get_prev_frame()).  */
1157   set_gdbarch_deprecated_init_frame_pc (gdbarch, init_frame_pc_default);
1158
1159   /*
1160    * Basic register fields and methods.
1161    */
1162
1163   set_gdbarch_num_pseudo_regs (gdbarch, 0);
1164   set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);
1165   set_gdbarch_deprecated_fp_regnum (gdbarch, E_FP_REGNUM);
1166   set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);
1167   set_gdbarch_register_type (gdbarch, h8300_register_type);
1168   set_gdbarch_print_registers_info (gdbarch, h8300_print_registers_info);
1169   set_gdbarch_print_float_info (gdbarch, h8300_print_float_info);
1170
1171   /*
1172    * Frame Info
1173    */
1174   set_gdbarch_skip_prologue (gdbarch, h8300_skip_prologue);
1175
1176   set_gdbarch_deprecated_frame_init_saved_regs (gdbarch, 
1177                                                 h8300_frame_init_saved_regs);
1178   set_gdbarch_deprecated_init_extra_frame_info (gdbarch, 
1179                                                 h8300_init_extra_frame_info);
1180   set_gdbarch_deprecated_frame_chain (gdbarch, h8300_frame_chain);
1181   set_gdbarch_deprecated_saved_pc_after_call (gdbarch, 
1182                                               h8300_saved_pc_after_call);
1183   set_gdbarch_deprecated_frame_saved_pc (gdbarch, h8300_frame_saved_pc);
1184
1185   /* 
1186    * Miscelany
1187    */
1188   /* Stack grows up. */
1189   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1190   /* PC stops zero byte after a trap instruction
1191      (which means: exactly on trap instruction). */
1192   set_gdbarch_decr_pc_after_break (gdbarch, 0);
1193   /* This value is almost never non-zero... */
1194   set_gdbarch_function_start_offset (gdbarch, 0);
1195   /* This value is almost never non-zero... */
1196   set_gdbarch_frame_args_skip (gdbarch, 0);
1197   set_gdbarch_frameless_function_invocation (gdbarch,
1198                                              frameless_look_for_prologue);
1199
1200   set_gdbarch_extract_struct_value_address (gdbarch,
1201                                             h8300_extract_struct_value_address);
1202   set_gdbarch_use_struct_convention (gdbarch, always_use_struct_convention);
1203   set_gdbarch_breakpoint_from_pc (gdbarch, h8300_breakpoint_from_pc);
1204   set_gdbarch_push_dummy_code (gdbarch, h8300_push_dummy_code);
1205
1206   set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1207   set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1208   set_gdbarch_long_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1209   set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1210   set_gdbarch_long_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1211
1212   /* set_gdbarch_stack_align (gdbarch, SOME_stack_align); */
1213   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
1214
1215   /*
1216    * Call Dummies
1217    * 
1218    * These values and methods are used when gdb calls a target function.  */
1219   /* Can all be replaced by push_dummy_call */
1220   set_gdbarch_deprecated_push_return_address (gdbarch, 
1221                                               h8300_push_return_address);
1222   set_gdbarch_deprecated_push_arguments (gdbarch, h8300_push_arguments);
1223   set_gdbarch_deprecated_pop_frame (gdbarch, h8300_pop_frame);
1224   set_gdbarch_deprecated_dummy_write_sp (gdbarch, deprecated_write_sp);
1225
1226
1227   return gdbarch;
1228 }
1229
1230 extern initialize_file_ftype _initialize_h8300_tdep; /* -Wmissing-prototypes */
1231
1232 void
1233 _initialize_h8300_tdep (void)
1234 {
1235   deprecated_tm_print_insn = gdb_print_insn_h8300;
1236   register_gdbarch_init (bfd_arch_h8300, h8300_gdbarch_init);
1237 }