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