#include "defs.h"
#include "inferior.h"
#include "regcache.h"
#include <sys/types.h>
#include <sys/ptrace.h>
#include <machine/reg.h>
#ifdef HAVE_SYS_PROCFS_H
#include <sys/procfs.h>
#endif
#ifndef HAVE_GREGSET_T
typedef struct reg gregset_t;
#endif
#ifndef HAVE_FPREGSET_T
typedef struct fpreg fpregset_t;
#endif
#include "gregset.h"
#define NUM_GREGS 32
#define NUM_FPREGS 31
void
supply_gregset (gregset_t *gregsetp)
{
int i;
for (i = 0; i < NUM_GREGS; i++)
{
if (CANNOT_FETCH_REGISTER (i))
supply_register (i, NULL);
else
supply_register (i, (char *) &gregsetp->r_regs[i]);
}
supply_register (PC_REGNUM, (char *) &gregsetp->r_regs[R_ZERO]);
}
void
fill_gregset (gregset_t *gregsetp, int regno)
{
int i;
for (i = 0; i < NUM_GREGS; i++)
if ((regno == -1 || regno == i) && ! CANNOT_STORE_REGISTER (i))
regcache_collect (i, (char *) &gregsetp->r_regs[i]);
if (regno == -1 || regno == PC_REGNUM)
regcache_collect (PC_REGNUM, (char *) &gregsetp->r_regs[R_ZERO]);
}
void
supply_fpregset (fpregset_t *fpregsetp)
{
int i;
for (i = FP0_REGNUM; i < FP0_REGNUM + NUM_FPREGS; i++)
{
if (CANNOT_FETCH_REGISTER (i))
supply_register (i, NULL);
else
supply_register (i, (char *) &fpregsetp->fpr_regs[i - FP0_REGNUM]);
}
supply_register (FPCR_REGNUM, (char *) &fpregsetp->fpr_cr);
}
void
fill_fpregset (fpregset_t *fpregsetp, int regno)
{
int i;
for (i = FP0_REGNUM; i < FP0_REGNUM + NUM_FPREGS; i++)
if ((regno == -1 || regno == i) && ! CANNOT_STORE_REGISTER (i))
regcache_collect (i, (char *) &fpregsetp->fpr_regs[i - FP0_REGNUM]);
if (regno == -1 || regno == FPCR_REGNUM)
regcache_collect (FPCR_REGNUM, (char *) &fpregsetp->fpr_cr);
}
static int
getregs_supplies (int regno)
{
return ((regno >= V0_REGNUM && regno <= ZERO_REGNUM)
|| regno >= PC_REGNUM);
}
void
fetch_inferior_registers (int regno)
{
if (regno == -1 || getregs_supplies (regno))
{
gregset_t gregs;
if (ptrace (PT_GETREGS, PIDGET (inferior_ptid),
(PTRACE_ARG3_TYPE) &gregs, 0) == -1)
perror_with_name ("Couldn't get registers");
supply_gregset (&gregs);
if (regno != -1)
return;
}
if (regno == -1 || regno >= FP0_REGNUM)
{
fpregset_t fpregs;
if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid),
(PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
perror_with_name ("Couldn't get floating point status");
supply_fpregset (&fpregs);
}
supply_register (FP_REGNUM, NULL);
}
void
store_inferior_registers (int regno)
{
if (regno == -1 || getregs_supplies (regno))
{
gregset_t gregs;
if (ptrace (PT_GETREGS, PIDGET (inferior_ptid),
(PTRACE_ARG3_TYPE) &gregs, 0) == -1)
perror_with_name ("Couldn't get registers");
fill_gregset (&gregs, regno);
if (ptrace (PT_SETREGS, PIDGET (inferior_ptid),
(PTRACE_ARG3_TYPE) &gregs, 0) == -1)
perror_with_name ("Couldn't write registers");
if (regno != -1)
return;
}
if (regno == -1 || regno >= FP0_REGNUM)
{
fpregset_t fpregs;
if (ptrace (PT_GETFPREGS, PIDGET (inferior_ptid),
(PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
perror_with_name ("Couldn't get floating point status");
fill_fpregset (&fpregs, regno);
if (ptrace (PT_SETFPREGS, PIDGET (inferior_ptid),
(PTRACE_ARG3_TYPE) &fpregs, 0) == -1)
perror_with_name ("Couldn't write floating point status");
}
}