* gdb.t00/help.exp: Increase expect input buffer size.
[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
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
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"
df14b38b 29#include <dis-asm.h>
256b4f37
SC
30#undef NUM_REGS
31#define NUM_REGS 11
32
1f46923f 33#define UNSIGNED_SHORT(X) ((X) & 0xffff)
400943fb
SC
34
35/* an easy to debug H8 stack frame looks like:
ec25d19b
SC
360x6df6 push r6
370x0d76 mov.w r7,r6
380x6dfn push reg
390x7905 nnnn mov.w #n,r5 or 0x1b87 subs #2,sp
400x1957 sub.w r5,sp
400943fb
SC
41
42 */
1f46923f 43
400943fb 44#define IS_PUSH(x) ((x & 0xff00)==0x6d00)
ec25d19b 45#define IS_PUSH_FP(x) (x == 0x6df6)
1f46923f
SC
46#define IS_MOVE_FP(x) (x == 0x0d76)
47#define IS_MOV_SP_FP(x) (x == 0x0d76)
48#define IS_SUB2_SP(x) (x==0x1b87)
49#define IS_MOVK_R5(x) (x==0x7905)
ec25d19b
SC
50#define IS_SUB_R5SP(x) (x==0x1957)
51CORE_ADDR examine_prologue ();
1f46923f 52
ec25d19b
SC
53void frame_find_saved_regs ();
54CORE_ADDR
55h8300_skip_prologue (start_pc)
56 CORE_ADDR start_pc;
0a8f9d31 57{
ec25d19b 58 short int w;
1f46923f 59
df14b38b 60 w = read_memory_unsigned_integer (start_pc, 2);
400943fb 61 /* Skip past all push insns */
ec25d19b
SC
62 while (IS_PUSH_FP (w))
63 {
64 start_pc += 2;
df14b38b 65 w = read_memory_unsigned_integer (start_pc, 2);
ec25d19b 66 }
0a8f9d31 67
1f46923f 68 /* Skip past a move to FP */
ec25d19b
SC
69 if (IS_MOVE_FP (w))
70 {
71 start_pc += 2;
df14b38b 72 w = read_memory_unsigned_integer (start_pc, 2);
1f46923f
SC
73 }
74
ec25d19b 75 /* Skip the stack adjust */
0a8f9d31 76
ec25d19b
SC
77 if (IS_MOVK_R5 (w))
78 {
79 start_pc += 2;
df14b38b 80 w = read_memory_unsigned_integer (start_pc, 2);
ec25d19b
SC
81 }
82 if (IS_SUB_R5SP (w))
83 {
84 start_pc += 2;
df14b38b 85 w = read_memory_unsigned_integer (start_pc, 2);
ec25d19b
SC
86 }
87 while (IS_SUB2_SP (w))
88 {
89 start_pc += 2;
df14b38b 90 w = read_memory_unsigned_integer (start_pc, 2);
ec25d19b
SC
91 }
92
93 return start_pc;
ec25d19b 94}
1f46923f 95
400943fb 96int
ec25d19b
SC
97print_insn (memaddr, stream)
98 CORE_ADDR memaddr;
99 FILE *stream;
0a8f9d31 100{
df14b38b
SC
101 disassemble_info info;
102 GDB_INIT_DISASSEMBLE_INFO(info, stream);
d0414a11
DE
103 if (HMODE)
104 return print_insn_h8300h (memaddr, &info);
105 else
106 return print_insn_h8300 (memaddr, &info);
0a8f9d31 107}
ec25d19b 108
1f46923f
SC
109/* Given a GDB frame, determine the address of the calling function's frame.
110 This will be used to create a new GDB frame struct, and then
111 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
112
113 For us, the frame address is its stack pointer value, so we look up
114 the function prologue to determine the caller's sp value, and return it. */
115
116FRAME_ADDR
117FRAME_CHAIN (thisframe)
118 FRAME thisframe;
119{
1f46923f 120 frame_find_saved_regs (thisframe, (struct frame_saved_regs *) 0);
ec25d19b 121 return thisframe->fsr->regs[SP_REGNUM];
1f46923f
SC
122}
123
1f46923f
SC
124/* Put here the code to store, into a struct frame_saved_regs,
125 the addresses of the saved registers of frame described by FRAME_INFO.
126 This includes special registers such as pc and fp saved in special
127 ways in the stack frame. sp is even more special:
128 the address we return for it IS the sp for the next frame.
129
130 We cache the result of doing this in the frame_cache_obstack, since
131 it is fairly expensive. */
132
133void
134frame_find_saved_regs (fi, fsr)
135 struct frame_info *fi;
136 struct frame_saved_regs *fsr;
137{
138 register CORE_ADDR next_addr;
139 register CORE_ADDR *saved_regs;
140 register int regnum;
141 register struct frame_saved_regs *cache_fsr;
142 extern struct obstack frame_cache_obstack;
143 CORE_ADDR ip;
144 struct symtab_and_line sal;
145 CORE_ADDR limit;
146
147 if (!fi->fsr)
148 {
149 cache_fsr = (struct frame_saved_regs *)
ec25d19b
SC
150 obstack_alloc (&frame_cache_obstack,
151 sizeof (struct frame_saved_regs));
1f46923f 152 bzero (cache_fsr, sizeof (struct frame_saved_regs));
ec25d19b 153
1f46923f
SC
154 fi->fsr = cache_fsr;
155
156 /* Find the start and end of the function prologue. If the PC
157 is in the function prologue, we only consider the part that
158 has executed already. */
ec25d19b 159
1f46923f
SC
160 ip = get_pc_function_start (fi->pc);
161 sal = find_pc_line (ip, 0);
ec25d19b 162 limit = (sal.end && sal.end < fi->pc) ? sal.end : fi->pc;
1f46923f
SC
163
164 /* This will fill in fields in *fi as well as in cache_fsr. */
165 examine_prologue (ip, limit, fi->frame, cache_fsr, fi);
166 }
167
168 if (fsr)
169 *fsr = *fi->fsr;
170}
1f46923f
SC
171
172/* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
173 is not the address of a valid instruction, the address of the next
174 instruction beyond ADDR otherwise. *PWORD1 receives the first word
175 of the instruction.*/
176
1f46923f 177CORE_ADDR
ec25d19b
SC
178NEXT_PROLOGUE_INSN (addr, lim, pword1)
179 CORE_ADDR addr;
180 CORE_ADDR lim;
181 short *pword1;
1f46923f 182{
ec25d19b
SC
183 if (addr < lim + 8)
184 {
185 read_memory (addr, pword1, sizeof (*pword1));
186 SWAP_TARGET_AND_HOST (pword1, sizeof (short));
1f46923f 187
ec25d19b
SC
188 return addr + 2;
189 }
1f46923f 190 return 0;
1f46923f
SC
191}
192
193/* Examine the prologue of a function. `ip' points to the first instruction.
ec25d19b 194 `limit' is the limit of the prologue (e.g. the addr of the first
1f46923f 195 linenumber, or perhaps the program counter if we're stepping through).
ec25d19b 196 `frame_sp' is the stack pointer value in use in this frame.
1f46923f 197 `fsr' is a pointer to a frame_saved_regs structure into which we put
ec25d19b 198 info about the registers saved by this frame.
1f46923f
SC
199 `fi' is a struct frame_info pointer; we fill in various fields in it
200 to reflect the offsets of the arg pointer and the locals pointer. */
201
1f46923f
SC
202static CORE_ADDR
203examine_prologue (ip, limit, after_prolog_fp, fsr, fi)
204 register CORE_ADDR ip;
205 register CORE_ADDR limit;
206 FRAME_ADDR after_prolog_fp;
207 struct frame_saved_regs *fsr;
208 struct frame_info *fi;
209{
210 register CORE_ADDR next_ip;
211 int r;
212 int i;
213 int have_fp = 0;
1f46923f
SC
214 register int src;
215 register struct pic_prologue_code *pcode;
216 INSN_WORD insn_word;
217 int size, offset;
d0414a11
DE
218 /* Number of things pushed onto stack, starts at 2/4, 'cause the
219 PC is already there */
220 unsigned int reg_save_depth = HMODE ? 4 : 2;
1f46923f
SC
221
222 unsigned int auto_depth = 0; /* Number of bytes of autos */
1f46923f 223
ddf30c37 224 char in_frame[11]; /* One for each reg */
1f46923f 225
ddf30c37 226 memset (in_frame, 1, 11);
256b4f37 227 for (r = 0; r < 8; r++)
ec25d19b
SC
228 {
229 fsr->regs[r] = 0;
230 }
231 if (after_prolog_fp == 0)
232 {
233 after_prolog_fp = read_register (SP_REGNUM);
234 }
d0414a11 235 if (ip == 0 || ip & (HMODE ? ~0xffff : ~0xffff))
ec25d19b 236 return 0;
1f46923f 237
ec25d19b 238 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
1f46923f 239
ec25d19b
SC
240 /* Skip over any fp push instructions */
241 fsr->regs[6] = after_prolog_fp;
242 while (next_ip && IS_PUSH_FP (insn_word))
243 {
244 ip = next_ip;
1f46923f 245
ec25d19b
SC
246 in_frame[insn_word & 0x7] = reg_save_depth;
247 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
248 reg_save_depth += 2;
249 }
1f46923f
SC
250
251 /* Is this a move into the fp */
ec25d19b
SC
252 if (next_ip && IS_MOV_SP_FP (insn_word))
253 {
254 ip = next_ip;
255 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
256 have_fp = 1;
257 }
1f46923f
SC
258
259 /* Skip over any stack adjustment, happens either with a number of
260 sub#2,sp or a mov #x,r5 sub r5,sp */
261
ec25d19b 262 if (next_ip && IS_SUB2_SP (insn_word))
1f46923f 263 {
ec25d19b
SC
264 while (next_ip && IS_SUB2_SP (insn_word))
265 {
266 auto_depth += 2;
267 ip = next_ip;
268 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
269 }
1f46923f 270 }
ec25d19b
SC
271 else
272 {
273 if (next_ip && IS_MOVK_R5 (insn_word))
274 {
275 ip = next_ip;
276 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
277 auto_depth += insn_word;
278
279 next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn_word);
280 auto_depth += insn_word;
ec25d19b
SC
281 }
282 }
283 /* Work out which regs are stored where */
284 while (next_ip && IS_PUSH (insn_word))
1f46923f
SC
285 {
286 ip = next_ip;
ec25d19b
SC
287 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word);
288 fsr->regs[r] = after_prolog_fp + auto_depth;
289 auto_depth += 2;
1f46923f 290 }
1f46923f 291
1f46923f 292 /* The args are always reffed based from the stack pointer */
ec25d19b 293 fi->args_pointer = after_prolog_fp;
1f46923f 294 /* Locals are always reffed based from the fp */
ec25d19b 295 fi->locals_pointer = after_prolog_fp;
1f46923f 296 /* The PC is at a known place */
df14b38b 297 fi->from_pc = read_memory_unsigned_integer (after_prolog_fp + 2, BINWORD);
1f46923f
SC
298
299 /* Rememeber any others too */
1f46923f 300 in_frame[PC_REGNUM] = 0;
ec25d19b
SC
301
302 if (have_fp)
303 /* We keep the old FP in the SP spot */
b1d0b161 304 fsr->regs[SP_REGNUM] = read_memory_unsigned_integer (fsr->regs[6], BINWORD);
ec25d19b
SC
305 else
306 fsr->regs[SP_REGNUM] = after_prolog_fp + auto_depth;
307
1f46923f
SC
308 return (ip);
309}
310
311void
312init_extra_frame_info (fromleaf, fi)
313 int fromleaf;
314 struct frame_info *fi;
315{
316 fi->fsr = 0; /* Not yet allocated */
317 fi->args_pointer = 0; /* Unknown */
318 fi->locals_pointer = 0; /* Unknown */
319 fi->from_pc = 0;
1f46923f 320}
ec25d19b 321
1f46923f
SC
322/* Return the saved PC from this frame.
323
324 If the frame has a memory copy of SRP_REGNUM, use that. If not,
325 just use the register SRP_REGNUM itself. */
326
327CORE_ADDR
328frame_saved_pc (frame)
ec25d19b 329 FRAME frame;
1f46923f
SC
330{
331 return frame->from_pc;
332}
333
1f46923f
SC
334CORE_ADDR
335frame_locals_address (fi)
336 struct frame_info *fi;
337{
ec25d19b
SC
338 if (!fi->locals_pointer)
339 {
340 struct frame_saved_regs ignore;
341
342 get_frame_saved_regs (fi, &ignore);
1f46923f 343
ec25d19b 344 }
1f46923f
SC
345 return fi->locals_pointer;
346}
347
348/* Return the address of the argument block for the frame
349 described by FI. Returns 0 if the address is unknown. */
350
351CORE_ADDR
352frame_args_address (fi)
353 struct frame_info *fi;
354{
ec25d19b
SC
355 if (!fi->args_pointer)
356 {
357 struct frame_saved_regs ignore;
358
359 get_frame_saved_regs (fi, &ignore);
360
361 }
1f46923f 362
1f46923f
SC
363 return fi->args_pointer;
364}
365
ec25d19b
SC
366void
367h8300_pop_frame ()
1f46923f
SC
368{
369 unsigned regnum;
370 struct frame_saved_regs fsr;
371 struct frame_info *fi;
372
ec25d19b 373 FRAME frame = get_current_frame ();
1f46923f 374
ec25d19b
SC
375 fi = get_frame_info (frame);
376 get_frame_saved_regs (fi, &fsr);
377
256b4f37 378 for (regnum = 0; regnum < 8; regnum++)
1f46923f 379 {
ec25d19b
SC
380 if (fsr.regs[regnum])
381 {
df14b38b 382 write_register (regnum, read_memory_integer(fsr.regs[regnum]), BINWORD);
ec25d19b
SC
383 }
384
385 flush_cached_frames ();
386 set_current_frame (create_new_frame (read_register (FP_REGNUM),
387 read_pc ()));
1f46923f 388 }
1f46923f 389}
ec25d19b
SC
390
391void
392print_register_hook (regno)
393{
394 if (regno == 8)
395 {
396 /* CCR register */
397
398 int C, Z, N, V;
399 unsigned char b[2];
400 unsigned char l;
401
402 read_relative_register_raw_bytes (regno, b);
403 l = b[1];
404 printf ("\t");
405 printf ("I-%d - ", (l & 0x80) != 0);
406 printf ("H-%d - ", (l & 0x20) != 0);
407 N = (l & 0x8) != 0;
408 Z = (l & 0x4) != 0;
409 V = (l & 0x2) != 0;
410 C = (l & 0x1) != 0;
411 printf ("N-%d ", N);
412 printf ("Z-%d ", Z);
413 printf ("V-%d ", V);
414 printf ("C-%d ", C);
415 if ((C | Z) == 0)
416 printf ("u> ");
417 if ((C | Z) == 1)
418 printf ("u<= ");
419 if ((C == 0))
420 printf ("u>= ");
421 if (C == 1)
422 printf ("u< ");
423 if (Z == 0)
424 printf ("!= ");
425 if (Z == 1)
426 printf ("== ");
427 if ((N ^ V) == 0)
428 printf (">= ");
429 if ((N ^ V) == 1)
430 printf ("< ");
431 if ((Z | (N ^ V)) == 0)
432 printf ("> ");
433 if ((Z | (N ^ V)) == 1)
434 printf ("<= ");
435 }
436}
This page took 0.095878 seconds and 4 git commands to generate.