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