1130803Smarcel/* Builtin frame register, for GDB, the GNU debugger.
2130803Smarcel
3130803Smarcel   Copyright 2002 Free Software Foundation, Inc.
4130803Smarcel
5130803Smarcel   Contributed by Red Hat.
6130803Smarcel
7130803Smarcel   This file is part of GDB.
8130803Smarcel
9130803Smarcel   This program is free software; you can redistribute it and/or modify
10130803Smarcel   it under the terms of the GNU General Public License as published by
11130803Smarcel   the Free Software Foundation; either version 2 of the License, or
12130803Smarcel   (at your option) any later version.
13130803Smarcel
14130803Smarcel   This program is distributed in the hope that it will be useful,
15130803Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
16130803Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17130803Smarcel   GNU General Public License for more details.
18130803Smarcel
19130803Smarcel   You should have received a copy of the GNU General Public License
20130803Smarcel   along with this program; if not, write to the Free Software
21130803Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
22130803Smarcel   Boston, MA 02111-1307, USA.  */
23130803Smarcel
24130803Smarcel#include "defs.h"
25130803Smarcel#include "user-regs.h"
26130803Smarcel#include "frame.h"
27130803Smarcel#include "gdbtypes.h"
28130803Smarcel#include "value.h"
29130803Smarcel#include "gdb_string.h"
30130803Smarcel
31130803Smarcel/* Types that describe the various builtin registers.  */
32130803Smarcel
33130803Smarcelstatic struct type *builtin_type_frame_reg;
34130803Smarcel
35130803Smarcel/* Constructors for those types.  */
36130803Smarcel
37130803Smarcelstatic void
38130803Smarcelbuild_builtin_type_frame_reg (void)
39130803Smarcel{
40130803Smarcel  /* $frame.  */
41130803Smarcel  if (builtin_type_frame_reg == NULL)
42130803Smarcel    {
43130803Smarcel#if 0
44130803Smarcel      struct frame
45130803Smarcel      {
46130803Smarcel	void *base;
47130803Smarcel      };
48130803Smarcel#endif
49130803Smarcel      builtin_type_frame_reg = init_composite_type ("frame", TYPE_CODE_STRUCT);
50130803Smarcel      append_composite_type_field (builtin_type_frame_reg, "base",
51130803Smarcel				   builtin_type_void_data_ptr);
52130803Smarcel    }
53130803Smarcel}
54130803Smarcel
55130803Smarcelstatic struct value *
56130803Smarcelvalue_of_builtin_frame_reg (struct frame_info *frame)
57130803Smarcel{
58130803Smarcel  struct value *val;
59130803Smarcel  char *buf;
60130803Smarcel  build_builtin_type_frame_reg ();
61130803Smarcel  val = allocate_value (builtin_type_frame_reg);
62130803Smarcel  VALUE_LVAL (val) = not_lval;
63130803Smarcel  buf = VALUE_CONTENTS_RAW (val);
64130803Smarcel  memset (buf, TYPE_LENGTH (VALUE_TYPE (val)), 0);
65130803Smarcel  /* frame.base.  */
66130803Smarcel  if (frame != NULL)
67130803Smarcel    ADDRESS_TO_POINTER (builtin_type_void_data_ptr, buf,
68130803Smarcel			get_frame_base (frame));
69130803Smarcel  buf += TYPE_LENGTH (builtin_type_void_data_ptr);
70130803Smarcel  /* frame.XXX.  */
71130803Smarcel  return val;
72130803Smarcel}
73130803Smarcel
74130803Smarcelstatic struct value *
75130803Smarcelvalue_of_builtin_frame_fp_reg (struct frame_info *frame)
76130803Smarcel{
77130803Smarcel  if (DEPRECATED_FP_REGNUM >= 0)
78130803Smarcel    /* NOTE: cagney/2003-04-24: Since the mere presence of "fp" in the
79130803Smarcel       register name table overrides this built-in $fp register, there
80130803Smarcel       is no real reason for this DEPRECATED_FP_REGNUM trickery here.
81130803Smarcel       An architecture wanting to implement "$fp" as alias for a raw
82130803Smarcel       register can do so by adding "fp" to register name table (mind
83130803Smarcel       you, doing this is probably a dangerous thing).  */
84130803Smarcel    return value_of_register (DEPRECATED_FP_REGNUM, frame);
85130803Smarcel  else
86130803Smarcel    {
87130803Smarcel      struct value *val = allocate_value (builtin_type_void_data_ptr);
88130803Smarcel      char *buf = VALUE_CONTENTS_RAW (val);
89130803Smarcel      if (frame == NULL)
90130803Smarcel	memset (buf, TYPE_LENGTH (VALUE_TYPE (val)), 0);
91130803Smarcel      else
92130803Smarcel	ADDRESS_TO_POINTER (builtin_type_void_data_ptr, buf,
93130803Smarcel			    get_frame_base_address (frame));
94130803Smarcel      return val;
95130803Smarcel    }
96130803Smarcel}
97130803Smarcel
98130803Smarcelstatic struct value *
99130803Smarcelvalue_of_builtin_frame_pc_reg (struct frame_info *frame)
100130803Smarcel{
101130803Smarcel  if (PC_REGNUM >= 0)
102130803Smarcel    return value_of_register (PC_REGNUM, frame);
103130803Smarcel  else
104130803Smarcel    {
105130803Smarcel      struct value *val = allocate_value (builtin_type_void_data_ptr);
106130803Smarcel      char *buf = VALUE_CONTENTS_RAW (val);
107130803Smarcel      if (frame == NULL)
108130803Smarcel	memset (buf, TYPE_LENGTH (VALUE_TYPE (val)), 0);
109130803Smarcel      else
110130803Smarcel	ADDRESS_TO_POINTER (builtin_type_void_data_ptr, buf,
111130803Smarcel			    get_frame_pc (frame));
112130803Smarcel      return val;
113130803Smarcel    }
114130803Smarcel}
115130803Smarcel
116130803Smarcelstatic struct value *
117130803Smarcelvalue_of_builtin_frame_sp_reg (struct frame_info *frame)
118130803Smarcel{
119130803Smarcel#ifdef SP_REGNUM
120130803Smarcel  if (SP_REGNUM >= 0)
121130803Smarcel    return value_of_register (SP_REGNUM, frame);
122130803Smarcel#endif
123130803Smarcel  error ("Standard register ``$sp'' is not available for this target");
124130803Smarcel}
125130803Smarcel
126130803Smarcelstatic struct value *
127130803Smarcelvalue_of_builtin_frame_ps_reg (struct frame_info *frame)
128130803Smarcel{
129130803Smarcel#ifdef PS_REGNUM
130130803Smarcel  if (PS_REGNUM >= 0)
131130803Smarcel    return value_of_register (PS_REGNUM, frame);
132130803Smarcel#endif
133130803Smarcel  error ("Standard register ``$ps'' is not available for this target");
134130803Smarcel}
135130803Smarcel
136130803Smarcelextern initialize_file_ftype _initialize_frame_reg; /* -Wmissing-prototypes */
137130803Smarcel
138130803Smarcelvoid
139130803Smarcel_initialize_frame_reg (void)
140130803Smarcel{
141130803Smarcel  /* FIXME: cagney/2002-02-08: At present the local builtin types
142130803Smarcel     can't be initialized using _initialize*() or gdbarch.  Due mainly
143130803Smarcel     to non-multi-arch targets, GDB initializes things piece meal and,
144130803Smarcel     as a consequence can leave these types NULL.  */
145130803Smarcel  DEPRECATED_REGISTER_GDBARCH_SWAP (builtin_type_frame_reg);
146130803Smarcel
147130803Smarcel  /* Frame based $fp, $pc, $sp and $ps.  These only come into play
148130803Smarcel     when the target does not define its own version of these
149130803Smarcel     registers.  */
150130803Smarcel  user_reg_add_builtin ("fp", value_of_builtin_frame_fp_reg);
151130803Smarcel  user_reg_add_builtin ("pc", value_of_builtin_frame_pc_reg);
152130803Smarcel  user_reg_add_builtin ("sp", value_of_builtin_frame_sp_reg);
153130803Smarcel  user_reg_add_builtin ("ps", value_of_builtin_frame_ps_reg);
154130803Smarcel
155130803Smarcel  /* NOTE: cagney/2002-04-05: For moment leave the $frame / $gdbframe
156130803Smarcel     / $gdb.frame disabled.  It isn't yet clear which of the many
157130803Smarcel     options is the best.  */
158130803Smarcel  if (0)
159130803Smarcel    user_reg_add_builtin ("frame", value_of_builtin_frame_reg);
160130803Smarcel}
161