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