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