* config/m88k/tm-m88k.h: Fix typo in comment.
[binutils-gdb.git] / gdb / m88k-tdep.c
1 /* Target-machine dependent code for Motorola 88000 series, for GDB.
2 Copyright (C) 1988, 1990, 1991 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "value.h"
24
25 #ifdef USG
26 #include <sys/types.h>
27 #endif
28
29 #include <sys/param.h>
30 #include <sys/dir.h>
31 #include <signal.h>
32 #include "gdbcore.h"
33 #include <sys/user.h>
34 #ifndef USER /* added to support BCS ptrace_user */
35
36 #define USER ptrace_user
37 #endif
38 #include <sys/ioctl.h>
39 #include <fcntl.h>
40
41 #include <sys/file.h>
42 #include <sys/stat.h>
43
44 #include "symtab.h"
45 #include "setjmp.h"
46 #include "value.h"
47
48 /* Size of an instruction */
49 #define BYTES_PER_88K_INSN 4
50
51 void frame_find_saved_regs ();
52
53
54 /* Given a GDB frame, determine the address of the calling function's frame.
55 This will be used to create a new GDB frame struct, and then
56 INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
57
58 For us, the frame address is its stack pointer value, so we look up
59 the function prologue to determine the caller's sp value, and return it. */
60
61 FRAME_ADDR
62 frame_chain (thisframe)
63 FRAME thisframe;
64 {
65
66 frame_find_saved_regs (thisframe, (struct frame_saved_regs *) 0);
67 /* NOTE: this depends on frame_find_saved_regs returning the VALUE, not
68 the ADDRESS, of SP_REGNUM. It also depends on the cache of
69 frame_find_saved_regs results. */
70 if (thisframe->fsr->regs[SP_REGNUM])
71 return thisframe->fsr->regs[SP_REGNUM];
72 else
73 return thisframe->frame; /* Leaf fn -- next frame up has same SP. */
74 }
75
76 int
77 frameless_function_invocation (frame)
78 FRAME frame;
79 {
80
81 frame_find_saved_regs (frame, (struct frame_saved_regs *) 0);
82 /* NOTE: this depends on frame_find_saved_regs returning the VALUE, not
83 the ADDRESS, of SP_REGNUM. It also depends on the cache of
84 frame_find_saved_regs results. */
85 if (frame->fsr->regs[SP_REGNUM])
86 return 0; /* Frameful -- return addr saved somewhere */
87 else
88 return 1; /* Frameless -- no saved return address */
89 }
90
91 void
92 init_extra_frame_info (fromleaf, fi)
93 int fromleaf;
94 struct frame_info *fi;
95 {
96 fi->fsr = 0; /* Not yet allocated */
97 fi->args_pointer = 0; /* Unknown */
98 fi->locals_pointer = 0; /* Unknown */
99 }
100 \f
101 /* Examine an m88k function prologue, recording the addresses at which
102 registers are saved explicitly by the prologue code, and returning
103 the address of the first instruction after the prologue (but not
104 after the instruction at address LIMIT, as explained below).
105
106 LIMIT places an upper bound on addresses of the instructions to be
107 examined. If the prologue code scan reaches LIMIT, the scan is
108 aborted and LIMIT is returned. This is used, when examining the
109 prologue for the current frame, to keep examine_prologue () from
110 claiming that a given register has been saved when in fact the
111 instruction that saves it has not yet been executed. LIMIT is used
112 at other times to stop the scan when we hit code after the true
113 function prologue (e.g. for the first source line) which might
114 otherwise be mistaken for function prologue.
115
116 The format of the function prologue matched by this routine is
117 derived from examination of the source to gcc 1.95, particularly
118 the routine output_prologue () in config/out-m88k.c.
119
120 subu r31,r31,n # stack pointer update
121
122 (st rn,r31,offset)? # save incoming regs
123 (st.d rn,r31,offset)?
124
125 (addu r30,r31,n)? # frame pointer update
126
127 (pic sequence)? # PIC code prologue
128
129 (or rn,rm,0)? # Move parameters to other regs
130 */
131
132 /* Macros for extracting fields from instructions. */
133
134 #define BITMASK(pos, width) (((0x1 << (width)) - 1) << (pos))
135 #define EXTRACT_FIELD(val, pos, width) ((val) >> (pos) & BITMASK (0, width))
136
137 /* Prologue code that handles position-independent-code setup. */
138
139 struct pic_prologue_code {
140 unsigned long insn, mask;
141 };
142
143 static struct pic_prologue_code pic_prologue_code [] = {
144 /* FIXME -- until this is translated to hex, we won't match it... */
145 0xffffffff, 0,
146 /* or r10,r1,0 (if not saved) */
147 /* bsr.n LabN */
148 /* or.u r25,r0,const */
149 /*LabN: or r25,r25,const2 */
150 /* addu r25,r25,1 */
151 /* or r1,r10,0 (if not saved) */
152 };
153
154 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
155 is not the address of a valid instruction, the address of the next
156 instruction beyond ADDR otherwise. *PWORD1 receives the first word
157 of the instruction. PWORD2 is ignored -- a remnant of the original
158 i960 version. */
159
160 #define NEXT_PROLOGUE_INSN(addr, lim, pword1) \
161 (((addr) < (lim)) ? next_insn (addr, pword1) : 0)
162
163 /* Read the m88k instruction at 'memaddr' and return the address of
164 the next instruction after that, or 0 if 'memaddr' is not the
165 address of a valid instruction. The instruction
166 is stored at 'pword1'. */
167
168 CORE_ADDR
169 next_insn (memaddr, pword1)
170 unsigned long *pword1;
171 CORE_ADDR memaddr;
172 {
173 unsigned long buf[1];
174
175 *pword1 = read_memory_integer (memaddr, BYTES_PER_88K_INSN);
176 return memaddr + BYTES_PER_88K_INSN;
177 }
178
179 /* Read a register from frames called by us (or from the hardware regs). */
180
181 static int
182 read_next_frame_reg(fi, regno)
183 FRAME fi;
184 int regno;
185 {
186 for (; fi; fi = fi->next) {
187 if (regno == SP_REGNUM) return fi->frame;
188 else if (fi->fsr->regs[regno])
189 return read_memory_integer(fi->fsr->regs[regno], 4);
190 }
191 return read_register(regno);
192 }
193
194 /* Examine the prologue of a function. `ip' points to the first instruction.
195 `limit' is the limit of the prologue (e.g. the addr of the first
196 linenumber, or perhaps the program counter if we're stepping through).
197 `frame_sp' is the stack pointer value in use in this frame.
198 `fsr' is a pointer to a frame_saved_regs structure into which we put
199 info about the registers saved by this frame.
200 `fi' is a struct frame_info pointer; we fill in various fields in it
201 to reflect the offsets of the arg pointer and the locals pointer. */
202
203 static CORE_ADDR
204 examine_prologue (ip, limit, frame_sp, fsr, fi)
205 register CORE_ADDR ip;
206 register CORE_ADDR limit;
207 FRAME_ADDR frame_sp;
208 struct frame_saved_regs *fsr;
209 struct frame_info *fi;
210 {
211 register CORE_ADDR next_ip;
212 register int src;
213 register struct pic_prologue_code *pcode;
214 unsigned int insn;
215 int size, offset;
216 char must_adjust[32]; /* If set, must adjust offsets in fsr */
217 int sp_offset = -1; /* -1 means not set (valid must be mult of 8) */
218 int fp_offset = -1; /* -1 means not set */
219 CORE_ADDR frame_fp;
220
221 bzero (must_adjust, sizeof (must_adjust));
222 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn);
223
224 /* Accept move of incoming registers to other registers, using
225 "or rd,rs,0" or "or.u rd,rs,0" or "or rd,r0,rs" or "or rd,rs,r0".
226 We don't have to worry about walking into the first lines of code,
227 since the first line number will stop us (assuming we have symbols).
228 What we have actually seen is "or r10,r0,r12". */
229
230 #define OR_MOVE_INSN 0x58000000 /* or/or.u with immed of 0 */
231 #define OR_MOVE_MASK 0xF800FFFF
232 #define OR_REG_MOVE1_INSN 0xF4005800 /* or rd,r0,rs */
233 #define OR_REG_MOVE1_MASK 0xFC1FFFE0
234 #define OR_REG_MOVE2_INSN 0xF4005800 /* or rd,rs,r0 */
235 #define OR_REG_MOVE2_MASK 0xFC00FFFF
236 while (next_ip &&
237 ((insn & OR_MOVE_MASK) == OR_MOVE_INSN ||
238 (insn & OR_REG_MOVE1_MASK) == OR_REG_MOVE1_INSN ||
239 (insn & OR_REG_MOVE2_MASK) == OR_REG_MOVE2_INSN
240 )
241 )
242 {
243 /* We don't care what moves to where. The result of the moves
244 has already been reflected in what the compiler tells us is the
245 location of these parameters. */
246 ip = next_ip;
247 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn);
248 }
249
250 /* Accept an optional "subu sp,sp,n" to set up the stack pointer. */
251
252 #define SUBU_SP_INSN 0x67ff0000
253 #define SUBU_SP_MASK 0xffff0007 /* Note offset must be mult. of 8 */
254 #define SUBU_OFFSET(x) ((unsigned)(x & 0xFFFF))
255 if (next_ip &&
256 ((insn & SUBU_SP_MASK) == SUBU_SP_INSN)) /* subu r31, r31, N */
257 {
258 sp_offset = -SUBU_OFFSET (insn);
259 ip = next_ip;
260 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn);
261 }
262
263 /* The function must start with a stack-pointer adjustment, or
264 we don't know WHAT'S going on... */
265 if (sp_offset == -1)
266 return ip;
267
268 /* Accept zero or more instances of "st rx,sp,n" or "st.d rx,sp,n".
269 This may cause us to mistake the copying of a register
270 parameter to the frame for the saving of a callee-saved
271 register, but that can't be helped, since with the
272 "-fcall-saved" flag, any register can be made callee-saved.
273 This probably doesn't matter, since the ``saved'' caller's values of
274 non-callee-saved registers are not relevant anyway. */
275
276 #define STD_STACK_INSN 0x201f0000
277 #define STD_STACK_MASK 0xfc1f0000
278 #define ST_STACK_INSN 0x241f0000
279 #define ST_STACK_MASK 0xfc1f0000
280 #define ST_OFFSET(x) ((unsigned)((x) & 0xFFFF))
281 #define ST_SRC(x) EXTRACT_FIELD ((x), 21, 5)
282
283 while (next_ip)
284 {
285 if ((insn & ST_STACK_MASK) == ST_STACK_INSN)
286 size = 1;
287 else if ((insn & STD_STACK_MASK) == STD_STACK_INSN)
288 size = 2;
289 else
290 break;
291
292 src = ST_SRC (insn);
293 offset = ST_OFFSET (insn);
294 while (size--)
295 {
296 must_adjust[src] = 1;
297 fsr->regs[src++] = offset; /* Will be adjusted later */
298 offset += 4;
299 }
300 ip = next_ip;
301 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn);
302 }
303
304 /* Accept an optional "addu r30,r31,n" to set up the frame pointer. */
305
306 #define ADDU_FP_INSN 0x63df0000
307 #define ADDU_FP_MASK 0xffff0000
308 #define ADDU_OFFSET(x) ((unsigned)(x & 0xFFFF))
309 if (next_ip &&
310 ((insn & ADDU_FP_MASK) == ADDU_FP_INSN)) /* addu r30, r31, N */
311 {
312 fp_offset = ADDU_OFFSET (insn);
313 ip = next_ip;
314 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn);
315 }
316
317 /* Accept the PIC prologue code if present. */
318
319 pcode = pic_prologue_code;
320 size = sizeof (pic_prologue_code) / sizeof (*pic_prologue_code);
321 /* If return addr is saved, we don't use first or last insn of PICstuff. */
322 if (fsr->regs[SRP_REGNUM]) {
323 pcode++;
324 size-=2;
325 }
326
327 while (size-- && next_ip && (pcode->insn == (pcode->mask & insn)))
328 {
329 pcode++;
330 ip = next_ip;
331 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn);
332 }
333
334 /* Accept moves of parameter registers to other registers, using
335 "or rd,rs,0" or "or.u rd,rs,0" or "or rd,r0,rs" or "or rd,rs,r0".
336 We don't have to worry about walking into the first lines of code,
337 since the first line number will stop us (assuming we have symbols).
338 What gcc actually seems to produce is "or rd,r0,rs". */
339
340 #define OR_MOVE_INSN 0x58000000 /* or/or.u with immed of 0 */
341 #define OR_MOVE_MASK 0xF800FFFF
342 #define OR_REG_MOVE1_INSN 0xF4005800 /* or rd,r0,rs */
343 #define OR_REG_MOVE1_MASK 0xFC1FFFE0
344 #define OR_REG_MOVE2_INSN 0xF4005800 /* or rd,rs,r0 */
345 #define OR_REG_MOVE2_MASK 0xFC00FFFF
346 while (next_ip &&
347 ((insn & OR_MOVE_MASK) == OR_MOVE_INSN ||
348 (insn & OR_REG_MOVE1_MASK) == OR_REG_MOVE1_INSN ||
349 (insn & OR_REG_MOVE2_MASK) == OR_REG_MOVE2_INSN
350 )
351 )
352 {
353 /* We don't care what moves to where. The result of the moves
354 has already been reflected in what the compiler tells us is the
355 location of these parameters. */
356 ip = next_ip;
357 next_ip = NEXT_PROLOGUE_INSN (ip, limit, &insn);
358 }
359
360 /* We're done with the prologue. If we don't care about the stack
361 frame itself, just return. (Note that fsr->regs has been trashed,
362 but the one caller who calls with fi==0 passes a dummy there.) */
363
364 if (fi == 0)
365 return ip;
366
367 /*
368 OK, now we have:
369
370 sp_offset original (before any alloca calls) displacement of SP
371 (will be negative).
372
373 fp_offset displacement from original SP to the FP for this frame
374 or -1.
375
376 fsr->regs[0..31] displacement from original SP to the stack
377 location where reg[0..31] is stored.
378
379 must_adjust[0..31] set if corresponding offset was set.
380
381 If alloca has been called between the function prologue and the current
382 IP, then the current SP (frame_sp) will not be the original SP as set by
383 the function prologue. If the current SP is not the original SP, then the
384 compiler will have allocated an FP for this frame, fp_offset will be set,
385 and we can use it to calculate the original SP.
386
387 Then, we figure out where the arguments and locals are, and relocate the
388 offsets in fsr->regs to absolute addresses. */
389
390 if (fp_offset != -1) {
391 /* We have a frame pointer, so get it, and base our calc's on it. */
392 frame_fp = (CORE_ADDR) read_next_frame_reg (fi->next, ACTUAL_FP_REGNUM);
393 frame_sp = frame_fp - fp_offset;
394 } else {
395 /* We have no frame pointer, therefore frame_sp is still the same value
396 as set by prologue. But where is the frame itself? */
397 if (must_adjust[SRP_REGNUM]) {
398 /* Function header saved SRP (r1), the return address. Frame starts
399 4 bytes down from where it was saved. */
400 frame_fp = frame_sp + fsr->regs[SRP_REGNUM] - 4;
401 fi->locals_pointer = frame_fp;
402 } else {
403 /* Function header didn't save SRP (r1), so we are in a leaf fn or
404 are otherwise confused. */
405 frame_fp = -1;
406 }
407 }
408
409 /* The locals are relative to the FP (whether it exists as an allocated
410 register, or just as an assumed offset from the SP) */
411 fi->locals_pointer = frame_fp;
412
413 /* The arguments are just above the SP as it was before we adjusted it
414 on entry. */
415 fi->args_pointer = frame_sp - sp_offset;
416
417 /* Now that we know the SP value used by the prologue, we know where
418 it saved all the registers. */
419 for (src = 0; src < 32; src++)
420 if (must_adjust[src])
421 fsr->regs[src] += frame_sp;
422
423 /* The saved value of the SP is always known. */
424 /* (we hope...) */
425 if (fsr->regs[SP_REGNUM] != 0
426 && fsr->regs[SP_REGNUM] != frame_sp - sp_offset)
427 fprintf(stderr, "Bad saved SP value %x != %x, offset %x!\n",
428 fsr->regs[SP_REGNUM],
429 frame_sp - sp_offset, sp_offset);
430
431 fsr->regs[SP_REGNUM] = frame_sp - sp_offset;
432
433 return (ip);
434 }
435
436 /* Given an ip value corresponding to the start of a function,
437 return the ip of the first instruction after the function
438 prologue. */
439
440 CORE_ADDR
441 skip_prologue (ip)
442 CORE_ADDR (ip);
443 {
444 struct frame_saved_regs saved_regs_dummy;
445 struct symtab_and_line sal;
446 CORE_ADDR limit;
447
448 sal = find_pc_line (ip, 0);
449 limit = (sal.end) ? sal.end : 0xffffffff;
450
451 return (examine_prologue (ip, limit, (FRAME_ADDR) 0, &saved_regs_dummy,
452 (struct frame_info *)0 ));
453 }
454
455 /* Put here the code to store, into a struct frame_saved_regs,
456 the addresses of the saved registers of frame described by FRAME_INFO.
457 This includes special registers such as pc and fp saved in special
458 ways in the stack frame. sp is even more special:
459 the address we return for it IS the sp for the next frame.
460
461 We cache the result of doing this in the frame_cache_obstack, since
462 it is fairly expensive. */
463
464 void
465 frame_find_saved_regs (fi, fsr)
466 struct frame_info *fi;
467 struct frame_saved_regs *fsr;
468 {
469 register CORE_ADDR next_addr;
470 register CORE_ADDR *saved_regs;
471 register int regnum;
472 register struct frame_saved_regs *cache_fsr;
473 extern struct obstack frame_cache_obstack;
474 CORE_ADDR ip;
475 struct symtab_and_line sal;
476 CORE_ADDR limit;
477
478 if (!fi->fsr)
479 {
480 cache_fsr = (struct frame_saved_regs *)
481 obstack_alloc (&frame_cache_obstack,
482 sizeof (struct frame_saved_regs));
483 bzero (cache_fsr, sizeof (struct frame_saved_regs));
484 fi->fsr = cache_fsr;
485
486 /* Find the start and end of the function prologue. If the PC
487 is in the function prologue, we only consider the part that
488 has executed already. */
489
490 ip = get_pc_function_start (fi->pc);
491 sal = find_pc_line (ip, 0);
492 limit = (sal.end && sal.end < fi->pc) ? sal.end: fi->pc;
493
494 /* This will fill in fields in *fi as well as in cache_fsr. */
495 examine_prologue (ip, limit, fi->frame, cache_fsr, fi);
496 }
497
498 if (fsr)
499 *fsr = *fi->fsr;
500 }
501
502 /* Return the address of the locals block for the frame
503 described by FI. Returns 0 if the address is unknown.
504 NOTE! Frame locals are referred to by negative offsets from the
505 argument pointer, so this is the same as frame_args_address(). */
506
507 CORE_ADDR
508 frame_locals_address (fi)
509 struct frame_info *fi;
510 {
511 register FRAME frame;
512 struct frame_saved_regs fsr;
513 CORE_ADDR ap;
514
515 if (fi->args_pointer) /* Cached value is likely there. */
516 return fi->args_pointer;
517
518 /* Nope, generate it. */
519
520 get_frame_saved_regs (fi, &fsr);
521
522 return fi->args_pointer;
523 }
524
525 /* Return the address of the argument block for the frame
526 described by FI. Returns 0 if the address is unknown. */
527
528 CORE_ADDR
529 frame_args_address (fi)
530 struct frame_info *fi;
531 {
532 register FRAME frame;
533 struct frame_saved_regs fsr;
534 CORE_ADDR ap;
535
536 if (fi->args_pointer) /* Cached value is likely there. */
537 return fi->args_pointer;
538
539 /* Nope, generate it. */
540
541 get_frame_saved_regs (fi, &fsr);
542
543 return fi->args_pointer;
544 }
545
546 /* Return the saved PC from this frame.
547
548 If the frame has a memory copy of SRP_REGNUM, use that. If not,
549 just use the register SRP_REGNUM itself. */
550
551 CORE_ADDR
552 frame_saved_pc (frame)
553 FRAME frame;
554 {
555 return read_next_frame_reg(frame, SRP_REGNUM);
556 }
557
558
559 static int
560 pushed_size (prev_words, v)
561 int prev_words;
562 struct value *v;
563 {
564 switch (TYPE_CODE (VALUE_TYPE (v)))
565 {
566 case TYPE_CODE_VOID: /* Void type (values zero length) */
567
568 return 0; /* That was easy! */
569
570 case TYPE_CODE_PTR: /* Pointer type */
571 case TYPE_CODE_ENUM: /* Enumeration type */
572 case TYPE_CODE_INT: /* Integer type */
573 case TYPE_CODE_REF: /* C++ Reference types */
574 case TYPE_CODE_ARRAY: /* Array type, lower & upper bounds */
575
576 return 1;
577
578 case TYPE_CODE_FLT: /* Floating type */
579
580 if (TYPE_LENGTH (VALUE_TYPE (v)) == 4)
581 return 1;
582 else
583 /* Assume that it must be a double. */
584 if (prev_words & 1) /* at an odd-word boundary */
585 return 3; /* round to 8-byte boundary */
586 else
587 return 2;
588
589 case TYPE_CODE_STRUCT: /* C struct or Pascal record */
590 case TYPE_CODE_UNION: /* C union or Pascal variant part */
591
592 return (((TYPE_LENGTH (VALUE_TYPE (v)) + 3) / 4) * 4);
593
594 case TYPE_CODE_FUNC: /* Function type */
595 case TYPE_CODE_SET: /* Pascal sets */
596 case TYPE_CODE_RANGE: /* Range (integers within bounds) */
597 case TYPE_CODE_STRING: /* String type */
598 case TYPE_CODE_MEMBER: /* Member type */
599 case TYPE_CODE_METHOD: /* Method type */
600 /* Don't know how to pass these yet. */
601
602 case TYPE_CODE_UNDEF: /* Not used; catches errors */
603 default:
604 abort ();
605 }
606 }
607
608 static void
609 store_parm_word (address, val)
610 CORE_ADDR address;
611 int val;
612 {
613 write_memory (address, (char *)&val, 4);
614 }
615
616 static int
617 store_parm (prev_words, left_parm_addr, v)
618 unsigned int prev_words;
619 CORE_ADDR left_parm_addr;
620 struct value *v;
621 {
622 CORE_ADDR start = left_parm_addr + (prev_words * 4);
623 int *val_addr = (int *)VALUE_CONTENTS(v);
624
625 switch (TYPE_CODE (VALUE_TYPE (v)))
626 {
627 case TYPE_CODE_VOID: /* Void type (values zero length) */
628
629 return 0;
630
631 case TYPE_CODE_PTR: /* Pointer type */
632 case TYPE_CODE_ENUM: /* Enumeration type */
633 case TYPE_CODE_INT: /* Integer type */
634 case TYPE_CODE_ARRAY: /* Array type, lower & upper bounds */
635 case TYPE_CODE_REF: /* C++ Reference types */
636
637 store_parm_word (start, *val_addr);
638 return 1;
639
640 case TYPE_CODE_FLT: /* Floating type */
641
642 if (TYPE_LENGTH (VALUE_TYPE (v)) == 4)
643 {
644 store_parm_word (start, *val_addr);
645 return 1;
646 }
647 else
648 {
649 store_parm_word (start + ((prev_words & 1) * 4), val_addr[0]);
650 store_parm_word (start + ((prev_words & 1) * 4) + 4, val_addr[1]);
651 return 2 + (prev_words & 1);
652 }
653
654 case TYPE_CODE_STRUCT: /* C struct or Pascal record */
655 case TYPE_CODE_UNION: /* C union or Pascal variant part */
656
657 {
658 unsigned int words = (((TYPE_LENGTH (VALUE_TYPE (v)) + 3) / 4) * 4);
659 unsigned int word;
660
661 for (word = 0; word < words; word++)
662 store_parm_word (start + (word * 4), val_addr[word]);
663 return words;
664 }
665
666 default:
667 abort ();
668 }
669 }
670
671 /* This routine sets up all of the parameter values needed to make a pseudo
672 call. The name "push_parameters" is a misnomer on some archs,
673 because (on the m88k) most parameters generally end up being passed in
674 registers rather than on the stack. In this routine however, we do
675 end up storing *all* parameter values onto the stack (even if we will
676 realize later that some of these stores were unnecessary). */
677
678 #define FIRST_PARM_REGNUM 2
679
680 void
681 push_parameters (return_type, struct_conv, nargs, args)
682 struct type *return_type;
683 int struct_conv;
684 int nargs;
685 value *args;
686 {
687 int parm_num;
688 unsigned int p_words = 0;
689 CORE_ADDR left_parm_addr;
690
691 /* Start out by creating a space for the return value (if need be). We
692 only need to do this if the return value is a struct or union. If we
693 do make a space for a struct or union return value, then we must also
694 arrange for the base address of that space to go into r12, which is the
695 standard place to pass the address of the return value area to the
696 callee. Note that only structs and unions are returned in this fashion.
697 Ints, enums, pointers, and floats are returned into r2. Doubles are
698 returned into the register pair {r2,r3}. Note also that the space
699 reserved for a struct or union return value only has to be word aligned
700 (not double-word) but it is double-word aligned here anyway (just in
701 case that becomes important someday). */
702
703 switch (TYPE_CODE (return_type))
704 {
705 case TYPE_CODE_STRUCT:
706 case TYPE_CODE_UNION:
707 {
708 int return_bytes = ((TYPE_LENGTH (return_type) + 7) / 8) * 8;
709 CORE_ADDR rv_addr;
710
711 rv_addr = read_register (SP_REGNUM) - return_bytes;
712
713 write_register (SP_REGNUM, rv_addr); /* push space onto the stack */
714 write_register (SRA_REGNUM, rv_addr);/* set return value register */
715 }
716 }
717
718 /* Here we make a pre-pass on the whole parameter list to figure out exactly
719 how many words worth of stuff we are going to pass. */
720
721 for (p_words = 0, parm_num = 0; parm_num < nargs; parm_num++)
722 p_words += pushed_size (p_words, value_arg_coerce (args[parm_num]));
723
724 /* Now, check to see if we have to round up the number of parameter words
725 to get up to the next 8-bytes boundary. This may be necessary because
726 of the software convention to always keep the stack aligned on an 8-byte
727 boundary. */
728
729 if (p_words & 1)
730 p_words++; /* round to 8-byte boundary */
731
732 /* Now figure out the absolute address of the leftmost parameter, and update
733 the stack pointer to point at that address. */
734
735 left_parm_addr = read_register (SP_REGNUM) - (p_words * 4);
736 write_register (SP_REGNUM, left_parm_addr);
737
738 /* Now we can go through all of the parameters (in left-to-right order)
739 and write them to their parameter stack slots. Note that we are not
740 really "pushing" the parameter values. The stack space for these values
741 was already allocated above. Now we are just filling it up. */
742
743 for (p_words = 0, parm_num = 0; parm_num < nargs; parm_num++)
744 p_words +=
745 store_parm (p_words, left_parm_addr, value_arg_coerce (args[parm_num]));
746
747 /* Now that we are all done storing the parameter values into the stack, we
748 must go back and load up the parameter registers with the values from the
749 corresponding stack slots. Note that in the two cases of (a) gaps in the
750 parameter word sequence causes by (otherwise) misaligned doubles, and (b)
751 slots correcponding to structs or unions, the work we do here in loading
752 some parameter registers may be unnecessary, but who cares? */
753
754 for (p_words = 0; p_words < 8; p_words++)
755 {
756 write_register (FIRST_PARM_REGNUM + p_words,
757 read_memory_integer (left_parm_addr + (p_words * 4), 4));
758 }
759 }
760
761 void
762 pop_frame ()
763 {
764 error ("Feature not implemented for the m88k yet.");
765 return;
766 }
767
768 void
769 collect_returned_value (rval, value_type, struct_return, nargs, args)
770 value *rval;
771 struct type *value_type;
772 int struct_return;
773 int nargs;
774 value *args;
775 {
776 char retbuf[REGISTER_BYTES];
777
778 bcopy (registers, retbuf, REGISTER_BYTES);
779 *rval = value_being_returned (value_type, retbuf, struct_return);
780 return;
781 }
782
783 #if 0
784 /* Now handled in a machine independent way with CALL_DUMMY_LOCATION. */
785 /* Stuff a breakpoint instruction onto the stack (or elsewhere if the stack
786 is not a good place for it). Return the address at which the instruction
787 got stuffed, or zero if we were unable to stuff it anywhere. */
788
789 CORE_ADDR
790 push_breakpoint ()
791 {
792 static char breakpoint_insn[] = BREAKPOINT;
793 extern CORE_ADDR text_end; /* of inferior */
794 static char readback_buffer[] = BREAKPOINT;
795 int i;
796
797 /* With a little bit of luck, we can just stash the breakpoint instruction
798 in the word just beyond the end of normal text space. For systems on
799 which the hardware will not allow us to execute out of the stack segment,
800 we have to hope that we *are* at least allowed to effectively extend the
801 text segment by one word. If the actual end of user's the text segment
802 happens to fall right at a page boundary this trick may fail. Note that
803 we check for this by reading after writing, and comparing in order to
804 be sure that the write worked. */
805
806 write_memory (text_end, &breakpoint_insn, 4);
807
808 /* Fill the readback buffer with some garbage which is certain to be
809 unequal to the breakpoint insn. That way we can tell if the
810 following read doesn't actually succeed. */
811
812 for (i = 0; i < sizeof (readback_buffer); i++)
813 readback_buffer[i] = ~ readback_buffer[i]; /* Invert the bits */
814
815 /* Now check that the breakpoint insn was successfully installed. */
816
817 read_memory (text_end, readback_buffer, sizeof (readback_buffer));
818 for (i = 0; i < sizeof (readback_buffer); i++)
819 if (readback_buffer[i] != breakpoint_insn[i])
820 return 0; /* Failed to install! */
821
822 return text_end;
823 }
824 #endif