* procfs.c (procfs_thread_alive procfs_stop): Make static.
[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
a3059251 36
256b4f37
SC
37#undef NUM_REGS
38#define NUM_REGS 11
39
1f46923f 40#define UNSIGNED_SHORT(X) ((X) & 0xffff)
400943fb
SC
41
42/* an easy to debug H8 stack frame looks like:
ec25d19b
SC
430x6df6 push r6
440x0d76 mov.w r7,r6
450x6dfn push reg
460x7905 nnnn mov.w #n,r5 or 0x1b87 subs #2,sp
470x1957 sub.w r5,sp
400943fb
SC
48
49 */
1f46923f 50
400943fb 51#define IS_PUSH(x) ((x & 0xff00)==0x6d00)
ec25d19b 52#define IS_PUSH_FP(x) (x == 0x6df6)
1f46923f
SC
53#define IS_MOVE_FP(x) (x == 0x0d76)
54#define IS_MOV_SP_FP(x) (x == 0x0d76)
55#define IS_SUB2_SP(x) (x==0x1b87)
56#define IS_MOVK_R5(x) (x==0x7905)
ec25d19b 57#define IS_SUB_R5SP(x) (x==0x1957)
1ca9e7c9 58
f9fedc48
MA
59/* Local function declarations. */
60
1ca9e7c9 61static CORE_ADDR examine_prologue ();
f9fedc48 62static void set_machine_hook PARAMS ((char *filename));
1f46923f 63
ec25d19b
SC
64void frame_find_saved_regs ();
65CORE_ADDR
66h8300_skip_prologue (start_pc)
67 CORE_ADDR start_pc;
0a8f9d31 68{
ec25d19b 69 short int w;
1f46923f 70
df14b38b 71 w = read_memory_unsigned_integer (start_pc, 2);
400943fb 72 /* Skip past all push insns */
ec25d19b
SC
73 while (IS_PUSH_FP (w))
74 {
75 start_pc += 2;
df14b38b 76 w = read_memory_unsigned_integer (start_pc, 2);
ec25d19b 77 }
0a8f9d31 78
1f46923f 79 /* Skip past a move to FP */
ec25d19b
SC
80 if (IS_MOVE_FP (w))
81 {
82 start_pc += 2;
df14b38b 83 w = read_memory_unsigned_integer (start_pc, 2);
1f46923f
SC
84 }
85
ec25d19b 86 /* Skip the stack adjust */
0a8f9d31 87
ec25d19b
SC
88 if (IS_MOVK_R5 (w))
89 {
90 start_pc += 2;
df14b38b 91 w = read_memory_unsigned_integer (start_pc, 2);
ec25d19b
SC
92 }
93 if (IS_SUB_R5SP (w))
94 {
95 start_pc += 2;
df14b38b 96 w = read_memory_unsigned_integer (start_pc, 2);
ec25d19b
SC
97 }
98 while (IS_SUB2_SP (w))
99 {
100 start_pc += 2;
df14b38b 101 w = read_memory_unsigned_integer (start_pc, 2);
ec25d19b
SC
102 }
103
104 return start_pc;
ec25d19b 105}
1f46923f 106
400943fb 107int
18b46e7c
SS
108gdb_print_insn_h8300 (memaddr, info)
109 bfd_vma memaddr;
110 disassemble_info *info;
0a8f9d31 111{
a3059251 112 if (h8300hmode)
5076ecd0 113 return print_insn_h8300h (memaddr, info);
d0414a11 114 else
5076ecd0 115 return print_insn_h8300 (memaddr, info);
0a8f9d31 116}
ec25d19b 117
1f46923f
SC
118/* Given a GDB frame, determine the address of the calling function's frame.
119 This will be used to create a new GDB frame struct, and then
120 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
121
122 For us, the frame address is its stack pointer value, so we look up
123 the function prologue to determine the caller's sp value, and return it. */
124
669caa9c
SS
125CORE_ADDR
126h8300_frame_chain (thisframe)
127 struct frame_info *thisframe;
1f46923f 128{
1f46923f 129 frame_find_saved_regs (thisframe, (struct frame_saved_regs *) 0);
ec25d19b 130 return thisframe->fsr->regs[SP_REGNUM];
1f46923f
SC
131}
132
1f46923f
SC
133/* Put here the code to store, into a struct frame_saved_regs,
134 the addresses of the saved registers of frame described by FRAME_INFO.
135 This includes special registers such as pc and fp saved in special
136 ways in the stack frame. sp is even more special:
137 the address we return for it IS the sp for the next frame.
138
139 We cache the result of doing this in the frame_cache_obstack, since
140 it is fairly expensive. */
141
142void
143frame_find_saved_regs (fi, fsr)
144 struct frame_info *fi;
145 struct frame_saved_regs *fsr;
146{
1f46923f
SC
147 register struct frame_saved_regs *cache_fsr;
148 extern struct obstack frame_cache_obstack;
149 CORE_ADDR ip;
150 struct symtab_and_line sal;
151 CORE_ADDR limit;
152
153 if (!fi->fsr)
154 {
155 cache_fsr = (struct frame_saved_regs *)
ec25d19b
SC
156 obstack_alloc (&frame_cache_obstack,
157 sizeof (struct frame_saved_regs));
4ed97c9a 158 memset (cache_fsr, '\0', sizeof (struct frame_saved_regs));
ec25d19b 159
1f46923f
SC
160 fi->fsr = cache_fsr;
161
162 /* Find the start and end of the function prologue. If the PC
163 is in the function prologue, we only consider the part that
164 has executed already. */
ec25d19b 165
1f46923f
SC
166 ip = get_pc_function_start (fi->pc);
167 sal = find_pc_line (ip, 0);
ec25d19b 168 limit = (sal.end && sal.end < fi->pc) ? sal.end : fi->pc;
1f46923f
SC
169
170 /* This will fill in fields in *fi as well as in cache_fsr. */
171 examine_prologue (ip, limit, fi->frame, cache_fsr, fi);
172 }
173
174 if (fsr)
175 *fsr = *fi->fsr;
176}
1f46923f
SC
177
178/* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
179 is not the address of a valid instruction, the address of the next
180 instruction beyond ADDR otherwise. *PWORD1 receives the first word
181 of the instruction.*/
182
1f46923f 183CORE_ADDR
ec25d19b
SC
184NEXT_PROLOGUE_INSN (addr, lim, pword1)
185 CORE_ADDR addr;
186 CORE_ADDR lim;
58e49e21 187 INSN_WORD *pword1;
1f46923f 188{
34df79fc 189 char buf[2];
ec25d19b
SC
190 if (addr < lim + 8)
191 {
34df79fc
JK
192 read_memory (addr, buf, 2);
193 *pword1 = extract_signed_integer (buf, 2);
1f46923f 194
ec25d19b
SC
195 return addr + 2;
196 }
1f46923f 197 return 0;
1f46923f
SC
198}
199
200/* Examine the prologue of a function. `ip' points to the first instruction.
ec25d19b 201 `limit' is the limit of the prologue (e.g. the addr of the first
1f46923f 202 linenumber, or perhaps the program counter if we're stepping through).
ec25d19b 203 `frame_sp' is the stack pointer value in use in this frame.
1f46923f 204 `fsr' is a pointer to a frame_saved_regs structure into which we put
ec25d19b 205 info about the registers saved by this frame.
1f46923f
SC
206 `fi' is a struct frame_info pointer; we fill in various fields in it
207 to reflect the offsets of the arg pointer and the locals pointer. */
208
1f46923f
SC
209static CORE_ADDR
210examine_prologue (ip, limit, after_prolog_fp, fsr, fi)
211 register CORE_ADDR ip;
212 register CORE_ADDR limit;
669caa9c 213 CORE_ADDR after_prolog_fp;
1f46923f
SC
214 struct frame_saved_regs *fsr;
215 struct frame_info *fi;
216{
217 register CORE_ADDR next_ip;
218 int r;
1f46923f 219 int have_fp = 0;
1f46923f 220 INSN_WORD insn_word;
d0414a11
DE
221 /* Number of things pushed onto stack, starts at 2/4, 'cause the
222 PC is already there */
a3059251 223 unsigned int reg_save_depth = h8300hmode ? 4 : 2;
1f46923f
SC
224
225 unsigned int auto_depth = 0; /* Number of bytes of autos */
1f46923f 226
ddf30c37 227 char in_frame[11]; /* One for each reg */
1f46923f 228
ddf30c37 229 memset (in_frame, 1, 11);
256b4f37 230 for (r = 0; r < 8; r++)
ec25d19b
SC
231 {
232 fsr->regs[r] = 0;
233 }
234 if (after_prolog_fp == 0)
235 {
236 after_prolog_fp = read_register (SP_REGNUM);
237 }
a3059251 238 if (ip == 0 || ip & (h8300hmode ? ~0xffff : ~0xffff))
ec25d19b 239 return 0;
1f46923f 240
ec25d19b 241 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
1f46923f 242
ec25d19b
SC
243 /* Skip over any fp push instructions */
244 fsr->regs[6] = after_prolog_fp;
245 while (next_ip && IS_PUSH_FP (insn_word))
246 {
247 ip = next_ip;
1f46923f 248
ec25d19b
SC
249 in_frame[insn_word & 0x7] = reg_save_depth;
250 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
251 reg_save_depth += 2;
252 }
1f46923f
SC
253
254 /* Is this a move into the fp */
ec25d19b
SC
255 if (next_ip && IS_MOV_SP_FP (insn_word))
256 {
257 ip = next_ip;
258 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
259 have_fp = 1;
260 }
1f46923f
SC
261
262 /* Skip over any stack adjustment, happens either with a number of
263 sub#2,sp or a mov #x,r5 sub r5,sp */
264
ec25d19b 265 if (next_ip && IS_SUB2_SP (insn_word))
1f46923f 266 {
ec25d19b
SC
267 while (next_ip && IS_SUB2_SP (insn_word))
268 {
269 auto_depth += 2;
270 ip = next_ip;
271 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
272 }
1f46923f 273 }
ec25d19b
SC
274 else
275 {
276 if (next_ip && IS_MOVK_R5 (insn_word))
277 {
278 ip = next_ip;
279 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
280 auto_depth += insn_word;
281
282 next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn_word);
283 auto_depth += insn_word;
ec25d19b
SC
284 }
285 }
286 /* Work out which regs are stored where */
287 while (next_ip && IS_PUSH (insn_word))
1f46923f
SC
288 {
289 ip = next_ip;
ec25d19b
SC
290 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
291 fsr->regs[r] = after_prolog_fp + auto_depth;
292 auto_depth += 2;
1f46923f 293 }
1f46923f 294
1f46923f 295 /* The args are always reffed based from the stack pointer */
ec25d19b 296 fi->args_pointer = after_prolog_fp;
1f46923f 297 /* Locals are always reffed based from the fp */
ec25d19b 298 fi->locals_pointer = after_prolog_fp;
1f46923f 299 /* The PC is at a known place */
df14b38b 300 fi->from_pc = read_memory_unsigned_integer (after_prolog_fp + 2, BINWORD);
1f46923f
SC
301
302 /* Rememeber any others too */
1f46923f 303 in_frame[PC_REGNUM] = 0;
ec25d19b
SC
304
305 if (have_fp)
306 /* We keep the old FP in the SP spot */
b1d0b161 307 fsr->regs[SP_REGNUM] = read_memory_unsigned_integer (fsr->regs[6], BINWORD);
ec25d19b
SC
308 else
309 fsr->regs[SP_REGNUM] = after_prolog_fp + auto_depth;
310
1f46923f
SC
311 return (ip);
312}
313
314void
315init_extra_frame_info (fromleaf, fi)
316 int fromleaf;
317 struct frame_info *fi;
318{
319 fi->fsr = 0; /* Not yet allocated */
320 fi->args_pointer = 0; /* Unknown */
321 fi->locals_pointer = 0; /* Unknown */
322 fi->from_pc = 0;
1f46923f 323}
ec25d19b 324
1f46923f
SC
325/* Return the saved PC from this frame.
326
327 If the frame has a memory copy of SRP_REGNUM, use that. If not,
328 just use the register SRP_REGNUM itself. */
329
330CORE_ADDR
331frame_saved_pc (frame)
669caa9c 332 struct frame_info *frame;
1f46923f
SC
333{
334 return frame->from_pc;
335}
336
1f46923f
SC
337CORE_ADDR
338frame_locals_address (fi)
339 struct frame_info *fi;
340{
ec25d19b
SC
341 if (!fi->locals_pointer)
342 {
343 struct frame_saved_regs ignore;
344
345 get_frame_saved_regs (fi, &ignore);
1f46923f 346
ec25d19b 347 }
1f46923f
SC
348 return fi->locals_pointer;
349}
350
351/* Return the address of the argument block for the frame
352 described by FI. Returns 0 if the address is unknown. */
353
354CORE_ADDR
355frame_args_address (fi)
356 struct frame_info *fi;
357{
ec25d19b
SC
358 if (!fi->args_pointer)
359 {
360 struct frame_saved_regs ignore;
361
362 get_frame_saved_regs (fi, &ignore);
363
364 }
1f46923f 365
1f46923f
SC
366 return fi->args_pointer;
367}
368
ec25d19b
SC
369void
370h8300_pop_frame ()
1f46923f
SC
371{
372 unsigned regnum;
373 struct frame_saved_regs fsr;
669caa9c 374 struct frame_info *frame = get_current_frame ();
1f46923f 375
669caa9c 376 get_frame_saved_regs (frame, &fsr);
ec25d19b 377
256b4f37 378 for (regnum = 0; regnum < 8; regnum++)
1f46923f 379 {
ec25d19b 380 if (fsr.regs[regnum])
f9fedc48 381 write_register (regnum, read_memory_integer(fsr.regs[regnum], BINWORD));
ec25d19b
SC
382
383 flush_cached_frames ();
1f46923f 384 }
1f46923f 385}
ec25d19b 386
a3059251
SC
387
388struct cmd_list_element *setmemorylist;
389
390static void
391h8300_command(args, from_tty)
392{
393 extern int h8300hmode;
394 h8300hmode = 0;
395}
396
397static void
398h8300h_command(args, from_tty)
399{
400 extern int h8300hmode;
401 h8300hmode = 1;
402}
403
404static void
405set_machine (args, from_tty)
406 char *args;
407 int from_tty;
408{
199b2450
TL
409 printf_unfiltered ("\"set machine\" must be followed by h8300 or h8300h.\n");
410 help_list (setmemorylist, "set memory ", -1, gdb_stdout);
a3059251
SC
411}
412
f9fedc48
MA
413/* set_machine_hook is called as the exec file is being opened, but
414 before the symbol file is opened. This allows us to set the
415 h8300hmode flag based on the machine type specified in the exec
416 file. This in turn will cause subsequently defined pointer types
417 to be 16 or 32 bits as appropriate for the machine. */
418
419static void
420set_machine_hook (filename)
421 char *filename;
422{
423 h8300hmode = (bfd_get_mach (exec_bfd) == bfd_mach_h8300h);
424}
425
a3059251
SC
426void
427_initialize_h8300m ()
428{
429 add_prefix_cmd ("machine", no_class, set_machine,
430 "set the machine type", &setmemorylist, "set machine ", 0,
431 &setlist);
432
433 add_cmd ("h8300", class_support, h8300_command,
434 "Set machine to be H8/300.", &setmemorylist);
435
436 add_cmd ("h8300h", class_support, h8300h_command,
437 "Set machine to be H8/300H.", &setmemorylist);
f9fedc48
MA
438
439 /* Add a hook to set the machine type when we're loading a file. */
440
441 specify_exec_file_hook(set_machine_hook);
a3059251
SC
442}
443
444
445
ec25d19b
SC
446void
447print_register_hook (regno)
448{
449 if (regno == 8)
450 {
451 /* CCR register */
ec25d19b 452 int C, Z, N, V;
08c0d7b8 453 unsigned char b[4];
ec25d19b 454 unsigned char l;
ec25d19b 455 read_relative_register_raw_bytes (regno, b);
08c0d7b8 456 l = b[REGISTER_VIRTUAL_SIZE(8) -1];
199b2450
TL
457 printf_unfiltered ("\t");
458 printf_unfiltered ("I-%d - ", (l & 0x80) != 0);
459 printf_unfiltered ("H-%d - ", (l & 0x20) != 0);
ec25d19b
SC
460 N = (l & 0x8) != 0;
461 Z = (l & 0x4) != 0;
462 V = (l & 0x2) != 0;
463 C = (l & 0x1) != 0;
199b2450
TL
464 printf_unfiltered ("N-%d ", N);
465 printf_unfiltered ("Z-%d ", Z);
466 printf_unfiltered ("V-%d ", V);
467 printf_unfiltered ("C-%d ", C);
ec25d19b 468 if ((C | Z) == 0)
199b2450 469 printf_unfiltered ("u> ");
ec25d19b 470 if ((C | Z) == 1)
199b2450 471 printf_unfiltered ("u<= ");
ec25d19b 472 if ((C == 0))
199b2450 473 printf_unfiltered ("u>= ");
ec25d19b 474 if (C == 1)
199b2450 475 printf_unfiltered ("u< ");
ec25d19b 476 if (Z == 0)
199b2450 477 printf_unfiltered ("!= ");
ec25d19b 478 if (Z == 1)
199b2450 479 printf_unfiltered ("== ");
ec25d19b 480 if ((N ^ V) == 0)
199b2450 481 printf_unfiltered (">= ");
ec25d19b 482 if ((N ^ V) == 1)
199b2450 483 printf_unfiltered ("< ");
ec25d19b 484 if ((Z | (N ^ V)) == 0)
199b2450 485 printf_unfiltered ("> ");
ec25d19b 486 if ((Z | (N ^ V)) == 1)
199b2450 487 printf_unfiltered ("<= ");
ec25d19b
SC
488 }
489}
a3059251 490
18b46e7c
SS
491void
492_initialize_h8300_tdep ()
493{
494 tm_print_insn = gdb_print_insn_h8300;
495}
This page took 0.318608 seconds and 4 git commands to generate.