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