Fix building Loongarch BFD with a 32-bit compiler
[binutils-gdb.git] / gdb / vax-tdep.c
bloba42c872feff140b9a3bdd1d737f815820747097b
1 /* Target-dependent code for the VAX.
3 Copyright (C) 1986-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "arch-utils.h"
21 #include "dis-asm.h"
22 #include "extract-store-integer.h"
23 #include "frame.h"
24 #include "frame-base.h"
25 #include "frame-unwind.h"
26 #include "gdbcore.h"
27 #include "gdbtypes.h"
28 #include "osabi.h"
29 #include "regcache.h"
30 #include "regset.h"
31 #include "trad-frame.h"
32 #include "value.h"
34 #include "vax-tdep.h"
36 /* Return the name of register REGNUM. */
38 static const char *
39 vax_register_name (struct gdbarch *gdbarch, int regnum)
41 static const char *register_names[] =
43 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
44 "r8", "r9", "r10", "r11", "ap", "fp", "sp", "pc",
45 "ps",
48 static_assert (VAX_NUM_REGS == ARRAY_SIZE (register_names));
49 return register_names[regnum];
52 /* Return the GDB type object for the "standard" data type of data in
53 register REGNUM. */
55 static struct type *
56 vax_register_type (struct gdbarch *gdbarch, int regnum)
58 return builtin_type (gdbarch)->builtin_int;
61 /* Core file support. */
63 /* Supply register REGNUM from the buffer specified by GREGS and LEN
64 in the general-purpose register set REGSET to register cache
65 REGCACHE. If REGNUM is -1, do this for all registers in REGSET. */
67 static void
68 vax_supply_gregset (const struct regset *regset, struct regcache *regcache,
69 int regnum, const void *gregs, size_t len)
71 const gdb_byte *regs = (const gdb_byte *) gregs;
72 int i;
74 for (i = 0; i < VAX_NUM_REGS; i++)
76 if (regnum == i || regnum == -1)
77 regcache->raw_supply (i, regs + i * 4);
81 /* VAX register set. */
83 static const struct regset vax_gregset =
85 NULL,
86 vax_supply_gregset
89 /* Iterate over core file register note sections. */
91 static void
92 vax_iterate_over_regset_sections (struct gdbarch *gdbarch,
93 iterate_over_regset_sections_cb *cb,
94 void *cb_data,
95 const struct regcache *regcache)
97 cb (".reg", VAX_NUM_REGS * 4, VAX_NUM_REGS * 4, &vax_gregset, NULL, cb_data);
100 /* The VAX UNIX calling convention uses R1 to pass a structure return
101 value address instead of passing it as a first (hidden) argument as
102 the VMS calling convention suggests. */
104 static CORE_ADDR
105 vax_store_arguments (struct regcache *regcache, int nargs,
106 struct value **args, CORE_ADDR sp)
108 struct gdbarch *gdbarch = regcache->arch ();
109 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
110 gdb_byte buf[4];
111 int count = 0;
112 int i;
114 /* We create an argument list on the stack, and make the argument
115 pointer to it. */
117 /* Push arguments in reverse order. */
118 for (i = nargs - 1; i >= 0; i--)
120 int len = args[i]->enclosing_type ()->length ();
122 sp -= (len + 3) & ~3;
123 count += (len + 3) / 4;
124 write_memory (sp, args[i]->contents_all ().data (), len);
127 /* Push argument count. */
128 sp -= 4;
129 store_unsigned_integer (buf, 4, byte_order, count);
130 write_memory (sp, buf, 4);
132 /* Update the argument pointer. */
133 store_unsigned_integer (buf, 4, byte_order, sp);
134 regcache->cooked_write (VAX_AP_REGNUM, buf);
136 return sp;
139 static CORE_ADDR
140 vax_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
141 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
142 struct value **args, CORE_ADDR sp,
143 function_call_return_method return_method,
144 CORE_ADDR struct_addr)
146 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
147 CORE_ADDR fp = sp;
148 gdb_byte buf[4];
150 /* Set up the function arguments. */
151 sp = vax_store_arguments (regcache, nargs, args, sp);
153 /* Store return value address. */
154 if (return_method == return_method_struct)
155 regcache_cooked_write_unsigned (regcache, VAX_R1_REGNUM, struct_addr);
157 /* Store return address in the PC slot. */
158 sp -= 4;
159 store_unsigned_integer (buf, 4, byte_order, bp_addr);
160 write_memory (sp, buf, 4);
162 /* Store the (fake) frame pointer in the FP slot. */
163 sp -= 4;
164 store_unsigned_integer (buf, 4, byte_order, fp);
165 write_memory (sp, buf, 4);
167 /* Skip the AP slot. */
168 sp -= 4;
170 /* Store register save mask and control bits. */
171 sp -= 4;
172 store_unsigned_integer (buf, 4, byte_order, 0);
173 write_memory (sp, buf, 4);
175 /* Store condition handler. */
176 sp -= 4;
177 store_unsigned_integer (buf, 4, byte_order, 0);
178 write_memory (sp, buf, 4);
180 /* Update the stack pointer and frame pointer. */
181 store_unsigned_integer (buf, 4, byte_order, sp);
182 regcache->cooked_write (VAX_SP_REGNUM, buf);
183 regcache->cooked_write (VAX_FP_REGNUM, buf);
185 /* Return the saved (fake) frame pointer. */
186 return fp;
189 static struct frame_id
190 vax_dummy_id (struct gdbarch *gdbarch, const frame_info_ptr &this_frame)
192 CORE_ADDR fp;
194 fp = get_frame_register_unsigned (this_frame, VAX_FP_REGNUM);
195 return frame_id_build (fp, get_frame_pc (this_frame));
199 static enum return_value_convention
200 vax_return_value (struct gdbarch *gdbarch, struct value *function,
201 struct type *type, struct regcache *regcache,
202 gdb_byte *readbuf, const gdb_byte *writebuf)
204 int len = type->length ();
205 gdb_byte buf[8];
207 if (type->code () == TYPE_CODE_STRUCT
208 || type->code () == TYPE_CODE_UNION
209 || type->code () == TYPE_CODE_ARRAY)
211 /* The default on VAX is to return structures in static memory.
212 Consequently a function must return the address where we can
213 find the return value. */
215 if (readbuf)
217 ULONGEST addr;
219 regcache_raw_read_unsigned (regcache, VAX_R0_REGNUM, &addr);
220 read_memory (addr, readbuf, len);
223 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
226 if (readbuf)
228 /* Read the contents of R0 and (if necessary) R1. */
229 regcache->cooked_read (VAX_R0_REGNUM, buf);
230 if (len > 4)
231 regcache->cooked_read (VAX_R1_REGNUM, buf + 4);
232 memcpy (readbuf, buf, len);
234 if (writebuf)
236 /* Read the contents to R0 and (if necessary) R1. */
237 memcpy (buf, writebuf, len);
238 regcache->cooked_write (VAX_R0_REGNUM, buf);
239 if (len > 4)
240 regcache->cooked_write (VAX_R1_REGNUM, buf + 4);
243 return RETURN_VALUE_REGISTER_CONVENTION;
247 /* Use the program counter to determine the contents and size of a
248 breakpoint instruction. Return a pointer to a string of bytes that
249 encode a breakpoint instruction, store the length of the string in
250 *LEN and optionally adjust *PC to point to the correct memory
251 location for inserting the breakpoint. */
253 constexpr gdb_byte vax_break_insn[] = { 3 };
255 typedef BP_MANIPULATION (vax_break_insn) vax_breakpoint;
257 /* Advance PC across any function entry prologue instructions
258 to reach some "real" code. */
260 static CORE_ADDR
261 vax_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
263 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
264 gdb_byte op = read_memory_unsigned_integer (pc, 1, byte_order);
266 if (op == 0x11)
267 pc += 2; /* skip brb */
268 if (op == 0x31)
269 pc += 3; /* skip brw */
270 if (op == 0xC2
271 && read_memory_unsigned_integer (pc + 2, 1, byte_order) == 0x5E)
272 pc += 3; /* skip subl2 */
273 if (op == 0x9E
274 && read_memory_unsigned_integer (pc + 1, 1, byte_order) == 0xAE
275 && read_memory_unsigned_integer (pc + 3, 1, byte_order) == 0x5E)
276 pc += 4; /* skip movab */
277 if (op == 0x9E
278 && read_memory_unsigned_integer (pc + 1, 1, byte_order) == 0xCE
279 && read_memory_unsigned_integer (pc + 4, 1, byte_order) == 0x5E)
280 pc += 5; /* skip movab */
281 if (op == 0x9E
282 && read_memory_unsigned_integer (pc + 1, 1, byte_order) == 0xEE
283 && read_memory_unsigned_integer (pc + 6, 1, byte_order) == 0x5E)
284 pc += 7; /* skip movab */
286 return pc;
290 /* Unwinding the stack is relatively easy since the VAX has a
291 dedicated frame pointer, and frames are set up automatically as the
292 result of a function call. Most of the relevant information can be
293 inferred from the documentation of the Procedure Call Instructions
294 in the VAX MACRO and Instruction Set Reference Manual. */
296 struct vax_frame_cache
298 /* Base address. */
299 CORE_ADDR base;
301 /* Table of saved registers. */
302 trad_frame_saved_reg *saved_regs;
305 static struct vax_frame_cache *
306 vax_frame_cache (const frame_info_ptr &this_frame, void **this_cache)
308 struct vax_frame_cache *cache;
309 CORE_ADDR addr;
310 ULONGEST mask;
311 int regnum;
313 if (*this_cache)
314 return (struct vax_frame_cache *) *this_cache;
316 /* Allocate a new cache. */
317 cache = FRAME_OBSTACK_ZALLOC (struct vax_frame_cache);
318 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
320 /* The frame pointer is used as the base for the frame. */
321 cache->base = get_frame_register_unsigned (this_frame, VAX_FP_REGNUM);
322 if (cache->base == 0)
323 return cache;
325 /* The register save mask and control bits determine the layout of
326 the stack frame. */
327 mask = get_frame_memory_unsigned (this_frame, cache->base + 4, 4) >> 16;
329 /* These are always saved. */
330 cache->saved_regs[VAX_PC_REGNUM].set_addr (cache->base + 16);
331 cache->saved_regs[VAX_FP_REGNUM].set_addr (cache->base + 12);
332 cache->saved_regs[VAX_AP_REGNUM].set_addr (cache->base + 8);
333 cache->saved_regs[VAX_PS_REGNUM].set_addr (cache->base + 4);
335 /* Scan the register save mask and record the location of the saved
336 registers. */
337 addr = cache->base + 20;
338 for (regnum = 0; regnum < VAX_AP_REGNUM; regnum++)
340 if (mask & (1 << regnum))
342 cache->saved_regs[regnum].set_addr (addr);
343 addr += 4;
347 /* The CALLS/CALLG flag determines whether this frame has a General
348 Argument List or a Stack Argument List. */
349 if (mask & (1 << 13))
351 ULONGEST numarg;
353 /* This is a procedure with Stack Argument List. Adjust the
354 stack address for the arguments that were pushed onto the
355 stack. The return instruction will automatically pop the
356 arguments from the stack. */
357 numarg = get_frame_memory_unsigned (this_frame, addr, 1);
358 addr += 4 + numarg * 4;
361 /* Bits 1:0 of the stack pointer were saved in the control bits. */
362 cache->saved_regs[VAX_SP_REGNUM].set_value (addr + (mask >> 14));
364 return cache;
367 static void
368 vax_frame_this_id (const frame_info_ptr &this_frame, void **this_cache,
369 struct frame_id *this_id)
371 struct vax_frame_cache *cache = vax_frame_cache (this_frame, this_cache);
373 /* This marks the outermost frame. */
374 if (cache->base == 0)
375 return;
377 (*this_id) = frame_id_build (cache->base, get_frame_func (this_frame));
380 static struct value *
381 vax_frame_prev_register (const frame_info_ptr &this_frame,
382 void **this_cache, int regnum)
384 struct vax_frame_cache *cache = vax_frame_cache (this_frame, this_cache);
386 return trad_frame_get_prev_register (this_frame, cache->saved_regs, regnum);
389 static const struct frame_unwind vax_frame_unwind =
391 "vax prologue",
392 NORMAL_FRAME,
393 default_frame_unwind_stop_reason,
394 vax_frame_this_id,
395 vax_frame_prev_register,
396 NULL,
397 default_frame_sniffer
401 static CORE_ADDR
402 vax_frame_base_address (const frame_info_ptr &this_frame, void **this_cache)
404 struct vax_frame_cache *cache = vax_frame_cache (this_frame, this_cache);
406 return cache->base;
409 static CORE_ADDR
410 vax_frame_args_address (const frame_info_ptr &this_frame, void **this_cache)
412 return get_frame_register_unsigned (this_frame, VAX_AP_REGNUM);
415 static const struct frame_base vax_frame_base =
417 &vax_frame_unwind,
418 vax_frame_base_address,
419 vax_frame_base_address,
420 vax_frame_args_address
423 /* Return number of arguments for FRAME. */
425 static int
426 vax_frame_num_args (const frame_info_ptr &frame)
428 CORE_ADDR args;
430 /* Assume that the argument pointer for the outermost frame is
431 hosed, as is the case on NetBSD/vax ELF. */
432 if (get_frame_base_address (frame) == 0)
433 return 0;
435 args = get_frame_register_unsigned (frame, VAX_AP_REGNUM);
436 return get_frame_memory_unsigned (frame, args, 1);
441 /* Initialize the current architecture based on INFO. If possible, re-use an
442 architecture from ARCHES, which is a list of architectures already created
443 during this debugging session.
445 Called e.g. at program startup, when reading a core file, and when reading
446 a binary file. */
448 static struct gdbarch *
449 vax_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
451 struct gdbarch *gdbarch;
453 /* If there is already a candidate, use it. */
454 arches = gdbarch_list_lookup_by_info (arches, &info);
455 if (arches != NULL)
456 return arches->gdbarch;
458 gdbarch = gdbarch_alloc (&info, NULL);
460 set_gdbarch_float_format (gdbarch, floatformats_vax_f);
461 set_gdbarch_double_format (gdbarch, floatformats_vax_d);
462 set_gdbarch_long_double_format (gdbarch, floatformats_vax_d);
463 set_gdbarch_long_double_bit (gdbarch, 64);
465 /* Register info */
466 set_gdbarch_num_regs (gdbarch, VAX_NUM_REGS);
467 set_gdbarch_register_name (gdbarch, vax_register_name);
468 set_gdbarch_register_type (gdbarch, vax_register_type);
469 set_gdbarch_sp_regnum (gdbarch, VAX_SP_REGNUM);
470 set_gdbarch_pc_regnum (gdbarch, VAX_PC_REGNUM);
471 set_gdbarch_ps_regnum (gdbarch, VAX_PS_REGNUM);
473 set_gdbarch_iterate_over_regset_sections
474 (gdbarch, vax_iterate_over_regset_sections);
476 /* Frame and stack info */
477 set_gdbarch_skip_prologue (gdbarch, vax_skip_prologue);
478 set_gdbarch_frame_num_args (gdbarch, vax_frame_num_args);
479 set_gdbarch_frame_args_skip (gdbarch, 4);
481 /* Stack grows downward. */
482 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
484 /* Return value info */
485 set_gdbarch_return_value (gdbarch, vax_return_value);
487 /* Call dummy code. */
488 set_gdbarch_push_dummy_call (gdbarch, vax_push_dummy_call);
489 set_gdbarch_dummy_id (gdbarch, vax_dummy_id);
491 /* Breakpoint info */
492 set_gdbarch_breakpoint_kind_from_pc (gdbarch, vax_breakpoint::kind_from_pc);
493 set_gdbarch_sw_breakpoint_from_kind (gdbarch, vax_breakpoint::bp_from_kind);
495 /* Misc info */
496 set_gdbarch_deprecated_function_start_offset (gdbarch, 2);
497 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
499 frame_base_set_default (gdbarch, &vax_frame_base);
501 /* Hook in ABI-specific overrides, if they have been registered. */
502 gdbarch_init_osabi (info, gdbarch);
504 frame_unwind_append_unwinder (gdbarch, &vax_frame_unwind);
506 return (gdbarch);
509 void _initialize_vax_tdep ();
510 void
511 _initialize_vax_tdep ()
513 gdbarch_register (bfd_arch_vax, vax_gdbarch_init, NULL);