df7e013509887b02b43698b6bcd54bb420a91049
[deliverable/binutils-gdb.git] / gdb / h8300-tdep.c
1 /* Target-machine dependent code for Hitachi H8/300, for GDB.
2
3 Copyright 1988, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998,
4 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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.
12
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.
17
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. */
22
23 /*
24 Contributed by Steve Chamberlain
25 sac@cygnus.com
26 */
27
28 #include "defs.h"
29 #include "value.h"
30 #include "inferior.h"
31 #include "symfile.h"
32 #include "arch-utils.h"
33 #include "regcache.h"
34 #include "gdbcore.h"
35 #include "objfiles.h"
36 #include "gdbcmd.h"
37 #include "gdb_assert.h"
38
39 /* Extra info which is saved in each frame_info. */
40 struct frame_extra_info
41 {
42 CORE_ADDR from_pc;
43 CORE_ADDR args_pointer;
44 CORE_ADDR locals_pointer;
45 };
46
47 enum
48 {
49 h8300_reg_size = 2,
50 h8300h_reg_size = 4,
51 h8300_max_reg_size = 4,
52 };
53 #define BINWORD (h8300hmode ? h8300h_reg_size : h8300_reg_size)
54
55 enum gdb_regnum
56 {
57 E_R0_REGNUM, E_ER0_REGNUM = E_R0_REGNUM, E_ARG0_REGNUM = E_R0_REGNUM,
58 E_R1_REGNUM, E_ER1_REGNUM = E_R1_REGNUM,
59 E_R2_REGNUM, E_ER2_REGNUM = E_R2_REGNUM, E_ARGLAST_REGNUM = E_R2_REGNUM,
60 E_R3_REGNUM, E_ER3_REGNUM = E_R3_REGNUM,
61 E_R4_REGNUM, E_ER4_REGNUM = E_R4_REGNUM,
62 E_R5_REGNUM, E_ER5_REGNUM = E_R5_REGNUM,
63 E_R6_REGNUM, E_ER6_REGNUM = E_R6_REGNUM, E_FP_REGNUM = E_R6_REGNUM,
64 E_SP_REGNUM,
65 E_CCR_REGNUM,
66 E_PC_REGNUM,
67 E_CYCLES_REGNUM,
68 E_TICK_REGNUM, E_EXR_REGNUM = E_TICK_REGNUM,
69 E_INST_REGNUM, E_TICKS_REGNUM = E_INST_REGNUM,
70 E_INSTS_REGNUM,
71 E_MACH_REGNUM,
72 E_MACL_REGNUM,
73 E_SBR_REGNUM,
74 E_VBR_REGNUM
75 };
76
77 #define UNSIGNED_SHORT(X) ((X) & 0xffff)
78
79 #define IS_PUSH(x) ((x & 0xfff0)==0x6df0)
80 #define IS_PUSH_FP(x) (x == 0x6df6)
81 #define IS_MOVE_FP(x) (x == 0x0d76 || x == 0x0ff6)
82 #define IS_MOV_SP_FP(x) (x == 0x0d76 || x == 0x0ff6)
83 #define IS_SUB2_SP(x) (x==0x1b87)
84 #define IS_SUB4_SP(x) (x==0x1b97)
85 #define IS_SUBL_SP(x) (x==0x7a37)
86 #define IS_MOVK_R5(x) (x==0x7905)
87 #define IS_SUB_R5SP(x) (x==0x1957)
88
89 /* If the instruction at PC is an argument register spill, return its
90 length. Otherwise, return zero.
91
92 An argument register spill is an instruction that moves an argument
93 from the register in which it was passed to the stack slot in which
94 it really lives. It is a byte, word, or longword move from an
95 argument register to a negative offset from the frame pointer. */
96
97 static int
98 h8300_is_argument_spill (CORE_ADDR pc)
99 {
100 int w = read_memory_unsigned_integer (pc, 2);
101
102 if ((w & 0xfff0) == 0x6ee0 /* mov.b Rs,@(d:16,er6) */
103 && 8 <= (w & 0xf) && (w & 0xf) <= 10) /* Rs is R0L, R1L, or R2L */
104 {
105 int w2 = read_memory_integer (pc + 2, 2);
106
107 /* ... and d:16 is negative. */
108 if (w2 < 0)
109 return 4;
110 }
111 else if (w == 0x7860)
112 {
113 int w2 = read_memory_integer (pc + 2, 2);
114
115 if ((w2 & 0xfff0) == 0x6aa0) /* mov.b Rs, @(d:24,er6) */
116 {
117 LONGEST disp = read_memory_integer (pc + 4, 4);
118
119 /* ... and d:24 is negative. */
120 if (disp < 0 && disp > 0xffffff)
121 return 8;
122 }
123 }
124 else if ((w & 0xfff0) == 0x6fe0 /* mov.w Rs,@(d:16,er6) */
125 && (w & 0xf) <= 2) /* Rs is R0, R1, or R2 */
126 {
127 int w2 = read_memory_integer (pc + 2, 2);
128
129 /* ... and d:16 is negative. */
130 if (w2 < 0)
131 return 4;
132 }
133 else if (w == 0x78e0)
134 {
135 int w2 = read_memory_integer (pc + 2, 2);
136
137 if ((w2 & 0xfff0) == 0x6ba0) /* mov.b Rs, @(d:24,er6) */
138 {
139 LONGEST disp = read_memory_integer (pc + 4, 4);
140
141 /* ... and d:24 is negative. */
142 if (disp < 0 && disp > 0xffffff)
143 return 8;
144 }
145 }
146 else if (w == 0x0100)
147 {
148 int w2 = read_memory_integer (pc + 2, 2);
149
150 if ((w2 & 0xfff0) == 0x6fe0 /* mov.l Rs,@(d:16,er6) */
151 && (w2 & 0xf) <= 2) /* Rs is ER0, ER1, or ER2 */
152 {
153 int w3 = read_memory_integer (pc + 4, 2);
154
155 /* ... and d:16 is negative. */
156 if (w3 < 0)
157 return 6;
158 }
159 else if (w2 == 0x78e0)
160 {
161 int w3 = read_memory_integer (pc + 4, 2);
162
163 if ((w3 & 0xfff0) == 0x6ba0) /* mov.l Rs, @(d:24,er6) */
164 {
165 LONGEST disp = read_memory_integer (pc + 6, 4);
166
167 /* ... and d:24 is negative. */
168 if (disp < 0 && disp > 0xffffff)
169 return 10;
170 }
171 }
172 }
173
174 return 0;
175 }
176
177 static CORE_ADDR
178 h8300_skip_prologue (CORE_ADDR start_pc)
179 {
180 short int w;
181 int adjust = 0;
182
183 /* Skip past all push and stm insns. */
184 while (1)
185 {
186 w = read_memory_unsigned_integer (start_pc, 2);
187 /* First look for push insns. */
188 if (w == 0x0100 || w == 0x0110 || w == 0x0120 || w == 0x0130)
189 {
190 w = read_memory_unsigned_integer (start_pc + 2, 2);
191 adjust = 2;
192 }
193
194 if (IS_PUSH (w))
195 {
196 start_pc += 2 + adjust;
197 w = read_memory_unsigned_integer (start_pc, 2);
198 continue;
199 }
200 adjust = 0;
201 break;
202 }
203
204 /* Skip past a move to FP, either word or long sized */
205 w = read_memory_unsigned_integer (start_pc, 2);
206 if (w == 0x0100)
207 {
208 w = read_memory_unsigned_integer (start_pc + 2, 2);
209 adjust += 2;
210 }
211
212 if (IS_MOVE_FP (w))
213 {
214 start_pc += 2 + adjust;
215 w = read_memory_unsigned_integer (start_pc, 2);
216 }
217
218 /* Check for loading either a word constant into r5;
219 long versions are handled by the SUBL_SP below. */
220 if (IS_MOVK_R5 (w))
221 {
222 start_pc += 2;
223 w = read_memory_unsigned_integer (start_pc, 2);
224 }
225
226 /* Now check for subtracting r5 from sp, word sized only. */
227 if (IS_SUB_R5SP (w))
228 {
229 start_pc += 2 + adjust;
230 w = read_memory_unsigned_integer (start_pc, 2);
231 }
232
233 /* Check for subs #2 and subs #4. */
234 while (IS_SUB2_SP (w) || IS_SUB4_SP (w))
235 {
236 start_pc += 2 + adjust;
237 w = read_memory_unsigned_integer (start_pc, 2);
238 }
239
240 /* Check for a 32bit subtract. */
241 if (IS_SUBL_SP (w))
242 start_pc += 6 + adjust;
243
244 /* Check for spilling an argument register to the stack frame.
245 This could also be an initializing store from non-prologue code,
246 but I don't think there's any harm in skipping that. */
247 for (;;)
248 {
249 int spill_size = h8300_is_argument_spill (start_pc);
250 if (spill_size == 0)
251 break;
252 start_pc += spill_size;
253 }
254
255 return start_pc;
256 }
257
258 static int
259 gdb_print_insn_h8300 (bfd_vma memaddr, disassemble_info * info)
260 {
261 if (h8300smode)
262 return print_insn_h8300s (memaddr, info);
263 else if (h8300hmode)
264 return print_insn_h8300h (memaddr, info);
265 else
266 return print_insn_h8300 (memaddr, info);
267 }
268
269 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
270 is not the address of a valid instruction, the address of the next
271 instruction beyond ADDR otherwise. *PWORD1 receives the first word
272 of the instruction. */
273
274 static CORE_ADDR
275 h8300_next_prologue_insn (CORE_ADDR addr,
276 CORE_ADDR lim,
277 unsigned short* pword1)
278 {
279 char buf[2];
280 if (addr < lim + 8)
281 {
282 read_memory (addr, buf, 2);
283 *pword1 = extract_signed_integer (buf, 2);
284
285 return addr + 2;
286 }
287 return 0;
288 }
289
290 /* Examine the prologue of a function. `ip' points to the first instruction.
291 `limit' is the limit of the prologue (e.g. the addr of the first
292 linenumber, or perhaps the program counter if we're stepping through).
293 `frame_sp' is the stack pointer value in use in this frame.
294 `fsr' is a pointer to a frame_saved_regs structure into which we put
295 info about the registers saved by this frame.
296 `fi' is a struct frame_info pointer; we fill in various fields in it
297 to reflect the offsets of the arg pointer and the locals pointer. */
298
299 /* Any function with a frame looks like this
300 SECOND ARG
301 FIRST ARG
302 RET PC
303 SAVED R2
304 SAVED R3
305 SAVED FP <-FP POINTS HERE
306 LOCALS0
307 LOCALS1 <-SP POINTS HERE
308 */
309
310 static CORE_ADDR
311 h8300_examine_prologue (register CORE_ADDR ip, register CORE_ADDR limit,
312 CORE_ADDR after_prolog_fp, CORE_ADDR *fsr,
313 struct frame_info *fi)
314 {
315 register CORE_ADDR next_ip;
316 int r;
317 int have_fp = 0;
318 unsigned short insn_word;
319 /* Number of things pushed onto stack, starts at 2/4, 'cause the
320 PC is already there */
321 unsigned int reg_save_depth = BINWORD;
322
323 unsigned int auto_depth = 0; /* Number of bytes of autos */
324
325 char in_frame[11]; /* One for each reg */
326
327 int adjust = 0;
328
329 memset (in_frame, 1, 11);
330 for (r = 0; r < 8; r++)
331 {
332 fsr[r] = 0;
333 }
334 if (after_prolog_fp == 0)
335 {
336 after_prolog_fp = read_register (E_SP_REGNUM);
337 }
338
339 /* If the PC isn't valid, quit now. */
340 if (ip == 0 || ip & (h8300hmode ? ~0xffffff : ~0xffff))
341 return 0;
342
343 next_ip = h8300_next_prologue_insn (ip, limit, &insn_word);
344
345 if (insn_word == 0x0100)
346 {
347 insn_word = read_memory_unsigned_integer (ip + 2, 2);
348 adjust = 2;
349 }
350
351 /* Skip over any fp push instructions */
352 fsr[E_FP_REGNUM] = after_prolog_fp;
353 while (next_ip && IS_PUSH_FP (insn_word))
354 {
355 ip = next_ip + adjust;
356
357 in_frame[insn_word & 0x7] = reg_save_depth;
358 next_ip = h8300_next_prologue_insn (ip, limit, &insn_word);
359 reg_save_depth += 2 + adjust;
360 }
361
362 /* Is this a move into the fp */
363 if (next_ip && IS_MOV_SP_FP (insn_word))
364 {
365 ip = next_ip;
366 next_ip = h8300_next_prologue_insn (ip, limit, &insn_word);
367 have_fp = 1;
368 }
369
370 /* Skip over any stack adjustment, happens either with a number of
371 sub#2,sp or a mov #x,r5 sub r5,sp */
372
373 if (next_ip && (IS_SUB2_SP (insn_word) || IS_SUB4_SP (insn_word)))
374 {
375 while (next_ip && (IS_SUB2_SP (insn_word) || IS_SUB4_SP (insn_word)))
376 {
377 auto_depth += IS_SUB2_SP (insn_word) ? 2 : 4;
378 ip = next_ip;
379 next_ip = h8300_next_prologue_insn (ip, limit, &insn_word);
380 }
381 }
382 else
383 {
384 if (next_ip && IS_MOVK_R5 (insn_word))
385 {
386 ip = next_ip;
387 next_ip = h8300_next_prologue_insn (ip, limit, &insn_word);
388 auto_depth += insn_word;
389
390 next_ip = h8300_next_prologue_insn (next_ip, limit, &insn_word);
391 auto_depth += insn_word;
392 }
393 if (next_ip && IS_SUBL_SP (insn_word))
394 {
395 ip = next_ip;
396 auto_depth += read_memory_unsigned_integer (ip, 4);
397 ip += 4;
398
399 next_ip = h8300_next_prologue_insn (ip, limit, &insn_word);
400 }
401 }
402
403 /* Now examine the push insns to determine where everything lives
404 on the stack. */
405 while (1)
406 {
407 adjust = 0;
408 if (!next_ip)
409 break;
410
411 if (insn_word == 0x0100)
412 {
413 ip = next_ip;
414 next_ip = h8300_next_prologue_insn (ip, limit, &insn_word);
415 adjust = 2;
416 }
417
418 if (IS_PUSH (insn_word))
419 {
420 auto_depth += 2 + adjust;
421 fsr[insn_word & 0x7] = after_prolog_fp - auto_depth;
422 ip = next_ip;
423 next_ip = h8300_next_prologue_insn (ip, limit, &insn_word);
424 continue;
425 }
426
427 /* Now check for push multiple insns. */
428 if (insn_word == 0x0110 || insn_word == 0x0120 || insn_word == 0x0130)
429 {
430 int count = ((insn_word >> 4) & 0xf) + 1;
431 int start, i;
432
433 ip = next_ip;
434 next_ip = h8300_next_prologue_insn (ip, limit, &insn_word);
435 start = insn_word & 0x7;
436
437 for (i = start; i < start + count; i++)
438 {
439 auto_depth += 4;
440 fsr[i] = after_prolog_fp - auto_depth;
441 }
442 }
443 break;
444 }
445
446 /* The args are always reffed based from the stack pointer */
447 get_frame_extra_info (fi)->args_pointer = after_prolog_fp;
448 /* Locals are always reffed based from the fp */
449 get_frame_extra_info (fi)->locals_pointer = after_prolog_fp;
450 /* The PC is at a known place */
451 get_frame_extra_info (fi)->from_pc =
452 read_memory_unsigned_integer (after_prolog_fp + BINWORD, BINWORD);
453
454 /* Rememeber any others too */
455 in_frame[E_PC_REGNUM] = 0;
456
457 if (have_fp)
458 /* We keep the old FP in the SP spot */
459 fsr[E_SP_REGNUM] = read_memory_unsigned_integer (fsr[E_FP_REGNUM],
460 BINWORD);
461 else
462 fsr[E_SP_REGNUM] = after_prolog_fp + auto_depth;
463
464 return (ip);
465 }
466
467 static void
468 h8300_frame_init_saved_regs (struct frame_info *fi)
469 {
470 CORE_ADDR func_addr, func_end;
471
472 if (!get_frame_saved_regs (fi))
473 {
474 frame_saved_regs_zalloc (fi);
475
476 /* Find the beginning of this function, so we can analyze its
477 prologue. */
478 if (find_pc_partial_function (get_frame_pc (fi), NULL,
479 &func_addr, &func_end))
480 {
481 struct symtab_and_line sal = find_pc_line (func_addr, 0);
482 CORE_ADDR limit = (sal.end && sal.end < get_frame_pc (fi))
483 ? sal.end : get_frame_pc (fi);
484 /* This will fill in fields in fi. */
485 h8300_examine_prologue (func_addr, limit, get_frame_base (fi),
486 get_frame_saved_regs (fi), fi);
487 }
488 /* Else we're out of luck (can't debug completely stripped code).
489 FIXME. */
490 }
491 }
492
493 /* Given a GDB frame, determine the address of the calling function's
494 frame. This will be used to create a new GDB frame struct, and
495 then DEPRECATED_INIT_EXTRA_FRAME_INFO and DEPRECATED_INIT_FRAME_PC
496 will be called for the new frame.
497
498 For us, the frame address is its stack pointer value, so we look up
499 the function prologue to determine the caller's sp value, and
500 return it. */
501
502 static CORE_ADDR
503 h8300_frame_chain (struct frame_info *thisframe)
504 {
505 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (thisframe),
506 get_frame_base (thisframe),
507 get_frame_base (thisframe)))
508 { /* initialize the from_pc now */
509 get_frame_extra_info (thisframe)->from_pc =
510 deprecated_read_register_dummy (get_frame_pc (thisframe),
511 get_frame_base (thisframe),
512 E_PC_REGNUM);
513 return get_frame_base (thisframe);
514 }
515 return get_frame_saved_regs (thisframe)[E_SP_REGNUM];
516 }
517
518 /* Return the saved PC from this frame.
519
520 If the frame has a memory copy of SRP_REGNUM, use that. If not,
521 just use the register SRP_REGNUM itself. */
522
523 static CORE_ADDR
524 h8300_frame_saved_pc (struct frame_info *frame)
525 {
526 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame),
527 get_frame_base (frame),
528 get_frame_base (frame)))
529 return deprecated_read_register_dummy (get_frame_pc (frame),
530 get_frame_base (frame),
531 E_PC_REGNUM);
532 else
533 return get_frame_extra_info (frame)->from_pc;
534 }
535
536 static void
537 h8300_init_extra_frame_info (int fromleaf, struct frame_info *fi)
538 {
539 if (!get_frame_extra_info (fi))
540 {
541 frame_extra_info_zalloc (fi, sizeof (struct frame_extra_info));
542 get_frame_extra_info (fi)->from_pc = 0;
543 get_frame_extra_info (fi)->args_pointer = 0; /* Unknown */
544 get_frame_extra_info (fi)->locals_pointer = 0; /* Unknown */
545
546 if (!get_frame_pc (fi))
547 {
548 if (get_next_frame (fi))
549 deprecated_update_frame_pc_hack (fi, h8300_frame_saved_pc (get_next_frame (fi)));
550 }
551 h8300_frame_init_saved_regs (fi);
552 }
553 }
554
555 static CORE_ADDR
556 h8300_frame_locals_address (struct frame_info *fi)
557 {
558 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi), get_frame_base (fi),
559 get_frame_base (fi)))
560 return (CORE_ADDR) 0; /* Not sure what else to do... */
561 return get_frame_extra_info (fi)->locals_pointer;
562 }
563
564 /* Return the address of the argument block for the frame
565 described by FI. Returns 0 if the address is unknown. */
566
567 static CORE_ADDR
568 h8300_frame_args_address (struct frame_info *fi)
569 {
570 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (fi), get_frame_base (fi),
571 get_frame_base (fi)))
572 return (CORE_ADDR) 0; /* Not sure what else to do... */
573 return get_frame_extra_info (fi)->args_pointer;
574 }
575
576 /* Round N up or down to the nearest multiple of UNIT.
577 Evaluate N only once, UNIT several times.
578 UNIT must be a power of two. */
579 #define round_up(n, unit) (((n) + (unit) - 1) & -(unit))
580 #define round_down(n, unit) ((n) & -(unit))
581
582 /* Function: push_arguments
583 Setup the function arguments for calling a function in the inferior.
584 In this discussion, a `word' is 16 bits on the H8/300s, and 32 bits
585 on the H8/300H.
586
587 There are actually two ABI's here: -mquickcall (the default) and
588 -mno-quickcall. With -mno-quickcall, all arguments are passed on
589 the stack after the return address, word-aligned. With
590 -mquickcall, GCC tries to use r0 -- r2 to pass registers. Since
591 GCC doesn't indicate in the object file which ABI was used to
592 compile it, GDB only supports the default --- -mquickcall.
593
594 Here are the rules for -mquickcall, in detail:
595
596 Each argument, whether scalar or aggregate, is padded to occupy a
597 whole number of words. Arguments smaller than a word are padded at
598 the most significant end; those larger than a word are padded at
599 the least significant end.
600
601 The initial arguments are passed in r0 -- r2. Earlier arguments go in
602 lower-numbered registers. Multi-word arguments are passed in
603 consecutive registers, with the most significant end in the
604 lower-numbered register.
605
606 If an argument doesn't fit entirely in the remaining registers, it
607 is passed entirely on the stack. Stack arguments begin just after
608 the return address. Once an argument has overflowed onto the stack
609 this way, all subsequent arguments are passed on the stack.
610
611 The above rule has odd consequences. For example, on the h8/300s,
612 if a function takes two longs and an int as arguments:
613 - the first long will be passed in r0/r1,
614 - the second long will be passed entirely on the stack, since it
615 doesn't fit in r2,
616 - and the int will be passed on the stack, even though it could fit
617 in r2.
618
619 A weird exception: if an argument is larger than a word, but not a
620 whole number of words in length (before padding), it is passed on
621 the stack following the rules for stack arguments above, even if
622 there are sufficient registers available to hold it. Stranger
623 still, the argument registers are still `used up' --- even though
624 there's nothing in them.
625
626 So, for example, on the h8/300s, if a function expects a three-byte
627 structure and an int, the structure will go on the stack, and the
628 int will go in r2, not r0.
629
630 If the function returns an aggregate type (struct, union, or class)
631 by value, the caller must allocate space to hold the return value,
632 and pass the callee a pointer to this space as an invisible first
633 argument, in R0.
634
635 For varargs functions, the last fixed argument and all the variable
636 arguments are always passed on the stack. This means that calls to
637 varargs functions don't work properly unless there is a prototype
638 in scope.
639
640 Basically, this ABI is not good, for the following reasons:
641 - You can't call vararg functions properly unless a prototype is in scope.
642 - Structure passing is inconsistent, to no purpose I can see.
643 - It often wastes argument registers, of which there are only three
644 to begin with. */
645
646 static CORE_ADDR
647 h8300_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
648 int struct_return, CORE_ADDR struct_addr)
649 {
650 int stack_align, stack_alloc, stack_offset;
651 int wordsize = BINWORD;
652 int reg;
653 int argument;
654
655 /* First, make sure the stack is properly aligned. */
656 sp = round_down (sp, wordsize);
657
658 /* Now make sure there's space on the stack for the arguments. We
659 may over-allocate a little here, but that won't hurt anything. */
660 stack_alloc = 0;
661 for (argument = 0; argument < nargs; argument++)
662 stack_alloc += round_up (TYPE_LENGTH (VALUE_TYPE (args[argument])),
663 wordsize);
664 sp -= stack_alloc;
665
666 /* Now load as many arguments as possible into registers, and push
667 the rest onto the stack. */
668 reg = E_ARG0_REGNUM;
669 stack_offset = 0;
670
671 /* If we're returning a structure by value, then we must pass a
672 pointer to the buffer for the return value as an invisible first
673 argument. */
674 if (struct_return)
675 write_register (reg++, struct_addr);
676
677 for (argument = 0; argument < nargs; argument++)
678 {
679 struct type *type = VALUE_TYPE (args[argument]);
680 int len = TYPE_LENGTH (type);
681 char *contents = (char *) VALUE_CONTENTS (args[argument]);
682
683 /* Pad the argument appropriately. */
684 int padded_len = round_up (len, wordsize);
685 char *padded = alloca (padded_len);
686
687 memset (padded, 0, padded_len);
688 memcpy (len < wordsize ? padded + padded_len - len : padded,
689 contents, len);
690
691 /* Could the argument fit in the remaining registers? */
692 if (padded_len <= (E_ARGLAST_REGNUM - reg + 1) * wordsize)
693 {
694 /* Are we going to pass it on the stack anyway, for no good
695 reason? */
696 if (len > wordsize && len % wordsize)
697 {
698 /* I feel so unclean. */
699 write_memory (sp + stack_offset, padded, padded_len);
700 stack_offset += padded_len;
701
702 /* That's right --- even though we passed the argument
703 on the stack, we consume the registers anyway! Love
704 me, love my dog. */
705 reg += padded_len / wordsize;
706 }
707 else
708 {
709 /* Heavens to Betsy --- it's really going in registers!
710 It would be nice if we could use write_register_bytes
711 here, but on the h8/300s, there are gaps between
712 the registers in the register file. */
713 int offset;
714
715 for (offset = 0; offset < padded_len; offset += wordsize)
716 {
717 ULONGEST word = extract_unsigned_integer (padded + offset,
718 wordsize);
719 write_register (reg++, word);
720 }
721 }
722 }
723 else
724 {
725 /* It doesn't fit in registers! Onto the stack it goes. */
726 write_memory (sp + stack_offset, padded, padded_len);
727 stack_offset += padded_len;
728
729 /* Once one argument has spilled onto the stack, all
730 subsequent arguments go on the stack. */
731 reg = E_ARGLAST_REGNUM + 1;
732 }
733 }
734
735 return sp;
736 }
737
738 /* Function: push_return_address
739 Setup the return address for a dummy frame, as called by
740 call_function_by_hand. Only necessary when you are using an
741 empty CALL_DUMMY, ie. the target will not actually be executing
742 a JSR/BSR instruction. */
743
744 static CORE_ADDR
745 h8300_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
746 {
747 unsigned char buf[4];
748 int wordsize = BINWORD;
749
750 sp -= wordsize;
751 store_unsigned_integer (buf, wordsize, CALL_DUMMY_ADDRESS ());
752 write_memory (sp, buf, wordsize);
753 return sp;
754 }
755
756 /* Function: h8300_pop_frame
757 Restore the machine to the state it had before the current frame
758 was created. Usually used either by the "RETURN" command, or by
759 call_function_by_hand after the dummy_frame is finished. */
760
761 static void
762 h8300_pop_frame (void)
763 {
764 unsigned regno;
765 struct frame_info *frame = get_current_frame ();
766
767 if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame),
768 get_frame_base (frame),
769 get_frame_base (frame)))
770 {
771 generic_pop_dummy_frame ();
772 }
773 else
774 {
775 for (regno = 0; regno < 8; regno++)
776 {
777 /* Don't forget E_SP_REGNUM is a frame_saved_regs struct is the
778 actual value we want, not the address of the value we want. */
779 if (get_frame_saved_regs (frame)[regno] && regno != E_SP_REGNUM)
780 write_register (regno,
781 read_memory_integer
782 (get_frame_saved_regs (frame)[regno], BINWORD));
783 else if (get_frame_saved_regs (frame)[regno] && regno == E_SP_REGNUM)
784 write_register (regno, get_frame_base (frame) + 2 * BINWORD);
785 }
786
787 /* Don't forget to update the PC too! */
788 write_register (E_PC_REGNUM, get_frame_extra_info (frame)->from_pc);
789 }
790 flush_cached_frames ();
791 }
792
793 /* Function: extract_return_value
794 Figure out where in REGBUF the called function has left its return value.
795 Copy that into VALBUF. Be sure to account for CPU type. */
796
797 static void
798 h8300_extract_return_value (struct type *type, char *regbuf, char *valbuf)
799 {
800 int wordsize = BINWORD;
801 int len = TYPE_LENGTH (type);
802
803 switch (len)
804 {
805 case 1: /* (char) */
806 case 2: /* (short), (int) */
807 memcpy (valbuf, regbuf + REGISTER_BYTE (0) + (wordsize - len), len);
808 break;
809 case 4: /* (long), (float) */
810 if (wordsize == 4)
811 {
812 memcpy (valbuf, regbuf + REGISTER_BYTE (0), 4);
813 }
814 else
815 {
816 memcpy (valbuf, regbuf + REGISTER_BYTE (0), 2);
817 memcpy (valbuf + 2, regbuf + REGISTER_BYTE (1), 2);
818 }
819 break;
820 case 8: /* (double) (doesn't seem to happen, which is good,
821 because this almost certainly isn't right.
822 FIXME: it will happen for h8sx... */
823 error ("I don't know how a double is returned.");
824 break;
825 }
826 }
827
828 /* Function: store_return_value
829 Place the appropriate value in the appropriate registers.
830 Primarily used by the RETURN command. */
831
832 static void
833 h8300_store_return_value (struct type *type, char *valbuf)
834 {
835 int regval;
836 int wordsize = BINWORD;
837 int len = TYPE_LENGTH (type);
838
839 switch (len)
840 {
841 case 1: /* char */
842 case 2: /* short, int */
843 regval = extract_unsigned_integer (valbuf, len);
844 write_register (0, regval);
845 break;
846 case 4: /* long, float */
847 regval = extract_unsigned_integer (valbuf, len);
848 if (wordsize == 4)
849 {
850 write_register (0, regval);
851 }
852 else
853 {
854 write_register (0, regval >> 16);
855 write_register (1, regval & 0xffff);
856 }
857 break;
858 case 8: /* presumeably double, but doesn't seem to happen */
859 error ("I don't know how to return a double.");
860 break;
861 }
862 }
863
864 static struct cmd_list_element *setmachinelist;
865
866 static const char *
867 h8300_register_name (int regno)
868 {
869 /* The register names change depending on which h8300 processor
870 type is selected. */
871 static char *register_names[] = {
872 "r0", "r1", "r2", "r3", "r4", "r5", "r6",
873 "sp", "ccr","pc","cycles", "tick", "inst", ""
874 };
875 if (regno < 0
876 || regno >= (sizeof (register_names) / sizeof (*register_names)))
877 internal_error (__FILE__, __LINE__,
878 "h8300_register_name: illegal register number %d", regno);
879 else
880 return register_names[regno];
881 }
882
883 static const char *
884 h8300s_register_name (int regno)
885 {
886 static char *register_names[] = {
887 "er0", "er1", "er2", "er3", "er4", "er5", "er6",
888 "sp", "ccr", "pc", "cycles", "exr", "tick", "inst"
889 };
890 if (regno < 0
891 || regno >= (sizeof (register_names) / sizeof (*register_names)))
892 internal_error (__FILE__, __LINE__,
893 "h8300s_register_name: illegal register number %d", regno);
894 else
895 return register_names[regno];
896 }
897
898 static const char *
899 h8300sx_register_name (int regno)
900 {
901 static char *register_names[] = {
902 "er0", "er1", "er2", "er3", "er4", "er5", "er6",
903 "sp", "ccr", "pc", "cycles", "exr", "tick", "inst",
904 "mach", "macl", "sbr", "vbr"
905 };
906 if (regno < 0
907 || regno >= (sizeof (register_names) / sizeof (*register_names)))
908 internal_error (__FILE__, __LINE__,
909 "h8300sx_register_name: illegal register number %d", regno);
910 else
911 return register_names[regno];
912 }
913
914 static void
915 h8300_print_register (struct gdbarch *gdbarch, struct ui_file *file,
916 struct frame_info *frame, int regno)
917 {
918 LONGEST rval;
919 const char *name = gdbarch_register_name (gdbarch, regno);
920
921 if (!name || !*name)
922 return;
923
924 frame_read_signed_register (frame, regno, &rval);
925
926 fprintf_filtered (file, "%-14s ", name);
927 if (regno == E_CCR_REGNUM || (regno == E_EXR_REGNUM && h8300smode))
928 {
929 fprintf_filtered (file, "0x%02x ", (unsigned char)rval);
930 print_longest (file, 'u', 1, rval);
931 }
932 else
933 {
934 fprintf_filtered (file, "0x%s ", phex ((ULONGEST)rval, BINWORD));
935 print_longest (file, 'd', 1, rval);
936 }
937 if (regno == E_CCR_REGNUM)
938 {
939 /* CCR register */
940 int C, Z, N, V;
941 unsigned char l = rval & 0xff;
942 fprintf_filtered (file, "\t");
943 fprintf_filtered (file, "I-%d ", (l & 0x80) != 0);
944 fprintf_filtered (file, "UI-%d ", (l & 0x40) != 0);
945 fprintf_filtered (file, "H-%d ", (l & 0x20) != 0);
946 fprintf_filtered (file, "U-%d ", (l & 0x10) != 0);
947 N = (l & 0x8) != 0;
948 Z = (l & 0x4) != 0;
949 V = (l & 0x2) != 0;
950 C = (l & 0x1) != 0;
951 fprintf_filtered (file, "N-%d ", N);
952 fprintf_filtered (file, "Z-%d ", Z);
953 fprintf_filtered (file, "V-%d ", V);
954 fprintf_filtered (file, "C-%d ", C);
955 if ((C | Z) == 0)
956 fprintf_filtered (file, "u> ");
957 if ((C | Z) == 1)
958 fprintf_filtered (file, "u<= ");
959 if ((C == 0))
960 fprintf_filtered (file, "u>= ");
961 if (C == 1)
962 fprintf_filtered (file, "u< ");
963 if (Z == 0)
964 fprintf_filtered (file, "!= ");
965 if (Z == 1)
966 fprintf_filtered (file, "== ");
967 if ((N ^ V) == 0)
968 fprintf_filtered (file, ">= ");
969 if ((N ^ V) == 1)
970 fprintf_filtered (file, "< ");
971 if ((Z | (N ^ V)) == 0)
972 fprintf_filtered (file, "> ");
973 if ((Z | (N ^ V)) == 1)
974 fprintf_filtered (file, "<= ");
975 }
976 else if (regno == E_EXR_REGNUM && h8300smode)
977 {
978 /* EXR register */
979 unsigned char l = rval & 0xff;
980 fprintf_filtered (file, "\t");
981 fprintf_filtered (file, "T-%d - - - ", (l & 0x80) != 0);
982 fprintf_filtered (file, "I2-%d ", (l & 4) != 0);
983 fprintf_filtered (file, "I1-%d ", (l & 2) != 0);
984 fprintf_filtered (file, "I0-%d", (l & 1) != 0);
985 }
986 fprintf_filtered (file, "\n");
987 }
988
989 static void
990 h8300_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
991 struct frame_info *frame, int regno, int cpregs)
992 {
993 if (regno < 0)
994 for (regno = 0; regno < NUM_REGS; ++regno)
995 h8300_print_register (gdbarch, file, frame, regno);
996 else
997 h8300_print_register (gdbarch, file, frame, regno);
998 }
999
1000 static CORE_ADDR
1001 h8300_saved_pc_after_call (struct frame_info *ignore)
1002 {
1003 return read_memory_unsigned_integer (read_register (E_SP_REGNUM), BINWORD);
1004 }
1005
1006 static int
1007 h8300_register_byte (int regno)
1008 {
1009 if (regno < 0 || regno >= NUM_REGS)
1010 internal_error (__FILE__, __LINE__,
1011 "h8300_register_byte: illegal register number %d", regno);
1012 else
1013 return regno * h8300_reg_size;
1014 }
1015
1016 static int
1017 h8300h_register_byte (int regno)
1018 {
1019 if (regno < 0 || regno >= NUM_REGS)
1020 internal_error (__FILE__, __LINE__,
1021 "h8300_register_byte: illegal register number %d", regno);
1022 else
1023 return regno * h8300h_reg_size;
1024 }
1025
1026 static struct type *
1027 h8300_register_virtual_type (int regno)
1028 {
1029 if (regno < 0 || regno >= NUM_REGS)
1030 internal_error (__FILE__, __LINE__,
1031 "h8300_register_virtual_type: illegal register number %d",
1032 regno);
1033 else
1034 {
1035 switch (regno)
1036 {
1037 case E_PC_REGNUM:
1038 return builtin_type_void_func_ptr;
1039 case E_SP_REGNUM:
1040 case E_FP_REGNUM:
1041 return builtin_type_void_data_ptr;
1042 case E_CCR_REGNUM:
1043 return builtin_type_uint8;
1044 case E_EXR_REGNUM:
1045 if (h8300smode)
1046 return builtin_type_uint8;
1047 /*FALLTHRU*/
1048 default:
1049 return h8300hmode ? builtin_type_int32
1050 : builtin_type_int16;
1051 }
1052 }
1053 }
1054
1055 static void
1056 h8300_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
1057 {
1058 write_register (0, addr);
1059 }
1060
1061 static CORE_ADDR
1062 h8300_extract_struct_value_address (char *regbuf)
1063 {
1064 return
1065 extract_unsigned_integer (regbuf + h8300_register_byte (E_ARG0_REGNUM),
1066 h8300_reg_size);
1067 }
1068
1069 static CORE_ADDR
1070 h8300h_extract_struct_value_address (char *regbuf)
1071 {
1072 return
1073 extract_unsigned_integer (regbuf + h8300_register_byte (E_ARG0_REGNUM),
1074 h8300h_reg_size);
1075 }
1076
1077 const static unsigned char *
1078 h8300_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
1079 {
1080 /*static unsigned char breakpoint[] = { 0x7A, 0xFF };*/ /* ??? */
1081 static unsigned char breakpoint[] = { 0x01, 0x80 }; /* Sleep */
1082
1083 *lenptr = sizeof (breakpoint);
1084 return breakpoint;
1085 }
1086
1087 static void
1088 h8300_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
1089 struct frame_info *frame, const char *args)
1090 {
1091 fprintf_filtered (file, "\
1092 No floating-point info available for this processor.\n");
1093 }
1094
1095 static struct gdbarch *
1096 h8300_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1097 {
1098 static LONGEST call_dummy_words[1] = { 0 };
1099 struct gdbarch_tdep *tdep = NULL;
1100 struct gdbarch *gdbarch;
1101
1102 arches = gdbarch_list_lookup_by_info (arches, &info);
1103 if (arches != NULL)
1104 return arches->gdbarch;
1105
1106 #if 0
1107 tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
1108 #endif
1109
1110 if (info.bfd_arch_info->arch != bfd_arch_h8300)
1111 return NULL;
1112
1113 gdbarch = gdbarch_alloc (&info, 0);
1114
1115 switch (info.bfd_arch_info->mach)
1116 {
1117 case bfd_mach_h8300:
1118 h8300sxmode = 0;
1119 h8300smode = 0;
1120 h8300hmode = 0;
1121 set_gdbarch_num_regs (gdbarch, 13);
1122 set_gdbarch_register_name (gdbarch, h8300_register_name);
1123 set_gdbarch_register_byte (gdbarch, h8300_register_byte);
1124 set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1125 set_gdbarch_addr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1126 break;
1127 case bfd_mach_h8300h:
1128 case bfd_mach_h8300hn:
1129 h8300sxmode = 0;
1130 h8300smode = 0;
1131 h8300hmode = 1;
1132 set_gdbarch_num_regs (gdbarch, 13);
1133 set_gdbarch_register_name (gdbarch, h8300_register_name);
1134 set_gdbarch_register_byte (gdbarch, h8300h_register_byte);
1135 set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1136 set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1137 break;
1138 case bfd_mach_h8300s:
1139 case bfd_mach_h8300sn:
1140 h8300sxmode = 0;
1141 h8300smode = 1;
1142 h8300hmode = 1;
1143 set_gdbarch_num_regs (gdbarch, 14);
1144 set_gdbarch_register_name (gdbarch, h8300s_register_name);
1145 set_gdbarch_register_byte (gdbarch, h8300h_register_byte);
1146 set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1147 set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1148 break;
1149 case bfd_mach_h8300sx:
1150 case bfd_mach_h8300sxn:
1151 h8300sxmode = 1;
1152 h8300smode = 1;
1153 h8300hmode = 1;
1154 set_gdbarch_num_regs (gdbarch, 18);
1155 set_gdbarch_register_name (gdbarch, h8300sx_register_name);
1156 set_gdbarch_register_byte (gdbarch, h8300h_register_byte);
1157 set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1158 set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1159 break;
1160 }
1161
1162 /* NOTE: cagney/2002-12-06: This can be deleted when this arch is
1163 ready to unwind the PC first (see frame.c:get_prev_frame()). */
1164 set_gdbarch_deprecated_init_frame_pc (gdbarch, init_frame_pc_default);
1165
1166 /*
1167 * Basic register fields and methods.
1168 */
1169
1170 set_gdbarch_num_pseudo_regs (gdbarch, 0);
1171 set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);
1172 set_gdbarch_deprecated_fp_regnum (gdbarch, E_FP_REGNUM);
1173 set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);
1174 set_gdbarch_register_virtual_type (gdbarch, h8300_register_virtual_type);
1175 set_gdbarch_print_registers_info (gdbarch, h8300_print_registers_info);
1176 set_gdbarch_print_float_info (gdbarch, h8300_print_float_info);
1177
1178 /*
1179 * Frame Info
1180 */
1181 set_gdbarch_deprecated_frame_init_saved_regs (gdbarch,
1182 h8300_frame_init_saved_regs);
1183 set_gdbarch_deprecated_init_extra_frame_info (gdbarch,
1184 h8300_init_extra_frame_info);
1185 set_gdbarch_deprecated_frame_chain (gdbarch, h8300_frame_chain);
1186 set_gdbarch_deprecated_saved_pc_after_call (gdbarch,
1187 h8300_saved_pc_after_call);
1188 set_gdbarch_deprecated_frame_saved_pc (gdbarch, h8300_frame_saved_pc);
1189 set_gdbarch_skip_prologue (gdbarch, h8300_skip_prologue);
1190 set_gdbarch_frame_args_address (gdbarch, h8300_frame_args_address);
1191 set_gdbarch_frame_locals_address (gdbarch, h8300_frame_locals_address);
1192
1193 /*
1194 * Miscelany
1195 */
1196 /* Stack grows up. */
1197 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1198 /* PC stops zero byte after a trap instruction
1199 (which means: exactly on trap instruction). */
1200 set_gdbarch_decr_pc_after_break (gdbarch, 0);
1201 /* This value is almost never non-zero... */
1202 set_gdbarch_function_start_offset (gdbarch, 0);
1203 /* This value is almost never non-zero... */
1204 set_gdbarch_frame_args_skip (gdbarch, 0);
1205 set_gdbarch_frameless_function_invocation (gdbarch,
1206 frameless_look_for_prologue);
1207
1208 /*
1209 * Call Dummies
1210 *
1211 * These values and methods are used when gdb calls a target function. */
1212 set_gdbarch_deprecated_push_return_address (gdbarch,
1213 h8300_push_return_address);
1214 set_gdbarch_deprecated_extract_return_value (gdbarch,
1215 h8300_extract_return_value);
1216 set_gdbarch_deprecated_push_arguments (gdbarch, h8300_push_arguments);
1217 set_gdbarch_deprecated_pop_frame (gdbarch, h8300_pop_frame);
1218 set_gdbarch_deprecated_store_struct_return (gdbarch,
1219 h8300_store_struct_return);
1220 set_gdbarch_deprecated_store_return_value (gdbarch,
1221 h8300_store_return_value);
1222 set_gdbarch_deprecated_extract_struct_value_address
1223 (gdbarch, h8300_extract_struct_value_address);
1224 set_gdbarch_use_struct_convention (gdbarch, always_use_struct_convention);
1225 set_gdbarch_deprecated_call_dummy_words (gdbarch, call_dummy_words);
1226 set_gdbarch_deprecated_sizeof_call_dummy_words (gdbarch, 0);
1227 set_gdbarch_breakpoint_from_pc (gdbarch, h8300_breakpoint_from_pc);
1228
1229 set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1230 set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1231 set_gdbarch_long_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1232 set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1233 set_gdbarch_long_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1234
1235 /* set_gdbarch_stack_align (gdbarch, SOME_stack_align); */
1236 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
1237
1238 /* Should be using push_dummy_call. */
1239 set_gdbarch_deprecated_dummy_write_sp (gdbarch, deprecated_write_sp);
1240
1241 return gdbarch;
1242 }
1243
1244 extern initialize_file_ftype _initialize_h8300_tdep; /* -Wmissing-prototypes */
1245
1246 void
1247 _initialize_h8300_tdep (void)
1248 {
1249 deprecated_tm_print_insn = gdb_print_insn_h8300;
1250 register_gdbarch_init (bfd_arch_h8300, h8300_gdbarch_init);
1251 }
This page took 0.056233 seconds and 4 git commands to generate.