Fix -Werror probs for ns32k.
[binutils-gdb.git] / gdb / ns32k-tdep.c
1 /* Print NS 32000 instructions for GDB, the GNU debugger.
2 Copyright 1986, 1988, 1991, 1992, 1994, 1995, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "frame.h"
24 #include "gdbcore.h"
25
26 static int sign_extend (int value, int bits);
27
28 void
29 _initialize_ns32k_tdep (void)
30 {
31 tm_print_insn = print_insn_ns32k;
32 }
33
34 /* Advance PC across any function entry prologue instructions
35 to reach some "real" code. */
36
37 CORE_ADDR
38 merlin_skip_prologue (CORE_ADDR pc)
39 {
40 register int op = read_memory_integer (pc, 1);
41 if (op == 0x82)
42 {
43 op = read_memory_integer (pc + 2, 1);
44 if ((op & 0x80) == 0)
45 pc += 3;
46 else if ((op & 0xc0) == 0x80)
47 pc += 4;
48 else
49 pc += 6;
50 }
51 return pc;
52 }
53
54 CORE_ADDR
55 umax_skip_prologue (CORE_ADDR pc)
56 {
57 register unsigned char op = read_memory_integer (pc, 1);
58 if (op == 0x82)
59 {
60 op = read_memory_integer (pc + 2, 1);
61 if ((op & 0x80) == 0)
62 pc += 3;
63 else if ((op & 0xc0) == 0x80)
64 pc += 4;
65 else
66 pc += 6;
67 }
68 return pc;
69 }
70
71 /* Return number of args passed to a frame.
72 Can return -1, meaning no way to tell. */
73
74 int
75 merlin_frame_num_args (struct frame_info *fi)
76 {
77 int numargs;
78 CORE_ADDR pc;
79 int insn;
80 int addr_mode;
81 int width;
82
83 pc = FRAME_SAVED_PC (fi);
84 insn = read_memory_integer (pc, 2);
85 addr_mode = (insn >> 11) & 0x1f;
86 insn = insn & 0x7ff;
87 if ((insn & 0x7fc) == 0x57c
88 && addr_mode == 0x14) /* immediate */
89 {
90 if (insn == 0x57c) /* adjspb */
91 width = 1;
92 else if (insn == 0x57d) /* adjspw */
93 width = 2;
94 else if (insn == 0x57f) /* adjspd */
95 width = 4;
96 else
97 internal_error (__FILE__, __LINE__, "bad else");
98 numargs = read_memory_integer (pc + 2, width);
99 if (width > 1)
100 flip_bytes (&numargs, width);
101 numargs = -sign_extend (numargs, width * 8) / 4;
102 }
103 else
104 numargs = -1;
105 return numargs;
106 }
107
108
109 /* Return number of args passed to a frame.
110 Can return -1, meaning no way to tell.
111 Encore's C compiler often reuses same area on stack for args,
112 so this will often not work properly. If the arg names
113 are known, it's likely most of them will be printed. */
114 int
115 umax_frame_num_args (struct frame_info *fi)
116 {
117 int numargs;
118 CORE_ADDR pc;
119 CORE_ADDR enter_addr;
120 unsigned int insn;
121 unsigned int addr_mode;
122 int width;
123
124 numargs = -1;
125 enter_addr = ns32k_get_enter_addr ((fi)->pc);
126 if (enter_addr > 0)
127 {
128 pc = ((enter_addr == 1)
129 ? SAVED_PC_AFTER_CALL (fi)
130 : FRAME_SAVED_PC (fi));
131 insn = read_memory_integer (pc, 2);
132 addr_mode = (insn >> 11) & 0x1f;
133 insn = insn & 0x7ff;
134 if ((insn & 0x7fc) == 0x57c
135 && addr_mode == 0x14) /* immediate */
136 {
137 if (insn == 0x57c) /* adjspb */
138 width = 1;
139 else if (insn == 0x57d) /* adjspw */
140 width = 2;
141 else if (insn == 0x57f) /* adjspd */
142 width = 4;
143 else
144 internal_error (__FILE__, __LINE__, "bad else");
145 numargs = read_memory_integer (pc + 2, width);
146 if (width > 1)
147 flip_bytes (&numargs, width);
148 numargs = -sign_extend (numargs, width * 8) / 4;
149 }
150 }
151 return numargs;
152 }
153
154
155 static int
156 sign_extend (int value, int bits)
157 {
158 value = value & ((1 << bits) - 1);
159 return (value & (1 << (bits - 1))
160 ? value | (~((1 << bits) - 1))
161 : value);
162 }
163
164 void
165 flip_bytes (void *p, int count)
166 {
167 char tmp;
168 char *ptr = 0;
169
170 while (count > 0)
171 {
172 tmp = *ptr;
173 ptr[0] = ptr[count - 1];
174 ptr[count - 1] = tmp;
175 ptr++;
176 count -= 2;
177 }
178 }
179
180 /* Return the number of locals in the current frame given a pc
181 pointing to the enter instruction. This is used in the macro
182 FRAME_FIND_SAVED_REGS. */
183
184 int
185 ns32k_localcount (CORE_ADDR enter_pc)
186 {
187 unsigned char localtype;
188 int localcount;
189
190 localtype = read_memory_integer (enter_pc + 2, 1);
191 if ((localtype & 0x80) == 0)
192 localcount = localtype;
193 else if ((localtype & 0xc0) == 0x80)
194 localcount = (((localtype & 0x3f) << 8)
195 | (read_memory_integer (enter_pc + 3, 1) & 0xff));
196 else
197 localcount = (((localtype & 0x3f) << 24)
198 | ((read_memory_integer (enter_pc + 3, 1) & 0xff) << 16)
199 | ((read_memory_integer (enter_pc + 4, 1) & 0xff) << 8)
200 | (read_memory_integer (enter_pc + 5, 1) & 0xff));
201 return localcount;
202 }
203
204
205 /* Nonzero if instruction at PC is a return instruction. */
206
207 static int
208 ns32k_about_to_return (CORE_ADDR pc)
209 {
210 return (read_memory_integer (pc, 1) == 0x12);
211 }
212
213
214 /*
215 * Get the address of the enter opcode for the function
216 * containing PC, if there is an enter for the function,
217 * and if the pc is between the enter and exit.
218 * Returns positive address if pc is between enter/exit,
219 * 1 if pc before enter or after exit, 0 otherwise.
220 */
221
222 CORE_ADDR
223 ns32k_get_enter_addr (CORE_ADDR pc)
224 {
225 CORE_ADDR enter_addr;
226 unsigned char op;
227
228 if (pc == 0)
229 return 0;
230
231 if (ns32k_about_to_return (pc))
232 return 1; /* after exit */
233
234 enter_addr = get_pc_function_start (pc);
235
236 if (pc == enter_addr)
237 return 1; /* before enter */
238
239 op = read_memory_integer (enter_addr, 1);
240
241 if (op != 0x82)
242 return 0; /* function has no enter/exit */
243
244 return enter_addr; /* pc is between enter and exit */
245 }