Update/correct copyright notices.
[external/binutils.git] / gdb / h8300-tdep.c
1 /* Target-machine dependent code for Hitachi H8/300, for GDB.
2    Copyright 1988, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
3    2000, 2001 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 /*
23    Contributed by Steve Chamberlain
24    sac@cygnus.com
25  */
26
27 #include "defs.h"
28 #include "frame.h"
29 #include "obstack.h"
30 #include "symtab.h"
31 #include "dis-asm.h"
32 #include "gdbcmd.h"
33 #include "gdbtypes.h"
34 #include "gdbcore.h"
35 #include "gdb_string.h"
36 #include "value.h"
37 #include "regcache.h"
38
39 extern int h8300hmode, h8300smode;
40
41 #undef NUM_REGS
42 #define NUM_REGS 11
43
44 #define UNSIGNED_SHORT(X) ((X) & 0xffff)
45
46 #define IS_PUSH(x) ((x & 0xfff0)==0x6df0)
47 #define IS_PUSH_FP(x) (x == 0x6df6)
48 #define IS_MOVE_FP(x) (x == 0x0d76 || x == 0x0ff6)
49 #define IS_MOV_SP_FP(x) (x == 0x0d76 || x == 0x0ff6)
50 #define IS_SUB2_SP(x) (x==0x1b87)
51 #define IS_SUB4_SP(x) (x==0x1b97)
52 #define IS_SUBL_SP(x) (x==0x7a37)
53 #define IS_MOVK_R5(x) (x==0x7905)
54 #define IS_SUB_R5SP(x) (x==0x1957)
55
56
57 /* The register names change depending on whether the h8300h processor
58    type is selected. */
59
60 static char *original_register_names[] = REGISTER_NAMES;
61
62 static char *h8300h_register_names[] =
63 {"er0", "er1", "er2", "er3", "er4", "er5", "er6",
64  "sp", "ccr", "pc", "cycles", "tick", "inst"};
65
66 char **h8300_register_names = original_register_names;
67
68
69 /* Local function declarations.  */
70
71 static CORE_ADDR examine_prologue ();
72 static void set_machine_hook (char *filename);
73
74 void h8300_frame_find_saved_regs ();
75
76 CORE_ADDR
77 h8300_skip_prologue (CORE_ADDR start_pc)
78 {
79   short int w;
80   int adjust = 0;
81
82   /* Skip past all push and stm insns.  */
83   while (1)
84     {
85       w = read_memory_unsigned_integer (start_pc, 2);
86       /* First look for push insns.  */
87       if (w == 0x0100 || w == 0x0110 || w == 0x0120 || w == 0x0130)
88         {
89           w = read_memory_unsigned_integer (start_pc + 2, 2);
90           adjust = 2;
91         }
92
93       if (IS_PUSH (w))
94         {
95           start_pc += 2 + adjust;
96           w = read_memory_unsigned_integer (start_pc, 2);
97           continue;
98         }
99       adjust = 0;
100       break;
101     }
102
103   /* Skip past a move to FP, either word or long sized */
104   w = read_memory_unsigned_integer (start_pc, 2);
105   if (w == 0x0100)
106     {
107       w = read_memory_unsigned_integer (start_pc + 2, 2);
108       adjust += 2;
109     }
110
111   if (IS_MOVE_FP (w))
112     {
113       start_pc += 2 + adjust;
114       w = read_memory_unsigned_integer (start_pc, 2);
115     }
116
117   /* Check for loading either a word constant into r5;
118      long versions are handled by the SUBL_SP below.  */
119   if (IS_MOVK_R5 (w))
120     {
121       start_pc += 2;
122       w = read_memory_unsigned_integer (start_pc, 2);
123     }
124
125   /* Now check for subtracting r5 from sp, word sized only.  */
126   if (IS_SUB_R5SP (w))
127     {
128       start_pc += 2 + adjust;
129       w = read_memory_unsigned_integer (start_pc, 2);
130     }
131
132   /* Check for subs #2 and subs #4. */
133   while (IS_SUB2_SP (w) || IS_SUB4_SP (w))
134     {
135       start_pc += 2 + adjust;
136       w = read_memory_unsigned_integer (start_pc, 2);
137     }
138
139   /* Check for a 32bit subtract.  */
140   if (IS_SUBL_SP (w))
141     start_pc += 6 + adjust;
142
143   return start_pc;
144 }
145
146 int
147 gdb_print_insn_h8300 (bfd_vma memaddr, disassemble_info *info)
148 {
149   if (h8300smode)
150     return print_insn_h8300s (memaddr, info);
151   else if (h8300hmode)
152     return print_insn_h8300h (memaddr, info);
153   else
154     return print_insn_h8300 (memaddr, info);
155 }
156
157 /* Given a GDB frame, determine the address of the calling function's frame.
158    This will be used to create a new GDB frame struct, and then
159    INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
160
161    For us, the frame address is its stack pointer value, so we look up
162    the function prologue to determine the caller's sp value, and return it.  */
163
164 CORE_ADDR
165 h8300_frame_chain (struct frame_info *thisframe)
166 {
167   if (PC_IN_CALL_DUMMY (thisframe->pc, thisframe->frame, thisframe->frame))
168     {                           /* initialize the from_pc now */
169       thisframe->from_pc = generic_read_register_dummy (thisframe->pc,
170                                                         thisframe->frame,
171                                                         PC_REGNUM);
172       return thisframe->frame;
173     }
174   h8300_frame_find_saved_regs (thisframe, (struct frame_saved_regs *) 0);
175   return thisframe->fsr->regs[SP_REGNUM];
176 }
177
178 /* Put here the code to store, into a struct frame_saved_regs,
179    the addresses of the saved registers of frame described by FRAME_INFO.
180    This includes special registers such as pc and fp saved in special
181    ways in the stack frame.  sp is even more special:
182    the address we return for it IS the sp for the next frame.
183
184    We cache the result of doing this in the frame_obstack, since it is
185    fairly expensive.  */
186
187 void
188 h8300_frame_find_saved_regs (struct frame_info *fi,
189                              struct frame_saved_regs *fsr)
190 {
191   register struct frame_saved_regs *cache_fsr;
192   CORE_ADDR ip;
193   struct symtab_and_line sal;
194   CORE_ADDR limit;
195
196   if (!fi->fsr)
197     {
198       cache_fsr = (struct frame_saved_regs *)
199         frame_obstack_alloc (sizeof (struct frame_saved_regs));
200       memset (cache_fsr, '\0', sizeof (struct frame_saved_regs));
201
202       fi->fsr = cache_fsr;
203
204       if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
205         {                       /* no more to do. */
206           if (fsr)
207             *fsr = *fi->fsr;
208           return;
209         }
210       /* Find the start and end of the function prologue.  If the PC
211          is in the function prologue, we only consider the part that
212          has executed already.  */
213
214       ip = get_pc_function_start (fi->pc);
215       sal = find_pc_line (ip, 0);
216       limit = (sal.end && sal.end < fi->pc) ? sal.end : fi->pc;
217
218       /* This will fill in fields in *fi as well as in cache_fsr.  */
219       examine_prologue (ip, limit, fi->frame, cache_fsr, fi);
220     }
221
222   if (fsr)
223     *fsr = *fi->fsr;
224 }
225
226 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
227    is not the address of a valid instruction, the address of the next
228    instruction beyond ADDR otherwise.  *PWORD1 receives the first word
229    of the instruction. */
230
231 CORE_ADDR
232 NEXT_PROLOGUE_INSN (CORE_ADDR addr, CORE_ADDR lim, INSN_WORD *pword1)
233 {
234   char buf[2];
235   if (addr < lim + 8)
236     {
237       read_memory (addr, buf, 2);
238       *pword1 = extract_signed_integer (buf, 2);
239
240       return addr + 2;
241     }
242   return 0;
243 }
244
245 /* Examine the prologue of a function.  `ip' points to the first instruction.
246    `limit' is the limit of the prologue (e.g. the addr of the first
247    linenumber, or perhaps the program counter if we're stepping through).
248    `frame_sp' is the stack pointer value in use in this frame.
249    `fsr' is a pointer to a frame_saved_regs structure into which we put
250    info about the registers saved by this frame.
251    `fi' is a struct frame_info pointer; we fill in various fields in it
252    to reflect the offsets of the arg pointer and the locals pointer.  */
253
254 static CORE_ADDR
255 examine_prologue (register CORE_ADDR ip, register CORE_ADDR limit,
256                   CORE_ADDR after_prolog_fp, struct frame_saved_regs *fsr,
257                   struct frame_info *fi)
258 {
259   register CORE_ADDR next_ip;
260   int r;
261   int have_fp = 0;
262   INSN_WORD insn_word;
263   /* Number of things pushed onto stack, starts at 2/4, 'cause the
264      PC is already there */
265   unsigned int reg_save_depth = h8300hmode ? 4 : 2;
266
267   unsigned int auto_depth = 0;  /* Number of bytes of autos */
268
269   char in_frame[11];            /* One for each reg */
270
271   int adjust = 0;
272
273   memset (in_frame, 1, 11);
274   for (r = 0; r < 8; r++)
275     {
276       fsr->regs[r] = 0;
277     }
278   if (after_prolog_fp == 0)
279     {
280       after_prolog_fp = read_register (SP_REGNUM);
281     }
282
283   /* If the PC isn't valid, quit now.  */
284   if (ip == 0 || ip & (h8300hmode ? ~0xffffff : ~0xffff))
285     return 0;
286
287   next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
288
289   if (insn_word == 0x0100)
290     {
291       insn_word = read_memory_unsigned_integer (ip + 2, 2);
292       adjust = 2;
293     }
294
295   /* Skip over any fp push instructions */
296   fsr->regs[6] = after_prolog_fp;
297   while (next_ip && IS_PUSH_FP (insn_word))
298     {
299       ip = next_ip + adjust;
300
301       in_frame[insn_word & 0x7] = reg_save_depth;
302       next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
303       reg_save_depth += 2 + adjust;
304     }
305
306   /* Is this a move into the fp */
307   if (next_ip && IS_MOV_SP_FP (insn_word))
308     {
309       ip = next_ip;
310       next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
311       have_fp = 1;
312     }
313
314   /* Skip over any stack adjustment, happens either with a number of
315      sub#2,sp or a mov #x,r5 sub r5,sp */
316
317   if (next_ip && (IS_SUB2_SP (insn_word) || IS_SUB4_SP (insn_word)))
318     {
319       while (next_ip && (IS_SUB2_SP (insn_word) || IS_SUB4_SP (insn_word)))
320         {
321           auto_depth += IS_SUB2_SP (insn_word) ? 2 : 4;
322           ip = next_ip;
323           next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
324         }
325     }
326   else
327     {
328       if (next_ip && IS_MOVK_R5 (insn_word))
329         {
330           ip = next_ip;
331           next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
332           auto_depth += insn_word;
333
334           next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn_word);
335           auto_depth += insn_word;
336         }
337       if (next_ip && IS_SUBL_SP (insn_word))
338         {
339           ip = next_ip;
340           auto_depth += read_memory_unsigned_integer (ip, 4);
341           ip += 4;
342
343           next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
344         }
345     }
346
347   /* Now examine the push insns to determine where everything lives
348      on the stack.  */
349   while (1)
350     {
351       adjust = 0;
352       if (!next_ip)
353         break;
354
355       if (insn_word == 0x0100)
356         {
357           ip = next_ip;
358           next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
359           adjust = 2;
360         }
361
362       if (IS_PUSH (insn_word))
363         {
364           ip = next_ip;
365           next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
366           fsr->regs[r] = after_prolog_fp + auto_depth;
367           auto_depth += 2 + adjust;
368           continue;
369         }
370
371       /* Now check for push multiple insns.  */
372       if (insn_word == 0x0110 || insn_word == 0x0120 || insn_word == 0x0130)
373         {
374           int count = ((insn_word >> 4) & 0xf) + 1;
375           int start, i;
376
377           ip = next_ip;
378           next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
379           start = insn_word & 0x7;
380
381           for (i = start; i <= start + count; i++)
382             {
383               fsr->regs[i] = after_prolog_fp + auto_depth;
384               auto_depth += 4;
385             }
386         }
387       break;
388     }
389
390   /* The args are always reffed based from the stack pointer */
391   fi->args_pointer = after_prolog_fp;
392   /* Locals are always reffed based from the fp */
393   fi->locals_pointer = after_prolog_fp;
394   /* The PC is at a known place */
395   fi->from_pc = read_memory_unsigned_integer (after_prolog_fp + BINWORD, BINWORD);
396
397   /* Rememeber any others too */
398   in_frame[PC_REGNUM] = 0;
399
400   if (have_fp)
401     /* We keep the old FP in the SP spot */
402     fsr->regs[SP_REGNUM] = read_memory_unsigned_integer (fsr->regs[6], BINWORD);
403   else
404     fsr->regs[SP_REGNUM] = after_prolog_fp + auto_depth;
405
406   return (ip);
407 }
408
409 void
410 h8300_init_extra_frame_info (int fromleaf, struct frame_info *fi)
411 {
412   fi->fsr = 0;                  /* Not yet allocated */
413   fi->args_pointer = 0;         /* Unknown */
414   fi->locals_pointer = 0;       /* Unknown */
415   fi->from_pc = 0;
416   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
417     {                           /* anything special to do? */
418       return;
419     }
420 }
421
422 /* Return the saved PC from this frame.
423
424    If the frame has a memory copy of SRP_REGNUM, use that.  If not,
425    just use the register SRP_REGNUM itself.  */
426
427 CORE_ADDR
428 h8300_frame_saved_pc (struct frame_info *frame)
429 {
430   if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
431     return generic_read_register_dummy (frame->pc, frame->frame, PC_REGNUM);
432   else
433     return frame->from_pc;
434 }
435
436 CORE_ADDR
437 frame_locals_address (struct frame_info *fi)
438 {
439   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
440     return (CORE_ADDR) 0;       /* Not sure what else to do... */
441   if (!fi->locals_pointer)
442     {
443       struct frame_saved_regs ignore;
444
445       get_frame_saved_regs (fi, &ignore);
446
447     }
448   return fi->locals_pointer;
449 }
450
451 /* Return the address of the argument block for the frame
452    described by FI.  Returns 0 if the address is unknown.  */
453
454 CORE_ADDR
455 frame_args_address (struct frame_info *fi)
456 {
457   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
458     return (CORE_ADDR) 0;       /* Not sure what else to do... */
459   if (!fi->args_pointer)
460     {
461       struct frame_saved_regs ignore;
462
463       get_frame_saved_regs (fi, &ignore);
464
465     }
466
467   return fi->args_pointer;
468 }
469
470 /* Function: push_arguments
471    Setup the function arguments for calling a function in the inferior.
472
473    On the Hitachi H8/300 architecture, there are three registers (R0 to R2)
474    which are dedicated for passing function arguments.  Up to the first
475    three arguments (depending on size) may go into these registers.
476    The rest go on the stack.
477
478    Arguments that are smaller than WORDSIZE bytes will still take up a
479    whole register or a whole WORDSIZE word on the stack, and will be
480    right-justified in the register or the stack word.  This includes
481    chars and small aggregate types.  Note that WORDSIZE depends on the 
482    cpu type.
483
484    Arguments that are larger than WORDSIZE bytes will be split between
485    two or more registers as available, but will NOT be split between a
486    register and the stack.
487
488    An exceptional case exists for struct arguments (and possibly other
489    aggregates such as arrays) -- if the size is larger than WORDSIZE
490    bytes but not a multiple of WORDSIZE bytes.  In this case the
491    argument is never split between the registers and the stack, but
492    instead is copied in its entirety onto the stack, AND also copied
493    into as many registers as there is room for.  In other words, space
494    in registers permitting, two copies of the same argument are passed
495    in.  As far as I can tell, only the one on the stack is used,
496    although that may be a function of the level of compiler
497    optimization.  I suspect this is a compiler bug.  Arguments of
498    these odd sizes are left-justified within the word (as opposed to
499    arguments smaller than WORDSIZE bytes, which are right-justified).
500
501    If the function is to return an aggregate type such as a struct,
502    the caller must allocate space into which the callee will copy the
503    return value.  In this case, a pointer to the return value location
504    is passed into the callee in register R0, which displaces one of
505    the other arguments passed in via registers R0 to R2.  */
506
507 CORE_ADDR
508 h8300_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
509                       unsigned char struct_return, CORE_ADDR struct_addr)
510 {
511   int stack_align, stack_alloc, stack_offset;
512   int wordsize;
513   int argreg;
514   int argnum;
515   struct type *type;
516   CORE_ADDR regval;
517   char *val;
518   char valbuf[4];
519   int len;
520
521   if (h8300hmode || h8300smode)
522     {
523       stack_align = 3;
524       wordsize = 4;
525     }
526   else
527     {
528       stack_align = 1;
529       wordsize = 2;
530     }
531
532   /* first force sp to a n-byte alignment */
533   sp = sp & ~stack_align;
534
535   /* Now make sure there's space on the stack */
536   for (argnum = 0, stack_alloc = 0;
537        argnum < nargs; argnum++)
538     stack_alloc += ((TYPE_LENGTH (VALUE_TYPE (args[argnum])) + stack_align)
539                     & ~stack_align);
540   sp -= stack_alloc;            /* make room on stack for args */
541   /* we may over-allocate a little here, but that won't hurt anything */
542
543   argreg = ARG0_REGNUM;
544   if (struct_return)            /* "struct return" pointer takes up one argreg */
545     {
546       write_register (argreg++, struct_addr);
547     }
548
549   /* Now load as many as possible of the first arguments into
550      registers, and push the rest onto the stack.  There are 3N bytes
551      in three registers available.  Loop thru args from first to last.  */
552
553   for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++)
554     {
555       type = VALUE_TYPE (args[argnum]);
556       len = TYPE_LENGTH (type);
557       memset (valbuf, 0, sizeof (valbuf));
558       if (len < wordsize)
559         {
560           /* the purpose of this is to right-justify the value within the word */
561           memcpy (valbuf + (wordsize - len),
562                   (char *) VALUE_CONTENTS (args[argnum]), len);
563           val = valbuf;
564         }
565       else
566         val = (char *) VALUE_CONTENTS (args[argnum]);
567
568       if (len > (ARGLAST_REGNUM + 1 - argreg) * REGISTER_RAW_SIZE (ARG0_REGNUM) ||
569           (len > wordsize && (len & stack_align) != 0))
570         {                       /* passed on the stack */
571           write_memory (sp + stack_offset, val,
572                         len < wordsize ? wordsize : len);
573           stack_offset += (len + stack_align) & ~stack_align;
574         }
575       /* NOTE WELL!!!!!  This is not an "else if" clause!!!
576          That's because some *&^%$ things get passed on the stack
577          AND in the registers!   */
578       if (len <= (ARGLAST_REGNUM + 1 - argreg) * REGISTER_RAW_SIZE (ARG0_REGNUM))
579         while (len > 0)
580           {                     /* there's room in registers */
581             regval = extract_address (val, wordsize);
582             write_register (argreg, regval);
583             len -= wordsize;
584             val += wordsize;
585             argreg++;
586           }
587     }
588   return sp;
589 }
590
591 /* Function: push_return_address
592    Setup the return address for a dummy frame, as called by
593    call_function_by_hand.  Only necessary when you are using an
594    empty CALL_DUMMY, ie. the target will not actually be executing
595    a JSR/BSR instruction.  */
596
597 CORE_ADDR
598 h8300_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
599 {
600   unsigned char buf[4];
601   int wordsize;
602
603   if (h8300hmode || h8300smode)
604     wordsize = 4;
605   else
606     wordsize = 2;
607
608   sp -= wordsize;
609   store_unsigned_integer (buf, wordsize, CALL_DUMMY_ADDRESS ());
610   write_memory (sp, buf, wordsize);
611   return sp;
612 }
613
614 /* Function: pop_frame
615    Restore the machine to the state it had before the current frame 
616    was created.  Usually used either by the "RETURN" command, or by
617    call_function_by_hand after the dummy_frame is finished. */
618
619 void
620 h8300_pop_frame (void)
621 {
622   unsigned regnum;
623   struct frame_saved_regs fsr;
624   struct frame_info *frame = get_current_frame ();
625
626   if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
627     {
628       generic_pop_dummy_frame ();
629     }
630   else
631     {
632       get_frame_saved_regs (frame, &fsr);
633
634       for (regnum = 0; regnum < 8; regnum++)
635         {
636           /* Don't forget SP_REGNUM is a frame_saved_regs struct is the
637              actual value we want, not the address of the value we want.  */
638           if (fsr.regs[regnum] && regnum != SP_REGNUM)
639             write_register (regnum,
640                             read_memory_integer (fsr.regs[regnum], BINWORD));
641           else if (fsr.regs[regnum] && regnum == SP_REGNUM)
642             write_register (regnum, frame->frame + 2 * BINWORD);
643         }
644
645       /* Don't forget the update the PC too!  */
646       write_pc (frame->from_pc);
647     }
648   flush_cached_frames ();
649 }
650
651 /* Function: extract_return_value
652    Figure out where in REGBUF the called function has left its return value.
653    Copy that into VALBUF.  Be sure to account for CPU type.   */
654
655 void
656 h8300_extract_return_value (struct type *type, char *regbuf, char *valbuf)
657 {
658   int wordsize, len;
659
660   if (h8300smode || h8300hmode)
661     wordsize = 4;
662   else
663     wordsize = 2;
664
665   len = TYPE_LENGTH (type);
666
667   switch (len)
668     {
669     case 1:                     /* (char) */
670     case 2:                     /* (short), (int) */
671       memcpy (valbuf, regbuf + REGISTER_BYTE (0) + (wordsize - len), len);
672       break;
673     case 4:                     /* (long), (float) */
674       if (h8300smode || h8300hmode)
675         {
676           memcpy (valbuf, regbuf + REGISTER_BYTE (0), 4);
677         }
678       else
679         {
680           memcpy (valbuf, regbuf + REGISTER_BYTE (0), 2);
681           memcpy (valbuf + 2, regbuf + REGISTER_BYTE (1), 2);
682         }
683       break;
684     case 8:                     /* (double) (doesn't seem to happen, which is good,
685                                    because this almost certainly isn't right.  */
686       error ("I don't know how a double is returned.");
687       break;
688     }
689 }
690
691 /* Function: store_return_value
692    Place the appropriate value in the appropriate registers.
693    Primarily used by the RETURN command.  */
694
695 void
696 h8300_store_return_value (struct type *type, char *valbuf)
697 {
698   int wordsize, len, regval;
699
700   if (h8300hmode || h8300smode)
701     wordsize = 4;
702   else
703     wordsize = 2;
704
705   len = TYPE_LENGTH (type);
706   switch (len)
707     {
708     case 1:                     /* char */
709     case 2:                     /* short, int */
710       regval = extract_address (valbuf, len);
711       write_register (0, regval);
712       break;
713     case 4:                     /* long, float */
714       regval = extract_address (valbuf, len);
715       if (h8300smode || h8300hmode)
716         {
717           write_register (0, regval);
718         }
719       else
720         {
721           write_register (0, regval >> 16);
722           write_register (1, regval & 0xffff);
723         }
724       break;
725     case 8:                     /* presumeably double, but doesn't seem to happen */
726       error ("I don't know how to return a double.");
727       break;
728     }
729 }
730
731 struct cmd_list_element *setmemorylist;
732
733 static void
734 set_register_names (void)
735 {
736   if (h8300hmode != 0)
737     h8300_register_names = h8300h_register_names;
738   else
739     h8300_register_names = original_register_names;
740 }
741
742 static void
743 h8300_command (char *args, int from_tty)
744 {
745   extern int h8300hmode;
746   h8300hmode = 0;
747   h8300smode = 0;
748   set_register_names ();
749 }
750
751 static void
752 h8300h_command (char *args, int from_tty)
753 {
754   extern int h8300hmode;
755   h8300hmode = 1;
756   h8300smode = 0;
757   set_register_names ();
758 }
759
760 static void
761 h8300s_command (char *args, int from_tty)
762 {
763   extern int h8300smode;
764   extern int h8300hmode;
765   h8300smode = 1;
766   h8300hmode = 1;
767   set_register_names ();
768 }
769
770
771 static void
772 set_machine (char *args, int from_tty)
773 {
774   printf_unfiltered ("\"set machine\" must be followed by h8300, h8300h");
775   printf_unfiltered ("or h8300s");
776   help_list (setmemorylist, "set memory ", -1, gdb_stdout);
777 }
778
779 /* set_machine_hook is called as the exec file is being opened, but
780    before the symbol file is opened.  This allows us to set the
781    h8300hmode flag based on the machine type specified in the exec
782    file.  This in turn will cause subsequently defined pointer types
783    to be 16 or 32 bits as appropriate for the machine.  */
784
785 static void
786 set_machine_hook (char *filename)
787 {
788   if (bfd_get_mach (exec_bfd) == bfd_mach_h8300s)
789     {
790       h8300smode = 1;
791       h8300hmode = 1;
792     }
793   else if (bfd_get_mach (exec_bfd) == bfd_mach_h8300h)
794     {
795       h8300smode = 0;
796       h8300hmode = 1;
797     }
798   else
799     {
800       h8300smode = 0;
801       h8300hmode = 0;
802     }
803   set_register_names ();
804 }
805
806 void
807 _initialize_h8300m (void)
808 {
809   add_prefix_cmd ("machine", no_class, set_machine,
810                   "set the machine type",
811                   &setmemorylist, "set machine ", 0,
812                   &setlist);
813
814   add_cmd ("h8300", class_support, h8300_command,
815            "Set machine to be H8/300.", &setmemorylist);
816
817   add_cmd ("h8300h", class_support, h8300h_command,
818            "Set machine to be H8/300H.", &setmemorylist);
819
820   add_cmd ("h8300s", class_support, h8300s_command,
821            "Set machine to be H8/300S.", &setmemorylist);
822
823   /* Add a hook to set the machine type when we're loading a file. */
824
825   specify_exec_file_hook (set_machine_hook);
826 }
827
828
829
830 void
831 print_register_hook (int regno)
832 {
833   if (regno == 8)
834     {
835       /* CCR register */
836       int C, Z, N, V;
837       unsigned char b[4];
838       unsigned char l;
839       read_relative_register_raw_bytes (regno, b);
840       l = b[REGISTER_VIRTUAL_SIZE (8) - 1];
841       printf_unfiltered ("\t");
842       printf_unfiltered ("I-%d - ", (l & 0x80) != 0);
843       printf_unfiltered ("H-%d - ", (l & 0x20) != 0);
844       N = (l & 0x8) != 0;
845       Z = (l & 0x4) != 0;
846       V = (l & 0x2) != 0;
847       C = (l & 0x1) != 0;
848       printf_unfiltered ("N-%d ", N);
849       printf_unfiltered ("Z-%d ", Z);
850       printf_unfiltered ("V-%d ", V);
851       printf_unfiltered ("C-%d ", C);
852       if ((C | Z) == 0)
853         printf_unfiltered ("u> ");
854       if ((C | Z) == 1)
855         printf_unfiltered ("u<= ");
856       if ((C == 0))
857         printf_unfiltered ("u>= ");
858       if (C == 1)
859         printf_unfiltered ("u< ");
860       if (Z == 0)
861         printf_unfiltered ("!= ");
862       if (Z == 1)
863         printf_unfiltered ("== ");
864       if ((N ^ V) == 0)
865         printf_unfiltered (">= ");
866       if ((N ^ V) == 1)
867         printf_unfiltered ("< ");
868       if ((Z | (N ^ V)) == 0)
869         printf_unfiltered ("> ");
870       if ((Z | (N ^ V)) == 1)
871         printf_unfiltered ("<= ");
872     }
873 }
874
875 void
876 _initialize_h8300_tdep (void)
877 {
878   tm_print_insn = gdb_print_insn_h8300;
879 }