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