* Makefile.in: add new files remote-vx29k.c, config/a29k/tm-vx29k.h,
[deliverable/binutils-gdb.git] / gdb / h8500-tdep.c
CommitLineData
195e46ea 1/* Target-machine dependent code for Hitachi H8/500, for GDB.
669caa9c 2 Copyright (C) 1993, 1994 Free Software Foundation, Inc.
195e46ea
SC
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
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20/*
21 Contributed by Steve Chamberlain
22 sac@cygnus.com
23 */
24
25#include "defs.h"
26#include "frame.h"
27#include "obstack.h"
28#include "symtab.h"
29#include "gdbtypes.h"
30#include "gdbcmd.h"
ccf1e898 31#include "value.h"
195e46ea
SC
32#include "dis-asm.h"
33#include "../opcodes/h8500-opc.h"
34;
195e46ea
SC
35
36#define UNSIGNED_SHORT(X) ((X) & 0xffff)
edd01519
SC
37int code_size = 2;
38int data_size = 2;
195e46ea 39
85e07872 40/* Shape of an H8/500 frame :
195e46ea
SC
41
42
43 arg-n
44 ..
45 arg-2
46 arg-1
47 return address <2 or 4 bytes>
48 old fp <2 bytes>
49 auto-n
50 ..
51 auto-1
52 saved registers
53
54*/
55
56
57/* an easy to debug H8 stack frame looks like:
580x6df6 push r6
590x0d76 mov.w r7,r6
600x6dfn push reg
610x7905 nnnn mov.w #n,r5 or 0x1b87 subs #2,sp
620x1957 sub.w r5,sp
63
64 */
65
d1445327 66#define IS_PUSH(x) (((x) & 0xff00)==0x6d00)
195e46ea
SC
67#define IS_LINK_8(x) ((x) == 0x17)
68#define IS_LINK_16(x) ((x) == 0x1f)
d1445327
FF
69#define IS_MOVE_FP(x) ((x) == 0x0d76)
70#define IS_MOV_SP_FP(x) ((x) == 0x0d76)
71#define IS_SUB2_SP(x) ((x) == 0x1b87)
72#define IS_MOVK_R5(x) ((x) == 0x7905)
73#define IS_SUB_R5SP(x) ((x) == 0x1957)
195e46ea
SC
74
75#define LINK_8 0x17
76#define LINK_16 0x1f
77
78int minimum_mode = 1;
79CORE_ADDR examine_prologue ();
80
81void frame_find_saved_regs ();
ccf1e898 82
ccf1e898 83
195e46ea
SC
84CORE_ADDR
85h8500_skip_prologue (start_pc)
86 CORE_ADDR start_pc;
87
88{
89 short int w;
90
08c0d7b8 91 w = read_memory_integer (start_pc, 1);
195e46ea
SC
92 if (w == LINK_8)
93 {
ccf1e898 94 start_pc += 2;
85e07872 95 w = read_memory_integer (start_pc, 1);
195e46ea
SC
96 }
97
98 if (w == LINK_16)
99 {
ccf1e898 100 start_pc += 3;
85e07872 101 w = read_memory_integer (start_pc, 2);
195e46ea
SC
102 }
103
195e46ea 104 return start_pc;
195e46ea
SC
105}
106
107int
108print_insn (memaddr, stream)
109 CORE_ADDR memaddr;
199b2450 110 GDB_FILE *stream;
195e46ea 111{
195e46ea 112 disassemble_info info;
85e07872 113 GDB_INIT_DISASSEMBLE_INFO (info, stream);
5d0734a7 114 return print_insn_h8500 (memaddr, &info);
195e46ea
SC
115}
116
117/* Given a GDB frame, determine the address of the calling function's frame.
118 This will be used to create a new GDB frame struct, and then
119 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
120
121 For us, the frame address is its stack pointer value, so we look up
122 the function prologue to determine the caller's sp value, and return it. */
123
669caa9c 124CORE_ADDR
ccf1e898 125h8500_frame_chain (thisframe)
669caa9c 126 struct frame_info *thisframe;
195e46ea 127{
ccf1e898 128 if (!inside_entry_file (thisframe->pc))
08c0d7b8 129 return (read_memory_integer (FRAME_FP (thisframe), PTR_SIZE));
ccf1e898
SG
130 else
131 return 0;
195e46ea
SC
132}
133
195e46ea
SC
134
135/* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
136 is not the address of a valid instruction, the address of the next
137 instruction beyond ADDR otherwise. *PWORD1 receives the first word
138 of the instruction.*/
139
140CORE_ADDR
141NEXT_PROLOGUE_INSN (addr, lim, pword1)
142 CORE_ADDR addr;
143 CORE_ADDR lim;
144 char *pword1;
145{
146 if (addr < lim + 8)
147 {
148 read_memory (addr, pword1, 1);
149 read_memory (addr, pword1 + 1, 1);
150 return 1;
151 }
152 return 0;
153}
154
155/* Examine the prologue of a function. `ip' points to the first instruction.
156 `limit' is the limit of the prologue (e.g. the addr of the first
157 linenumber, or perhaps the program counter if we're stepping through).
158 `frame_sp' is the stack pointer value in use in this frame.
159 `fsr' is a pointer to a frame_saved_regs structure into which we put
160 info about the registers saved by this frame.
161 `fi' is a struct frame_info pointer; we fill in various fields in it
162 to reflect the offsets of the arg pointer and the locals pointer. */
d1445327 163
195e46ea
SC
164
165/* Return the saved PC from this frame. */
166
167CORE_ADDR
168frame_saved_pc (frame)
669caa9c 169 struct frame_info *frame;
195e46ea 170{
669caa9c 171 return read_memory_integer (FRAME_FP (frame) + 2, PTR_SIZE);
195e46ea
SC
172}
173
174CORE_ADDR
175frame_locals_address (fi)
176 struct frame_info *fi;
177{
178 return fi->frame;
179}
180
181/* Return the address of the argument block for the frame
182 described by FI. Returns 0 if the address is unknown. */
183
184CORE_ADDR
185frame_args_address (fi)
186 struct frame_info *fi;
187{
ccf1e898 188 return fi->frame;
195e46ea
SC
189}
190
191void
192h8300_pop_frame ()
193{
194 unsigned regnum;
195 struct frame_saved_regs fsr;
669caa9c 196 struct frame_info *frame = get_current_frame ();
195e46ea 197
669caa9c 198 get_frame_saved_regs (frame, &fsr);
195e46ea
SC
199
200 for (regnum = 0; regnum < 8; regnum++)
201 {
202 if (fsr.regs[regnum])
195e46ea 203 write_register (regnum, read_memory_short (fsr.regs[regnum]));
195e46ea
SC
204
205 flush_cached_frames ();
195e46ea
SC
206 }
207
208}
209
210void
211print_register_hook (regno)
212{
213 if (regno == CCR_REGNUM)
214 {
215 /* CCR register */
216
217 int C, Z, N, V;
218 unsigned char b[2];
219 unsigned char l;
220
221 read_relative_register_raw_bytes (regno, b);
222 l = b[1];
199b2450
TL
223 printf_unfiltered ("\t");
224 printf_unfiltered ("I-%d - ", (l & 0x80) != 0);
195e46ea
SC
225 N = (l & 0x8) != 0;
226 Z = (l & 0x4) != 0;
227 V = (l & 0x2) != 0;
228 C = (l & 0x1) != 0;
199b2450
TL
229 printf_unfiltered ("N-%d ", N);
230 printf_unfiltered ("Z-%d ", Z);
231 printf_unfiltered ("V-%d ", V);
232 printf_unfiltered ("C-%d ", C);
195e46ea 233 if ((C | Z) == 0)
199b2450 234 printf_unfiltered ("u> ");
195e46ea 235 if ((C | Z) == 1)
199b2450 236 printf_unfiltered ("u<= ");
195e46ea 237 if ((C == 0))
199b2450 238 printf_unfiltered ("u>= ");
195e46ea 239 if (C == 1)
199b2450 240 printf_unfiltered ("u< ");
195e46ea 241 if (Z == 0)
199b2450 242 printf_unfiltered ("!= ");
195e46ea 243 if (Z == 1)
199b2450 244 printf_unfiltered ("== ");
195e46ea 245 if ((N ^ V) == 0)
199b2450 246 printf_unfiltered (">= ");
195e46ea 247 if ((N ^ V) == 1)
199b2450 248 printf_unfiltered ("< ");
195e46ea 249 if ((Z | (N ^ V)) == 0)
199b2450 250 printf_unfiltered ("> ");
195e46ea 251 if ((Z | (N ^ V)) == 1)
199b2450 252 printf_unfiltered ("<= ");
195e46ea
SC
253 }
254}
255
ccf1e898
SG
256int
257h8500_register_size (regno)
258 int regno;
195e46ea 259{
08c0d7b8
SC
260 switch (regno) {
261 case SEG_C_REGNUM:
262 case SEG_D_REGNUM:
263 case SEG_E_REGNUM:
264 case SEG_T_REGNUM:
ccf1e898 265 return 1;
08c0d7b8
SC
266 case R0_REGNUM:
267 case R1_REGNUM:
268 case R2_REGNUM:
269 case R3_REGNUM:
270 case R4_REGNUM:
271 case R5_REGNUM:
272 case R6_REGNUM:
273 case R7_REGNUM:
274 case CCR_REGNUM:
275 return 2;
276
277 case PR0_REGNUM:
278 case PR1_REGNUM:
279 case PR2_REGNUM:
280 case PR3_REGNUM:
281 case PR4_REGNUM:
282 case PR5_REGNUM:
283 case PR6_REGNUM:
284 case PR7_REGNUM:
285 case PC_REGNUM:
286 return 4;
287 }
195e46ea
SC
288}
289
290struct type *
ccf1e898
SG
291h8500_register_virtual_type (regno)
292 int regno;
195e46ea 293{
ccf1e898 294 switch (regno)
195e46ea 295 {
ccf1e898
SG
296 case SEG_C_REGNUM:
297 case SEG_E_REGNUM:
298 case SEG_D_REGNUM:
299 case SEG_T_REGNUM:
195e46ea 300 return builtin_type_unsigned_char;
ccf1e898
SG
301 case R0_REGNUM:
302 case R1_REGNUM:
303 case R2_REGNUM:
304 case R3_REGNUM:
305 case R4_REGNUM:
306 case R5_REGNUM:
307 case R6_REGNUM:
308 case R7_REGNUM:
195e46ea
SC
309 case CCR_REGNUM:
310 return builtin_type_unsigned_short;
08c0d7b8
SC
311 case PR0_REGNUM:
312 case PR1_REGNUM:
313 case PR2_REGNUM:
314 case PR3_REGNUM:
315 case PR4_REGNUM:
316 case PR5_REGNUM:
317 case PR6_REGNUM:
318 case PR7_REGNUM:
319 case PC_REGNUM:
320 return builtin_type_unsigned_long;
195e46ea 321 default:
85e07872 322 abort ();
195e46ea
SC
323 }
324}
325
195e46ea
SC
326/* Put here the code to store, into a struct frame_saved_regs,
327 the addresses of the saved registers of frame described by FRAME_INFO.
328 This includes special registers such as pc and fp saved in special
329 ways in the stack frame. sp is even more special:
330 the address we return for it IS the sp for the next frame. */
331
332void
333frame_find_saved_regs (frame_info, frame_saved_regs)
334 struct frame_info *frame_info;
335 struct frame_saved_regs *frame_saved_regs;
336
337{
338 register int regnum;
339 register int regmask;
340 register CORE_ADDR next_addr;
341 register CORE_ADDR pc;
342 unsigned char thebyte;
343
4ed97c9a 344 memset (frame_saved_regs, '\0', sizeof *frame_saved_regs);
195e46ea
SC
345
346 if ((frame_info)->pc >= (frame_info)->frame - CALL_DUMMY_LENGTH - FP_REGNUM * 4 - 4
347 && (frame_info)->pc <= (frame_info)->frame)
348 {
349 next_addr = (frame_info)->frame;
350 pc = (frame_info)->frame - CALL_DUMMY_LENGTH - FP_REGNUM * 4 - 4;
351 }
352 else
353 {
354 pc = get_pc_function_start ((frame_info)->pc);
355 /* Verify we have a link a6 instruction next;
356 if not we lose. If we win, find the address above the saved
357 regs using the amount of storage from the link instruction.
358 */
359
85e07872 360 thebyte = read_memory_integer (pc, 1);
195e46ea
SC
361 if (0x1f == thebyte)
362 next_addr = (frame_info)->frame + read_memory_integer (pc += 1, 2), pc += 2;
363 else if (0x17 == thebyte)
364 next_addr = (frame_info)->frame + read_memory_integer (pc += 1, 1), pc += 1;
365 else
366 goto lose;
367#if 0
d1445327 368 /* FIXME steve */
85e07872
SC
369 /* If have an add:g.waddal #-n, sp next, adjust next_addr. */
370 if ((0x0c0177777 & read_memory_integer (pc, 2)) == 0157774)
371 next_addr += read_memory_integer (pc += 2, 4), pc += 4;
195e46ea
SC
372#endif
373 }
374
85e07872
SC
375 thebyte = read_memory_integer (pc, 1);
376 if (thebyte == 0x12)
377 {
378 /* Got stm */
379 pc++;
380 regmask = read_memory_integer (pc, 1);
381 pc++;
382 for (regnum = 0; regnum < 8; regnum++, regmask >>= 1)
383 {
384 if (regmask & 1)
385 {
386 (frame_saved_regs)->regs[regnum] = (next_addr += 2) - 2;
387 }
388 }
389 thebyte = read_memory_integer (pc, 1);
390 }
195e46ea 391 /* Maybe got a load of pushes */
85e07872
SC
392 while (thebyte == 0xbf)
393 {
394 pc++;
395 regnum = read_memory_integer (pc, 1) & 0x7;
396 pc++;
397 (frame_saved_regs)->regs[regnum] = (next_addr += 2) - 2;
398 thebyte = read_memory_integer (pc, 1);
399 }
400
401lose:;
402
195e46ea
SC
403 /* Remember the address of the frame pointer */
404 (frame_saved_regs)->regs[FP_REGNUM] = (frame_info)->frame;
405
406 /* This is where the old sp is hidden */
407 (frame_saved_regs)->regs[SP_REGNUM] = (frame_info)->frame;
408
409 /* And the PC - remember the pushed FP is always two bytes long */
410 (frame_saved_regs)->regs[PC_REGNUM] = (frame_info)->frame + 2;
411}
412
85e07872 413saved_pc_after_call (frame)
195e46ea
SC
414{
415 int x;
85e07872 416 int a = read_register (SP_REGNUM);
edd01519
SC
417 x = read_memory_integer (a, code_size);
418 if (code_size == 2)
419 {
420 /* Stick current code segement onto top */
421 x &= 0xffff;
422 x |= read_register (SEG_C_REGNUM) << 16;
423 }
424 x &= 0xffffff;
195e46ea
SC
425 return x;
426}
427
428
429/* Nonzero if instruction at PC is a return instruction. */
430
85e07872 431about_to_return (pc)
195e46ea 432{
85e07872 433 int b1 = read_memory_integer (pc, 1);
195e46ea 434
85e07872 435 switch (b1)
195e46ea
SC
436 {
437 case 0x14: /* rtd #8 */
438 case 0x1c: /* rtd #16 */
439 case 0x19: /* rts */
440 case 0x1a: /* rte */
441 return 1;
442 case 0x11:
443 {
85e07872
SC
444 int b2 = read_memory_integer (pc + 1, 1);
445 switch (b2)
195e46ea
SC
446 {
447 case 0x18: /* prts */
448 case 0x14: /* prtd #8 */
449 case 0x16: /* prtd #16 */
450 return 1;
451 }
452 }
453 }
454 return 0;
455}
456
457
458void
459h8500_set_pointer_size (newsize)
460 int newsize;
461{
462 static int oldsize = 0;
463
464 if (oldsize != newsize)
465 {
199b2450 466 printf_unfiltered ("pointer size set to %d bits\n", newsize);
195e46ea
SC
467 oldsize = newsize;
468 if (newsize == 32)
469 {
470 minimum_mode = 0;
471 }
472 else
473 {
474 minimum_mode = 1;
475 }
476 _initialize_gdbtypes ();
477 }
478}
479
480
481struct cmd_list_element *setmemorylist;
482
483
edd01519 484#define C(name,a,b,c) name () { h8500_set_pointer_size(a); code_size = b; data_size = c; }
195e46ea 485
63eef03a 486C(big_command, 32,4,4);
edd01519
SC
487C(medium_command, 32, 4,2);
488C(compact_command, 32,2,4);
489C(small_command, 16,2,2);
195e46ea
SC
490
491static void
492set_memory (args, from_tty)
493 char *args;
494 int from_tty;
495{
199b2450
TL
496 printf_unfiltered ("\"set memory\" must be followed by the name of a memory subcommand.\n");
497 help_list (setmemorylist, "set memory ", -1, gdb_stdout);
195e46ea
SC
498}
499
ccf1e898 500/* See if variable name is ppc or pr[0-7] */
195e46ea 501
ccf1e898
SG
502int
503h8500_is_trapped_internalvar (name)
504 char *name;
505{
506 if (name[0] != 'p')
507 return 0;
508
85e07872 509 if (strcmp (name + 1, "pc") == 0)
ccf1e898
SG
510 return 1;
511
512 if (name[1] == 'r'
513 && name[2] >= '0'
514 && name[2] <= '7'
515 && name[3] == '\000')
516 return 1;
517 else
518 return 0;
519}
520
63eef03a 521value_ptr
ccf1e898
SG
522h8500_value_of_trapped_internalvar (var)
523 struct internalvar *var;
524{
525 LONGEST regval;
526 unsigned char regbuf[4];
527 int page_regnum, regnum;
528
529 regnum = var->name[2] == 'c' ? PC_REGNUM : var->name[2] - '0';
530
531 switch (var->name[2])
532 {
533 case 'c':
534 page_regnum = SEG_C_REGNUM;
535 break;
85e07872
SC
536 case '0':
537 case '1':
538 case '2':
539 case '3':
ccf1e898
SG
540 page_regnum = SEG_D_REGNUM;
541 break;
85e07872
SC
542 case '4':
543 case '5':
ccf1e898
SG
544 page_regnum = SEG_E_REGNUM;
545 break;
85e07872
SC
546 case '6':
547 case '7':
ccf1e898
SG
548 page_regnum = SEG_T_REGNUM;
549 break;
550 }
551
552 get_saved_register (regbuf, NULL, NULL, selected_frame, page_regnum, NULL);
553 regval = regbuf[0] << 16;
554
555 get_saved_register (regbuf, NULL, NULL, selected_frame, regnum, NULL);
556 regval |= regbuf[0] << 8 | regbuf[1]; /* XXX host/target byte order */
557
558 free (var->value); /* Free up old value */
559
560 var->value = value_from_longest (builtin_type_unsigned_long, regval);
561 release_value (var->value); /* Unchain new value */
562
563 VALUE_LVAL (var->value) = lval_internalvar;
564 VALUE_INTERNALVAR (var->value) = var;
565 return var->value;
566}
567
568void
569h8500_set_trapped_internalvar (var, newval, bitpos, bitsize, offset)
570 struct internalvar *var;
571 int offset, bitpos, bitsize;
63eef03a 572 value_ptr newval;
195e46ea 573{
ccf1e898
SG
574 char *page_regnum, *regnum;
575 char expression[100];
576 unsigned new_regval;
577 struct type *type;
578 enum type_code newval_type_code;
579
580 type = VALUE_TYPE (newval);
581 newval_type_code = TYPE_CODE (type);
582
583 if ((newval_type_code != TYPE_CODE_INT
584 && newval_type_code != TYPE_CODE_PTR)
85e07872
SC
585 || TYPE_LENGTH (type) != sizeof (new_regval))
586 error ("Illegal type (%s) for assignment to $%s\n",
587 TYPE_NAME (type), var->name);
195e46ea 588
85e07872 589 new_regval = *(long *) VALUE_CONTENTS_RAW (newval);
ccf1e898
SG
590
591 regnum = var->name + 1;
592
593 switch (var->name[2])
594 {
595 case 'c':
596 page_regnum = "cp";
597 break;
85e07872
SC
598 case '0':
599 case '1':
600 case '2':
601 case '3':
ccf1e898
SG
602 page_regnum = "dp";
603 break;
85e07872
SC
604 case '4':
605 case '5':
ccf1e898
SG
606 page_regnum = "ep";
607 break;
85e07872
SC
608 case '6':
609 case '7':
ccf1e898
SG
610 page_regnum = "tp";
611 break;
612 }
613
614 sprintf (expression, "$%s=%d", page_regnum, new_regval >> 16);
85e07872 615 parse_and_eval (expression);
ccf1e898
SG
616
617 sprintf (expression, "$%s=%d", regnum, new_regval & 0xffff);
85e07872 618 parse_and_eval (expression);
ccf1e898
SG
619}
620
976bb0be 621void
ccf1e898
SG
622_initialize_h8500_tdep ()
623{
195e46ea
SC
624 add_prefix_cmd ("memory", no_class, set_memory,
625 "set the memory model", &setmemorylist, "set memory ", 0,
626 &setlist);
edd01519
SC
627
628 add_cmd ("small", class_support, small_command,
629 "Set small memory model. (16 bit code, 16 bit data)", &setmemorylist);
630
63eef03a
SC
631 add_cmd ("big", class_support, big_command,
632 "Set big memory model. (32 bit code, 32 bit data)", &setmemorylist);
edd01519
SC
633
634 add_cmd ("medium", class_support, medium_command,
635 "Set medium memory model. (32 bit code, 16 bit data)", &setmemorylist);
636
637 add_cmd ("compact", class_support, compact_command,
638 "Set compact memory model. (16 bit code, 32 bit data)", &setmemorylist);
195e46ea
SC
639
640}
85e07872
SC
641
642CORE_ADDR
f4eb9968 643h8500_read_sp ()
85e07872 644{
08c0d7b8 645 return read_register (PR7_REGNUM);
85e07872
SC
646}
647
648void
f4eb9968 649h8500_write_sp (v)
85e07872
SC
650 CORE_ADDR v;
651{
08c0d7b8 652 write_register (PR7_REGNUM, v);
85e07872
SC
653}
654
655CORE_ADDR
f4eb9968
SS
656h8500_read_pc (pid)
657 int pid;
85e07872 658{
08c0d7b8 659 return read_register (PC_REGNUM);
85e07872
SC
660}
661
662void
f4eb9968 663h8500_write_pc (v, pid)
85e07872 664 CORE_ADDR v;
f4eb9968 665 int pid;
85e07872 666{
08c0d7b8 667 write_register (PC_REGNUM, v);
85e07872
SC
668}
669
670CORE_ADDR
f4eb9968 671h8500_read_fp ()
85e07872 672{
08c0d7b8 673 return read_register (PR6_REGNUM);
85e07872
SC
674}
675
676void
f4eb9968 677h8500_write_fp (v)
85e07872
SC
678 CORE_ADDR v;
679{
08c0d7b8 680 write_register (PR6_REGNUM, v);
85e07872 681}
1468bec9 682
This page took 0.145478 seconds and 4 git commands to generate.