Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Target-machine dependent code for Hitachi H8/300, for GDB. |
cda5a58a AC |
2 | |
3 | Copyright 1988, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, | |
4 | 1999, 2000, 2001, 2002 Free Software Foundation, Inc. | |
c906108c | 5 | |
c5aa993b | 6 | This file is part of GDB. |
c906108c | 7 | |
c5aa993b JM |
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. | |
c906108c | 12 | |
c5aa993b JM |
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. | |
c906108c | 17 | |
c5aa993b JM |
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. */ | |
c906108c SS |
22 | |
23 | /* | |
c5aa993b JM |
24 | Contributed by Steve Chamberlain |
25 | sac@cygnus.com | |
c906108c SS |
26 | */ |
27 | ||
28 | #include "defs.h" | |
29 | #include "frame.h" | |
30 | #include "obstack.h" | |
31 | #include "symtab.h" | |
32 | #include "dis-asm.h" | |
33 | #include "gdbcmd.h" | |
34 | #include "gdbtypes.h" | |
35 | #include "gdbcore.h" | |
36 | #include "gdb_string.h" | |
37 | #include "value.h" | |
4e052eda | 38 | #include "regcache.h" |
c906108c SS |
39 | |
40 | extern int h8300hmode, h8300smode; | |
41 | ||
42 | #undef NUM_REGS | |
43 | #define NUM_REGS 11 | |
44 | ||
45 | #define UNSIGNED_SHORT(X) ((X) & 0xffff) | |
46 | ||
47 | #define IS_PUSH(x) ((x & 0xfff0)==0x6df0) | |
48 | #define IS_PUSH_FP(x) (x == 0x6df6) | |
49 | #define IS_MOVE_FP(x) (x == 0x0d76 || x == 0x0ff6) | |
50 | #define IS_MOV_SP_FP(x) (x == 0x0d76 || x == 0x0ff6) | |
51 | #define IS_SUB2_SP(x) (x==0x1b87) | |
52 | #define IS_SUB4_SP(x) (x==0x1b97) | |
53 | #define IS_SUBL_SP(x) (x==0x7a37) | |
54 | #define IS_MOVK_R5(x) (x==0x7905) | |
55 | #define IS_SUB_R5SP(x) (x==0x1957) | |
56 | ||
57 | ||
58 | /* The register names change depending on whether the h8300h processor | |
59 | type is selected. */ | |
60 | ||
61 | static char *original_register_names[] = REGISTER_NAMES; | |
62 | ||
63 | static char *h8300h_register_names[] = | |
c5aa993b JM |
64 | {"er0", "er1", "er2", "er3", "er4", "er5", "er6", |
65 | "sp", "ccr", "pc", "cycles", "tick", "inst"}; | |
c906108c SS |
66 | |
67 | char **h8300_register_names = original_register_names; | |
68 | ||
69 | ||
70 | /* Local function declarations. */ | |
71 | ||
72 | static CORE_ADDR examine_prologue (); | |
a14ed312 | 73 | static void set_machine_hook (char *filename); |
c906108c | 74 | |
c5aa993b | 75 | CORE_ADDR |
fba45db2 | 76 | h8300_skip_prologue (CORE_ADDR start_pc) |
c906108c SS |
77 | { |
78 | short int w; | |
79 | int adjust = 0; | |
80 | ||
81 | /* Skip past all push and stm insns. */ | |
82 | while (1) | |
83 | { | |
84 | w = read_memory_unsigned_integer (start_pc, 2); | |
85 | /* First look for push insns. */ | |
86 | if (w == 0x0100 || w == 0x0110 || w == 0x0120 || w == 0x0130) | |
87 | { | |
88 | w = read_memory_unsigned_integer (start_pc + 2, 2); | |
89 | adjust = 2; | |
90 | } | |
91 | ||
92 | if (IS_PUSH (w)) | |
93 | { | |
94 | start_pc += 2 + adjust; | |
95 | w = read_memory_unsigned_integer (start_pc, 2); | |
96 | continue; | |
97 | } | |
98 | adjust = 0; | |
99 | break; | |
100 | } | |
101 | ||
102 | /* Skip past a move to FP, either word or long sized */ | |
103 | w = read_memory_unsigned_integer (start_pc, 2); | |
104 | if (w == 0x0100) | |
105 | { | |
106 | w = read_memory_unsigned_integer (start_pc + 2, 2); | |
107 | adjust += 2; | |
108 | } | |
109 | ||
110 | if (IS_MOVE_FP (w)) | |
111 | { | |
112 | start_pc += 2 + adjust; | |
113 | w = read_memory_unsigned_integer (start_pc, 2); | |
114 | } | |
115 | ||
116 | /* Check for loading either a word constant into r5; | |
117 | long versions are handled by the SUBL_SP below. */ | |
118 | if (IS_MOVK_R5 (w)) | |
119 | { | |
120 | start_pc += 2; | |
121 | w = read_memory_unsigned_integer (start_pc, 2); | |
122 | } | |
123 | ||
124 | /* Now check for subtracting r5 from sp, word sized only. */ | |
125 | if (IS_SUB_R5SP (w)) | |
126 | { | |
127 | start_pc += 2 + adjust; | |
128 | w = read_memory_unsigned_integer (start_pc, 2); | |
129 | } | |
130 | ||
131 | /* Check for subs #2 and subs #4. */ | |
132 | while (IS_SUB2_SP (w) || IS_SUB4_SP (w)) | |
133 | { | |
134 | start_pc += 2 + adjust; | |
135 | w = read_memory_unsigned_integer (start_pc, 2); | |
136 | } | |
137 | ||
138 | /* Check for a 32bit subtract. */ | |
139 | if (IS_SUBL_SP (w)) | |
140 | start_pc += 6 + adjust; | |
141 | ||
142 | return start_pc; | |
143 | } | |
144 | ||
145 | int | |
fba45db2 | 146 | gdb_print_insn_h8300 (bfd_vma memaddr, disassemble_info *info) |
c906108c SS |
147 | { |
148 | if (h8300smode) | |
149 | return print_insn_h8300s (memaddr, info); | |
150 | else if (h8300hmode) | |
151 | return print_insn_h8300h (memaddr, info); | |
152 | else | |
153 | return print_insn_h8300 (memaddr, info); | |
154 | } | |
155 | ||
156 | /* Given a GDB frame, determine the address of the calling function's frame. | |
157 | This will be used to create a new GDB frame struct, and then | |
158 | INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame. | |
159 | ||
160 | For us, the frame address is its stack pointer value, so we look up | |
161 | the function prologue to determine the caller's sp value, and return it. */ | |
162 | ||
163 | CORE_ADDR | |
fba45db2 | 164 | h8300_frame_chain (struct frame_info *thisframe) |
c906108c | 165 | { |
c5aa993b JM |
166 | if (PC_IN_CALL_DUMMY (thisframe->pc, thisframe->frame, thisframe->frame)) |
167 | { /* initialize the from_pc now */ | |
c906108c | 168 | thisframe->from_pc = generic_read_register_dummy (thisframe->pc, |
c5aa993b | 169 | thisframe->frame, |
c906108c SS |
170 | PC_REGNUM); |
171 | return thisframe->frame; | |
172 | } | |
173 | h8300_frame_find_saved_regs (thisframe, (struct frame_saved_regs *) 0); | |
174 | return thisframe->fsr->regs[SP_REGNUM]; | |
175 | } | |
176 | ||
177 | /* Put here the code to store, into a struct frame_saved_regs, | |
178 | the addresses of the saved registers of frame described by FRAME_INFO. | |
179 | This includes special registers such as pc and fp saved in special | |
180 | ways in the stack frame. sp is even more special: | |
181 | the address we return for it IS the sp for the next frame. | |
182 | ||
183 | We cache the result of doing this in the frame_obstack, since it is | |
184 | fairly expensive. */ | |
185 | ||
186 | void | |
fba45db2 KB |
187 | h8300_frame_find_saved_regs (struct frame_info *fi, |
188 | struct frame_saved_regs *fsr) | |
c906108c SS |
189 | { |
190 | register struct frame_saved_regs *cache_fsr; | |
191 | CORE_ADDR ip; | |
192 | struct symtab_and_line sal; | |
193 | CORE_ADDR limit; | |
194 | ||
195 | if (!fi->fsr) | |
196 | { | |
197 | cache_fsr = (struct frame_saved_regs *) | |
198 | frame_obstack_alloc (sizeof (struct frame_saved_regs)); | |
199 | memset (cache_fsr, '\0', sizeof (struct frame_saved_regs)); | |
200 | ||
201 | fi->fsr = cache_fsr; | |
202 | ||
c5aa993b JM |
203 | if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame)) |
204 | { /* no more to do. */ | |
c906108c SS |
205 | if (fsr) |
206 | *fsr = *fi->fsr; | |
207 | return; | |
208 | } | |
209 | /* Find the start and end of the function prologue. If the PC | |
c5aa993b JM |
210 | is in the function prologue, we only consider the part that |
211 | has executed already. */ | |
c906108c SS |
212 | |
213 | ip = get_pc_function_start (fi->pc); | |
214 | sal = find_pc_line (ip, 0); | |
215 | limit = (sal.end && sal.end < fi->pc) ? sal.end : fi->pc; | |
216 | ||
217 | /* This will fill in fields in *fi as well as in cache_fsr. */ | |
218 | examine_prologue (ip, limit, fi->frame, cache_fsr, fi); | |
219 | } | |
220 | ||
221 | if (fsr) | |
222 | *fsr = *fi->fsr; | |
223 | } | |
224 | ||
225 | /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or | |
226 | is not the address of a valid instruction, the address of the next | |
227 | instruction beyond ADDR otherwise. *PWORD1 receives the first word | |
c5aa993b | 228 | of the instruction. */ |
c906108c SS |
229 | |
230 | CORE_ADDR | |
fba45db2 | 231 | NEXT_PROLOGUE_INSN (CORE_ADDR addr, CORE_ADDR lim, INSN_WORD *pword1) |
c906108c SS |
232 | { |
233 | char buf[2]; | |
234 | if (addr < lim + 8) | |
235 | { | |
236 | read_memory (addr, buf, 2); | |
237 | *pword1 = extract_signed_integer (buf, 2); | |
238 | ||
239 | return addr + 2; | |
240 | } | |
241 | return 0; | |
242 | } | |
243 | ||
244 | /* Examine the prologue of a function. `ip' points to the first instruction. | |
245 | `limit' is the limit of the prologue (e.g. the addr of the first | |
246 | linenumber, or perhaps the program counter if we're stepping through). | |
247 | `frame_sp' is the stack pointer value in use in this frame. | |
248 | `fsr' is a pointer to a frame_saved_regs structure into which we put | |
249 | info about the registers saved by this frame. | |
250 | `fi' is a struct frame_info pointer; we fill in various fields in it | |
251 | to reflect the offsets of the arg pointer and the locals pointer. */ | |
252 | ||
253 | static CORE_ADDR | |
fba45db2 KB |
254 | examine_prologue (register CORE_ADDR ip, register CORE_ADDR limit, |
255 | CORE_ADDR after_prolog_fp, struct frame_saved_regs *fsr, | |
256 | struct frame_info *fi) | |
c906108c SS |
257 | { |
258 | register CORE_ADDR next_ip; | |
259 | int r; | |
260 | int have_fp = 0; | |
261 | INSN_WORD insn_word; | |
262 | /* Number of things pushed onto stack, starts at 2/4, 'cause the | |
263 | PC is already there */ | |
264 | unsigned int reg_save_depth = h8300hmode ? 4 : 2; | |
265 | ||
266 | unsigned int auto_depth = 0; /* Number of bytes of autos */ | |
267 | ||
268 | char in_frame[11]; /* One for each reg */ | |
269 | ||
270 | int adjust = 0; | |
271 | ||
272 | memset (in_frame, 1, 11); | |
273 | for (r = 0; r < 8; r++) | |
274 | { | |
275 | fsr->regs[r] = 0; | |
276 | } | |
277 | if (after_prolog_fp == 0) | |
278 | { | |
279 | after_prolog_fp = read_register (SP_REGNUM); | |
280 | } | |
281 | ||
282 | /* If the PC isn't valid, quit now. */ | |
283 | if (ip == 0 || ip & (h8300hmode ? ~0xffffff : ~0xffff)) | |
284 | return 0; | |
285 | ||
286 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); | |
287 | ||
288 | if (insn_word == 0x0100) | |
289 | { | |
290 | insn_word = read_memory_unsigned_integer (ip + 2, 2); | |
291 | adjust = 2; | |
292 | } | |
293 | ||
294 | /* Skip over any fp push instructions */ | |
295 | fsr->regs[6] = after_prolog_fp; | |
296 | while (next_ip && IS_PUSH_FP (insn_word)) | |
297 | { | |
298 | ip = next_ip + adjust; | |
299 | ||
300 | in_frame[insn_word & 0x7] = reg_save_depth; | |
301 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); | |
302 | reg_save_depth += 2 + adjust; | |
303 | } | |
304 | ||
305 | /* Is this a move into the fp */ | |
306 | if (next_ip && IS_MOV_SP_FP (insn_word)) | |
307 | { | |
308 | ip = next_ip; | |
309 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); | |
310 | have_fp = 1; | |
311 | } | |
312 | ||
313 | /* Skip over any stack adjustment, happens either with a number of | |
314 | sub#2,sp or a mov #x,r5 sub r5,sp */ | |
315 | ||
316 | if (next_ip && (IS_SUB2_SP (insn_word) || IS_SUB4_SP (insn_word))) | |
317 | { | |
318 | while (next_ip && (IS_SUB2_SP (insn_word) || IS_SUB4_SP (insn_word))) | |
319 | { | |
320 | auto_depth += IS_SUB2_SP (insn_word) ? 2 : 4; | |
321 | ip = next_ip; | |
322 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); | |
323 | } | |
324 | } | |
325 | else | |
326 | { | |
327 | if (next_ip && IS_MOVK_R5 (insn_word)) | |
328 | { | |
329 | ip = next_ip; | |
330 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); | |
331 | auto_depth += insn_word; | |
332 | ||
333 | next_ip = NEXT_PROLOGUE_INSN (next_ip, limit, &insn_word); | |
334 | auto_depth += insn_word; | |
335 | } | |
336 | if (next_ip && IS_SUBL_SP (insn_word)) | |
337 | { | |
338 | ip = next_ip; | |
339 | auto_depth += read_memory_unsigned_integer (ip, 4); | |
340 | ip += 4; | |
341 | ||
342 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); | |
343 | } | |
344 | } | |
345 | ||
346 | /* Now examine the push insns to determine where everything lives | |
347 | on the stack. */ | |
348 | while (1) | |
349 | { | |
350 | adjust = 0; | |
351 | if (!next_ip) | |
352 | break; | |
353 | ||
354 | if (insn_word == 0x0100) | |
355 | { | |
356 | ip = next_ip; | |
357 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); | |
358 | adjust = 2; | |
359 | } | |
360 | ||
361 | if (IS_PUSH (insn_word)) | |
362 | { | |
363 | ip = next_ip; | |
364 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); | |
365 | fsr->regs[r] = after_prolog_fp + auto_depth; | |
366 | auto_depth += 2 + adjust; | |
367 | continue; | |
368 | } | |
369 | ||
370 | /* Now check for push multiple insns. */ | |
371 | if (insn_word == 0x0110 || insn_word == 0x0120 || insn_word == 0x0130) | |
372 | { | |
373 | int count = ((insn_word >> 4) & 0xf) + 1; | |
374 | int start, i; | |
375 | ||
376 | ip = next_ip; | |
377 | next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn_word); | |
378 | start = insn_word & 0x7; | |
379 | ||
380 | for (i = start; i <= start + count; i++) | |
381 | { | |
382 | fsr->regs[i] = after_prolog_fp + auto_depth; | |
383 | auto_depth += 4; | |
384 | } | |
385 | } | |
386 | break; | |
387 | } | |
388 | ||
389 | /* The args are always reffed based from the stack pointer */ | |
390 | fi->args_pointer = after_prolog_fp; | |
391 | /* Locals are always reffed based from the fp */ | |
392 | fi->locals_pointer = after_prolog_fp; | |
393 | /* The PC is at a known place */ | |
394 | fi->from_pc = read_memory_unsigned_integer (after_prolog_fp + BINWORD, BINWORD); | |
395 | ||
396 | /* Rememeber any others too */ | |
397 | in_frame[PC_REGNUM] = 0; | |
c5aa993b | 398 | |
c906108c SS |
399 | if (have_fp) |
400 | /* We keep the old FP in the SP spot */ | |
401 | fsr->regs[SP_REGNUM] = read_memory_unsigned_integer (fsr->regs[6], BINWORD); | |
402 | else | |
403 | fsr->regs[SP_REGNUM] = after_prolog_fp + auto_depth; | |
404 | ||
405 | return (ip); | |
406 | } | |
407 | ||
408 | void | |
fba45db2 | 409 | h8300_init_extra_frame_info (int fromleaf, struct frame_info *fi) |
c906108c SS |
410 | { |
411 | fi->fsr = 0; /* Not yet allocated */ | |
412 | fi->args_pointer = 0; /* Unknown */ | |
413 | fi->locals_pointer = 0; /* Unknown */ | |
414 | fi->from_pc = 0; | |
c5aa993b JM |
415 | if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame)) |
416 | { /* anything special to do? */ | |
c906108c SS |
417 | return; |
418 | } | |
419 | } | |
420 | ||
421 | /* Return the saved PC from this frame. | |
422 | ||
423 | If the frame has a memory copy of SRP_REGNUM, use that. If not, | |
424 | just use the register SRP_REGNUM itself. */ | |
425 | ||
426 | CORE_ADDR | |
fba45db2 | 427 | h8300_frame_saved_pc (struct frame_info *frame) |
c906108c | 428 | { |
c5aa993b | 429 | if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame)) |
c906108c SS |
430 | return generic_read_register_dummy (frame->pc, frame->frame, PC_REGNUM); |
431 | else | |
432 | return frame->from_pc; | |
433 | } | |
434 | ||
435 | CORE_ADDR | |
7256e1a5 | 436 | h8300_frame_locals_address (struct frame_info *fi) |
c906108c | 437 | { |
c5aa993b | 438 | if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame)) |
c906108c SS |
439 | return (CORE_ADDR) 0; /* Not sure what else to do... */ |
440 | if (!fi->locals_pointer) | |
441 | { | |
442 | struct frame_saved_regs ignore; | |
443 | ||
444 | get_frame_saved_regs (fi, &ignore); | |
445 | ||
446 | } | |
447 | return fi->locals_pointer; | |
448 | } | |
449 | ||
450 | /* Return the address of the argument block for the frame | |
451 | described by FI. Returns 0 if the address is unknown. */ | |
452 | ||
453 | CORE_ADDR | |
7256e1a5 | 454 | h8300_frame_args_address (struct frame_info *fi) |
c906108c | 455 | { |
c5aa993b | 456 | if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame)) |
c906108c SS |
457 | return (CORE_ADDR) 0; /* Not sure what else to do... */ |
458 | if (!fi->args_pointer) | |
459 | { | |
460 | struct frame_saved_regs ignore; | |
461 | ||
462 | get_frame_saved_regs (fi, &ignore); | |
463 | ||
464 | } | |
465 | ||
466 | return fi->args_pointer; | |
467 | } | |
468 | ||
469 | /* Function: push_arguments | |
470 | Setup the function arguments for calling a function in the inferior. | |
471 | ||
472 | On the Hitachi H8/300 architecture, there are three registers (R0 to R2) | |
473 | which are dedicated for passing function arguments. Up to the first | |
474 | three arguments (depending on size) may go into these registers. | |
475 | The rest go on the stack. | |
476 | ||
477 | Arguments that are smaller than WORDSIZE bytes will still take up a | |
478 | whole register or a whole WORDSIZE word on the stack, and will be | |
479 | right-justified in the register or the stack word. This includes | |
480 | chars and small aggregate types. Note that WORDSIZE depends on the | |
481 | cpu type. | |
482 | ||
483 | Arguments that are larger than WORDSIZE bytes will be split between | |
484 | two or more registers as available, but will NOT be split between a | |
485 | register and the stack. | |
486 | ||
487 | An exceptional case exists for struct arguments (and possibly other | |
488 | aggregates such as arrays) -- if the size is larger than WORDSIZE | |
489 | bytes but not a multiple of WORDSIZE bytes. In this case the | |
490 | argument is never split between the registers and the stack, but | |
491 | instead is copied in its entirety onto the stack, AND also copied | |
492 | into as many registers as there is room for. In other words, space | |
493 | in registers permitting, two copies of the same argument are passed | |
494 | in. As far as I can tell, only the one on the stack is used, | |
495 | although that may be a function of the level of compiler | |
496 | optimization. I suspect this is a compiler bug. Arguments of | |
497 | these odd sizes are left-justified within the word (as opposed to | |
498 | arguments smaller than WORDSIZE bytes, which are right-justified). | |
c5aa993b | 499 | |
c906108c SS |
500 | If the function is to return an aggregate type such as a struct, |
501 | the caller must allocate space into which the callee will copy the | |
502 | return value. In this case, a pointer to the return value location | |
503 | is passed into the callee in register R0, which displaces one of | |
504 | the other arguments passed in via registers R0 to R2. */ | |
505 | ||
506 | CORE_ADDR | |
fba45db2 KB |
507 | h8300_push_arguments (int nargs, struct value **args, CORE_ADDR sp, |
508 | unsigned char struct_return, CORE_ADDR struct_addr) | |
c906108c SS |
509 | { |
510 | int stack_align, stack_alloc, stack_offset; | |
511 | int wordsize; | |
512 | int argreg; | |
513 | int argnum; | |
514 | struct type *type; | |
515 | CORE_ADDR regval; | |
516 | char *val; | |
517 | char valbuf[4]; | |
518 | int len; | |
519 | ||
520 | if (h8300hmode || h8300smode) | |
521 | { | |
522 | stack_align = 3; | |
c5aa993b | 523 | wordsize = 4; |
c906108c SS |
524 | } |
525 | else | |
526 | { | |
527 | stack_align = 1; | |
c5aa993b | 528 | wordsize = 2; |
c906108c SS |
529 | } |
530 | ||
531 | /* first force sp to a n-byte alignment */ | |
532 | sp = sp & ~stack_align; | |
533 | ||
534 | /* Now make sure there's space on the stack */ | |
c5aa993b | 535 | for (argnum = 0, stack_alloc = 0; |
c906108c | 536 | argnum < nargs; argnum++) |
c5aa993b | 537 | stack_alloc += ((TYPE_LENGTH (VALUE_TYPE (args[argnum])) + stack_align) |
c906108c | 538 | & ~stack_align); |
c5aa993b | 539 | sp -= stack_alloc; /* make room on stack for args */ |
c906108c SS |
540 | /* we may over-allocate a little here, but that won't hurt anything */ |
541 | ||
c5aa993b JM |
542 | argreg = ARG0_REGNUM; |
543 | if (struct_return) /* "struct return" pointer takes up one argreg */ | |
c906108c SS |
544 | { |
545 | write_register (argreg++, struct_addr); | |
546 | } | |
547 | ||
548 | /* Now load as many as possible of the first arguments into | |
549 | registers, and push the rest onto the stack. There are 3N bytes | |
550 | in three registers available. Loop thru args from first to last. */ | |
551 | ||
552 | for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++) | |
553 | { | |
554 | type = VALUE_TYPE (args[argnum]); | |
555 | len = TYPE_LENGTH (type); | |
c5aa993b | 556 | memset (valbuf, 0, sizeof (valbuf)); |
c906108c SS |
557 | if (len < wordsize) |
558 | { | |
559 | /* the purpose of this is to right-justify the value within the word */ | |
c5aa993b JM |
560 | memcpy (valbuf + (wordsize - len), |
561 | (char *) VALUE_CONTENTS (args[argnum]), len); | |
c906108c SS |
562 | val = valbuf; |
563 | } | |
564 | else | |
565 | val = (char *) VALUE_CONTENTS (args[argnum]); | |
566 | ||
c5aa993b JM |
567 | if (len > (ARGLAST_REGNUM + 1 - argreg) * REGISTER_RAW_SIZE (ARG0_REGNUM) || |
568 | (len > wordsize && (len & stack_align) != 0)) | |
569 | { /* passed on the stack */ | |
570 | write_memory (sp + stack_offset, val, | |
c906108c SS |
571 | len < wordsize ? wordsize : len); |
572 | stack_offset += (len + stack_align) & ~stack_align; | |
573 | } | |
574 | /* NOTE WELL!!!!! This is not an "else if" clause!!! | |
c5aa993b JM |
575 | That's because some *&^%$ things get passed on the stack |
576 | AND in the registers! */ | |
577 | if (len <= (ARGLAST_REGNUM + 1 - argreg) * REGISTER_RAW_SIZE (ARG0_REGNUM)) | |
c906108c | 578 | while (len > 0) |
c5aa993b | 579 | { /* there's room in registers */ |
c906108c SS |
580 | regval = extract_address (val, wordsize); |
581 | write_register (argreg, regval); | |
582 | len -= wordsize; | |
583 | val += wordsize; | |
584 | argreg++; | |
585 | } | |
586 | } | |
587 | return sp; | |
588 | } | |
589 | ||
590 | /* Function: push_return_address | |
591 | Setup the return address for a dummy frame, as called by | |
592 | call_function_by_hand. Only necessary when you are using an | |
593 | empty CALL_DUMMY, ie. the target will not actually be executing | |
594 | a JSR/BSR instruction. */ | |
595 | ||
596 | CORE_ADDR | |
fba45db2 | 597 | h8300_push_return_address (CORE_ADDR pc, CORE_ADDR sp) |
c906108c SS |
598 | { |
599 | unsigned char buf[4]; | |
600 | int wordsize; | |
601 | ||
602 | if (h8300hmode || h8300smode) | |
603 | wordsize = 4; | |
604 | else | |
605 | wordsize = 2; | |
606 | ||
607 | sp -= wordsize; | |
608 | store_unsigned_integer (buf, wordsize, CALL_DUMMY_ADDRESS ()); | |
609 | write_memory (sp, buf, wordsize); | |
610 | return sp; | |
611 | } | |
612 | ||
7256e1a5 | 613 | /* Function: h8300_pop_frame |
c906108c SS |
614 | Restore the machine to the state it had before the current frame |
615 | was created. Usually used either by the "RETURN" command, or by | |
616 | call_function_by_hand after the dummy_frame is finished. */ | |
617 | ||
c5aa993b | 618 | void |
fba45db2 | 619 | h8300_pop_frame (void) |
c906108c SS |
620 | { |
621 | unsigned regnum; | |
622 | struct frame_saved_regs fsr; | |
623 | struct frame_info *frame = get_current_frame (); | |
624 | ||
c5aa993b | 625 | if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame)) |
c906108c | 626 | { |
c5aa993b | 627 | generic_pop_dummy_frame (); |
c906108c SS |
628 | } |
629 | else | |
630 | { | |
631 | get_frame_saved_regs (frame, &fsr); | |
632 | ||
633 | for (regnum = 0; regnum < 8; regnum++) | |
634 | { | |
635 | /* Don't forget SP_REGNUM is a frame_saved_regs struct is the | |
636 | actual value we want, not the address of the value we want. */ | |
637 | if (fsr.regs[regnum] && regnum != SP_REGNUM) | |
c5aa993b JM |
638 | write_register (regnum, |
639 | read_memory_integer (fsr.regs[regnum], BINWORD)); | |
c906108c SS |
640 | else if (fsr.regs[regnum] && regnum == SP_REGNUM) |
641 | write_register (regnum, frame->frame + 2 * BINWORD); | |
642 | } | |
643 | ||
644 | /* Don't forget the update the PC too! */ | |
645 | write_pc (frame->from_pc); | |
646 | } | |
647 | flush_cached_frames (); | |
648 | } | |
649 | ||
650 | /* Function: extract_return_value | |
651 | Figure out where in REGBUF the called function has left its return value. | |
652 | Copy that into VALBUF. Be sure to account for CPU type. */ | |
653 | ||
654 | void | |
fba45db2 | 655 | h8300_extract_return_value (struct type *type, char *regbuf, char *valbuf) |
c906108c SS |
656 | { |
657 | int wordsize, len; | |
658 | ||
659 | if (h8300smode || h8300hmode) | |
660 | wordsize = 4; | |
661 | else | |
662 | wordsize = 2; | |
663 | ||
c5aa993b JM |
664 | len = TYPE_LENGTH (type); |
665 | ||
666 | switch (len) | |
667 | { | |
668 | case 1: /* (char) */ | |
669 | case 2: /* (short), (int) */ | |
670 | memcpy (valbuf, regbuf + REGISTER_BYTE (0) + (wordsize - len), len); | |
671 | break; | |
672 | case 4: /* (long), (float) */ | |
673 | if (h8300smode || h8300hmode) | |
674 | { | |
675 | memcpy (valbuf, regbuf + REGISTER_BYTE (0), 4); | |
676 | } | |
677 | else | |
678 | { | |
679 | memcpy (valbuf, regbuf + REGISTER_BYTE (0), 2); | |
680 | memcpy (valbuf + 2, regbuf + REGISTER_BYTE (1), 2); | |
681 | } | |
682 | break; | |
683 | case 8: /* (double) (doesn't seem to happen, which is good, | |
684 | because this almost certainly isn't right. */ | |
685 | error ("I don't know how a double is returned."); | |
686 | break; | |
687 | } | |
c906108c SS |
688 | } |
689 | ||
690 | /* Function: store_return_value | |
691 | Place the appropriate value in the appropriate registers. | |
692 | Primarily used by the RETURN command. */ | |
693 | ||
c5aa993b | 694 | void |
fba45db2 | 695 | h8300_store_return_value (struct type *type, char *valbuf) |
c906108c SS |
696 | { |
697 | int wordsize, len, regval; | |
c5aa993b | 698 | |
c906108c SS |
699 | if (h8300hmode || h8300smode) |
700 | wordsize = 4; | |
701 | else | |
702 | wordsize = 2; | |
703 | ||
c5aa993b JM |
704 | len = TYPE_LENGTH (type); |
705 | switch (len) | |
706 | { | |
707 | case 1: /* char */ | |
708 | case 2: /* short, int */ | |
709 | regval = extract_address (valbuf, len); | |
710 | write_register (0, regval); | |
711 | break; | |
712 | case 4: /* long, float */ | |
713 | regval = extract_address (valbuf, len); | |
714 | if (h8300smode || h8300hmode) | |
715 | { | |
716 | write_register (0, regval); | |
717 | } | |
718 | else | |
719 | { | |
720 | write_register (0, regval >> 16); | |
721 | write_register (1, regval & 0xffff); | |
722 | } | |
723 | break; | |
724 | case 8: /* presumeably double, but doesn't seem to happen */ | |
725 | error ("I don't know how to return a double."); | |
726 | break; | |
727 | } | |
c906108c SS |
728 | } |
729 | ||
c906108c SS |
730 | struct cmd_list_element *setmemorylist; |
731 | ||
732 | static void | |
fba45db2 | 733 | set_register_names (void) |
c906108c SS |
734 | { |
735 | if (h8300hmode != 0) | |
736 | h8300_register_names = h8300h_register_names; | |
737 | else | |
738 | h8300_register_names = original_register_names; | |
739 | } | |
740 | ||
741 | static void | |
55d80160 | 742 | h8300_command (char *args, int from_tty) |
c906108c SS |
743 | { |
744 | extern int h8300hmode; | |
745 | h8300hmode = 0; | |
746 | h8300smode = 0; | |
747 | set_register_names (); | |
748 | } | |
749 | ||
750 | static void | |
55d80160 | 751 | h8300h_command (char *args, int from_tty) |
c906108c SS |
752 | { |
753 | extern int h8300hmode; | |
754 | h8300hmode = 1; | |
755 | h8300smode = 0; | |
756 | set_register_names (); | |
757 | } | |
758 | ||
759 | static void | |
55d80160 | 760 | h8300s_command (char *args, int from_tty) |
c906108c SS |
761 | { |
762 | extern int h8300smode; | |
763 | extern int h8300hmode; | |
764 | h8300smode = 1; | |
765 | h8300hmode = 1; | |
766 | set_register_names (); | |
767 | } | |
768 | ||
769 | ||
c5aa993b | 770 | static void |
fba45db2 | 771 | set_machine (char *args, int from_tty) |
c906108c SS |
772 | { |
773 | printf_unfiltered ("\"set machine\" must be followed by h8300, h8300h"); | |
774 | printf_unfiltered ("or h8300s"); | |
775 | help_list (setmemorylist, "set memory ", -1, gdb_stdout); | |
776 | } | |
777 | ||
778 | /* set_machine_hook is called as the exec file is being opened, but | |
779 | before the symbol file is opened. This allows us to set the | |
780 | h8300hmode flag based on the machine type specified in the exec | |
781 | file. This in turn will cause subsequently defined pointer types | |
782 | to be 16 or 32 bits as appropriate for the machine. */ | |
783 | ||
784 | static void | |
fba45db2 | 785 | set_machine_hook (char *filename) |
c906108c SS |
786 | { |
787 | if (bfd_get_mach (exec_bfd) == bfd_mach_h8300s) | |
788 | { | |
789 | h8300smode = 1; | |
790 | h8300hmode = 1; | |
791 | } | |
c5aa993b | 792 | else if (bfd_get_mach (exec_bfd) == bfd_mach_h8300h) |
c906108c SS |
793 | { |
794 | h8300smode = 0; | |
795 | h8300hmode = 1; | |
796 | } | |
797 | else | |
798 | { | |
799 | h8300smode = 0; | |
800 | h8300hmode = 0; | |
801 | } | |
802 | set_register_names (); | |
803 | } | |
804 | ||
805 | void | |
fba45db2 | 806 | _initialize_h8300m (void) |
c906108c SS |
807 | { |
808 | add_prefix_cmd ("machine", no_class, set_machine, | |
c5aa993b | 809 | "set the machine type", |
c906108c SS |
810 | &setmemorylist, "set machine ", 0, |
811 | &setlist); | |
812 | ||
813 | add_cmd ("h8300", class_support, h8300_command, | |
814 | "Set machine to be H8/300.", &setmemorylist); | |
815 | ||
816 | add_cmd ("h8300h", class_support, h8300h_command, | |
817 | "Set machine to be H8/300H.", &setmemorylist); | |
818 | ||
819 | add_cmd ("h8300s", class_support, h8300s_command, | |
820 | "Set machine to be H8/300S.", &setmemorylist); | |
821 | ||
822 | /* Add a hook to set the machine type when we're loading a file. */ | |
823 | ||
c5aa993b | 824 | specify_exec_file_hook (set_machine_hook); |
c906108c SS |
825 | } |
826 | ||
827 | ||
828 | ||
829 | void | |
7256e1a5 | 830 | h8300_print_register_hook (int regno) |
c906108c | 831 | { |
6e591d68 | 832 | if (regno == CCR_REGNUM) |
c906108c SS |
833 | { |
834 | /* CCR register */ | |
835 | int C, Z, N, V; | |
6e591d68 | 836 | unsigned char b[REGISTER_SIZE]; |
c906108c | 837 | unsigned char l; |
cda5a58a | 838 | frame_register_read (selected_frame, regno, b); |
6e591d68 | 839 | l = b[REGISTER_VIRTUAL_SIZE (CCR_REGNUM) - 1]; |
c906108c SS |
840 | printf_unfiltered ("\t"); |
841 | printf_unfiltered ("I-%d - ", (l & 0x80) != 0); | |
842 | printf_unfiltered ("H-%d - ", (l & 0x20) != 0); | |
843 | N = (l & 0x8) != 0; | |
844 | Z = (l & 0x4) != 0; | |
845 | V = (l & 0x2) != 0; | |
846 | C = (l & 0x1) != 0; | |
847 | printf_unfiltered ("N-%d ", N); | |
848 | printf_unfiltered ("Z-%d ", Z); | |
849 | printf_unfiltered ("V-%d ", V); | |
850 | printf_unfiltered ("C-%d ", C); | |
851 | if ((C | Z) == 0) | |
852 | printf_unfiltered ("u> "); | |
853 | if ((C | Z) == 1) | |
854 | printf_unfiltered ("u<= "); | |
855 | if ((C == 0)) | |
856 | printf_unfiltered ("u>= "); | |
857 | if (C == 1) | |
858 | printf_unfiltered ("u< "); | |
859 | if (Z == 0) | |
860 | printf_unfiltered ("!= "); | |
861 | if (Z == 1) | |
862 | printf_unfiltered ("== "); | |
863 | if ((N ^ V) == 0) | |
864 | printf_unfiltered (">= "); | |
865 | if ((N ^ V) == 1) | |
866 | printf_unfiltered ("< "); | |
867 | if ((Z | (N ^ V)) == 0) | |
868 | printf_unfiltered ("> "); | |
869 | if ((Z | (N ^ V)) == 1) | |
870 | printf_unfiltered ("<= "); | |
871 | } | |
872 | } | |
873 | ||
874 | void | |
fba45db2 | 875 | _initialize_h8300_tdep (void) |
c906108c SS |
876 | { |
877 | tm_print_insn = gdb_print_insn_h8300; | |
878 | } |