Fix whitespace problem in my most recent entry.
[deliverable/binutils-gdb.git] / gdb / h8500-tdep.c
CommitLineData
c906108c 1/* Target-dependent code for Hitachi H8/500, for GDB.
4e052eda 2 Copyright 1993, 1994, 1995, 2001 Free Software Foundation, Inc.
c906108c 3
c5aa993b 4 This file is part of GDB.
c906108c 5
c5aa993b
JM
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.
c906108c 10
c5aa993b
JM
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.
c906108c 15
c5aa993b
JM
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. */
c906108c
SS
20
21/*
c5aa993b
JM
22 Contributed by Steve Chamberlain
23 sac@cygnus.com
c906108c
SS
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"
4e052eda 35#include "regcache.h"
c906108c
SS
36
37#define UNSIGNED_SHORT(X) ((X) & 0xffff)
38
39static int code_size = 2;
40
41static 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>
c5aa993b 50 old fp <2 bytes>
c906108c
SS
51 auto-n
52 ..
53 auto-1
54 saved registers
55
c5aa993b 56 */
c906108c
SS
57
58/* an easy to debug H8 stack frame looks like:
c5aa993b
JM
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
c906108c
SS
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
79int minimum_mode = 1;
80
81CORE_ADDR
fba45db2 82h8500_skip_prologue (CORE_ADDR start_pc)
c906108c
SS
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
102CORE_ADDR
fba45db2 103h8500_addr_bits_remove (CORE_ADDR addr)
c906108c
SS
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
115CORE_ADDR
fba45db2 116h8500_frame_chain (struct frame_info *thisframe)
c906108c
SS
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
c5aa993b 127 of the instruction. */
c906108c
SS
128
129CORE_ADDR
fba45db2 130NEXT_PROLOGUE_INSN (CORE_ADDR addr, CORE_ADDR lim, char *pword1)
c906108c
SS
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
153CORE_ADDR
fba45db2 154frame_saved_pc (struct frame_info *frame)
c906108c
SS
155{
156 return read_memory_integer (FRAME_FP (frame) + 2, PTR_SIZE);
157}
158
c5aa993b 159void
fba45db2 160h8500_pop_frame (void)
c906108c
SS
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
177void
fba45db2 178print_register_hook (int regno)
c906108c
SS
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
223int
fba45db2 224h8500_register_size (int regno)
c906108c
SS
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:
e1e9e218 255 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c
SS
256 }
257}
258
259struct type *
fba45db2 260h8500_register_virtual_type (int regno)
c906108c
SS
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:
e1e9e218 290 internal_error (__FILE__, __LINE__, "failed internal consistency check");
c906108c
SS
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
300void
fba45db2
KB
301frame_find_saved_regs (struct frame_info *frame_info,
302 struct frame_saved_regs *frame_saved_regs)
c906108c
SS
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;
c5aa993b
JM
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 */
c906108c
SS
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
367lose:;
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
379CORE_ADDR
fba45db2 380saved_pc_after_call (void)
c906108c
SS
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
396void
fba45db2 397h8500_set_pointer_size (int newsize)
c906108c
SS
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
417static void
55d80160 418big_command (char *arg, int from_tty)
c906108c
SS
419{
420 h8500_set_pointer_size (32);
421 code_size = 4;
422 data_size = 4;
423}
424
425static void
55d80160 426medium_command (char *arg, int from_tty)
c906108c
SS
427{
428 h8500_set_pointer_size (32);
429 code_size = 4;
430 data_size = 2;
431}
432
433static void
55d80160 434compact_command (char *arg, int from_tty)
c906108c
SS
435{
436 h8500_set_pointer_size (32);
437 code_size = 2;
438 data_size = 4;
439}
440
441static void
55d80160 442small_command (char *arg, int from_tty)
c906108c
SS
443{
444 h8500_set_pointer_size (16);
445 code_size = 2;
446 data_size = 2;
447}
448
449static struct cmd_list_element *setmemorylist;
450
451static void
fba45db2 452set_memory (char *args, int from_tty)
c906108c
SS
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
460int
fba45db2 461h8500_is_trapped_internalvar (char *name)
c906108c
SS
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
478value_ptr
fba45db2 479h8500_value_of_trapped_internalvar (struct internalvar *var)
c906108c
SS
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);
c5aa993b 512 regval |= regbuf[0] << 8 | regbuf[1]; /* XXX host/target byte order */
c906108c 513
b8c9b27d 514 xfree (var->value); /* Free up old value */
c906108c
SS
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
524void
fba45db2
KB
525h8500_set_trapped_internalvar (struct internalvar *var, value_ptr newval,
526 int bitpos, int bitsize, int offset)
c906108c
SS
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
575CORE_ADDR
fba45db2 576h8500_read_sp (void)
c906108c
SS
577{
578 return read_register (PR7_REGNUM);
579}
580
581void
fba45db2 582h8500_write_sp (CORE_ADDR v)
c906108c
SS
583{
584 write_register (PR7_REGNUM, v);
585}
586
587CORE_ADDR
fba45db2 588h8500_read_pc (int pid)
c906108c
SS
589{
590 return read_register (PC_REGNUM);
591}
592
593void
fba45db2 594h8500_write_pc (CORE_ADDR v, int pid)
c906108c
SS
595{
596 write_register (PC_REGNUM, v);
597}
598
599CORE_ADDR
fba45db2 600h8500_read_fp (void)
c906108c
SS
601{
602 return read_register (PR6_REGNUM);
603}
604
605void
fba45db2 606h8500_write_fp (CORE_ADDR v)
c906108c
SS
607{
608 write_register (PR6_REGNUM, v);
609}
610
611void
fba45db2 612_initialize_h8500_tdep (void)
c906108c
SS
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,
c5aa993b 621 "Set small memory model. (16 bit code, 16 bit data)", &setmemorylist);
c906108c
SS
622
623 add_cmd ("big", class_support, big_command,
c5aa993b 624 "Set big memory model. (32 bit code, 32 bit data)", &setmemorylist);
c906108c
SS
625
626 add_cmd ("medium", class_support, medium_command,
c5aa993b 627 "Set medium memory model. (32 bit code, 16 bit data)", &setmemorylist);
c906108c
SS
628
629 add_cmd ("compact", class_support, compact_command,
c5aa993b 630 "Set compact memory model. (16 bit code, 32 bit data)", &setmemorylist);
c906108c
SS
631
632}
This page took 0.105529 seconds and 4 git commands to generate.