This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[rfc] Helper routines for internal array/string types
- From: "Ulrich Weigand" <uweigand at de dot ibm dot com>
- To: gdb-patches at sourceware dot org
- Date: Fri, 26 Jun 2009 17:56:37 +0200 (CEST)
- Subject: [rfc] Helper routines for internal array/string types
Hello,
many places in GDB want to create temporary array or string types.
This always involves creating both the array/string type, and in
addition creating the range type to use. The typical pattern is:
index_type = builtin_type_int32;
range_type = create_range_type (NULL, index_type, low, high);
array_type = create_array_type (NULL, element_type, range_type);
This patch creates helper routines that simplify that operation,
and changes all instances of the above pattern to use them.
This not just simplifies the code, but also has the advantage of
moving the use of builtin_type_int32 into the helper routine.
In a future patch we can use the architecture-specifc integer type
of the architecture associated with the element type here.
Tested on amd64-linux.
Bye,
Ulrich
ChangeLog:
* gdbtypes.c (lookup_array_range_type): Add prototype.
(lookup_string_range_type): Likewise.
* gdbtypes.c (lookup_array_range_type): New function.
(lookup_string_range_type): Likewise.
* ax-gdb.c (gen_repeat): Use lookup_array_range_type.
* parse.c (follow_types): Likewise.
* jv-lang.c (java_array_type): Likewise.
* gnu-v3-abi.c (build_gdb_vtable_type): Likewise.
* mt-tdep.c (mt_register_type): Likewise.
* sh-tdep.c (sh_sh4_build_float_register_type): Likewise.
* sh64-tdep.c (sh64_build_float_register_type): Likewise.
* value.c (allocate_repeat_value): Likewise.
* valops.c (value_array, value_cstring): Likewise.
* valops.c (value_string): Use lookup_string_range_type.
Index: gdb-head/gdb/ax-gdb.c
===================================================================
--- gdb-head.orig/gdb/ax-gdb.c
+++ gdb-head/gdb/ax-gdb.c
@@ -1394,9 +1394,8 @@ gen_repeat (struct expression *exp, unio
{
/* FIXME-type-allocation: need a way to free this type when we are
done with it. */
- struct type *range
- = create_range_type (0, builtin_type_int32, 0, length - 1);
- struct type *array = create_array_type (0, value1.type, range);
+ struct type *array
+ = lookup_array_range_type (value1.type, 0, length - 1);
value->kind = axs_lvalue_memory;
value->type = array;
Index: gdb-head/gdb/gdbtypes.c
===================================================================
--- gdb-head.orig/gdb/gdbtypes.c
+++ gdb-head/gdb/gdbtypes.c
@@ -833,6 +833,17 @@ create_array_type (struct type *result_t
return result_type;
}
+struct type *
+lookup_array_range_type (struct type *element_type,
+ int low_bound, int high_bound)
+{
+ struct gdbarch *gdbarch = current_gdbarch;
+ struct type *index_type = builtin_type (gdbarch)->builtin_int;
+ struct type *range_type
+ = create_range_type (NULL, index_type, low_bound, high_bound);
+ return create_array_type (NULL, element_type, range_type);
+}
+
/* Create a string type using either a blank type supplied in
RESULT_TYPE, or creating a new type. String types are similar
enough to array of char types that we can use create_array_type to
@@ -858,6 +869,17 @@ create_string_type (struct type *result_
}
struct type *
+lookup_string_range_type (struct type *string_char_type,
+ int low_bound, int high_bound)
+{
+ struct type *result_type;
+ result_type = lookup_array_range_type (string_char_type,
+ low_bound, high_bound);
+ TYPE_CODE (result_type) = TYPE_CODE_STRING;
+ return result_type;
+}
+
+struct type *
create_set_type (struct type *result_type, struct type *domain_type)
{
if (result_type == NULL)
@@ -947,11 +969,7 @@ struct type *
init_vector_type (struct type *elt_type, int n)
{
struct type *array_type;
-
- array_type = create_array_type (0, elt_type,
- create_range_type (0,
- builtin_type_int32,
- 0, n-1));
+ array_type = lookup_array_range_type (elt_type, 0, n - 1);
make_vector_type (array_type);
return array_type;
}
Index: gdb-head/gdb/gdbtypes.h
===================================================================
--- gdb-head.orig/gdb/gdbtypes.h
+++ gdb-head/gdb/gdbtypes.h
@@ -1201,9 +1201,11 @@ extern struct type *create_range_type (s
extern struct type *create_array_type (struct type *, struct type *,
struct type *);
+extern struct type *lookup_array_range_type (struct type *, int, int);
extern struct type *create_string_type (struct type *, struct type *,
struct type *);
+extern struct type *lookup_string_range_type (struct type *, int, int);
extern struct type *create_set_type (struct type *, struct type *);
Index: gdb-head/gdb/gnu-v3-abi.c
===================================================================
--- gdb-head.orig/gdb/gnu-v3-abi.c
+++ gdb-head/gdb/gnu-v3-abi.c
@@ -145,9 +145,7 @@ build_gdb_vtable_type (struct gdbarch *a
/* ptrdiff_t vcall_and_vbase_offsets[0]; */
FIELD_NAME (*field) = "vcall_and_vbase_offsets";
- FIELD_TYPE (*field)
- = create_array_type (0, ptrdiff_type,
- create_range_type (0, builtin_type_int32, 0, -1));
+ FIELD_TYPE (*field) = lookup_array_range_type (ptrdiff_type, 0, -1);
FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
offset += TYPE_LENGTH (FIELD_TYPE (*field));
field++;
@@ -168,9 +166,7 @@ build_gdb_vtable_type (struct gdbarch *a
/* void (*virtual_functions[0]) (); */
FIELD_NAME (*field) = "virtual_functions";
- FIELD_TYPE (*field)
- = create_array_type (0, ptr_to_void_fn_type,
- create_range_type (0, builtin_type_int32, 0, -1));
+ FIELD_TYPE (*field) = lookup_array_range_type (ptr_to_void_fn_type, 0, -1);
FIELD_BITPOS (*field) = offset * TARGET_CHAR_BIT;
offset += TYPE_LENGTH (FIELD_TYPE (*field));
field++;
Index: gdb-head/gdb/jv-lang.c
===================================================================
--- gdb-head.orig/gdb/jv-lang.c
+++ gdb-head/gdb/jv-lang.c
@@ -803,13 +803,10 @@ java_demangle_type_signature (char *sign
struct type *
java_array_type (struct type *type, int dims)
{
- struct type *range_type;
-
while (dims-- > 0)
{
- range_type = create_range_type (NULL, builtin_type_int32, 0, 0);
/* FIXME This is bogus! Java arrays are not gdb arrays! */
- type = create_array_type (NULL, type, range_type);
+ type = lookup_array_range_type (type, 0, 0);
}
return type;
Index: gdb-head/gdb/mt-tdep.c
===================================================================
--- gdb-head.orig/gdb/mt-tdep.c
+++ gdb-head/gdb/mt-tdep.c
@@ -257,11 +257,7 @@ mt_register_type (struct gdbarch *arch,
if (regnum >= 0 && regnum < MT_NUM_REGS + MT_NUM_PSEUDO_REGS)
{
if (copro_type == NULL)
- {
- struct type *temp;
- temp = create_range_type (NULL, builtin_type_int32, 0, 1);
- copro_type = create_array_type (NULL, builtin_type_int16, temp);
- }
+ copro_type = lookup_array_range_type (builtin_type_int16, 0, 1);
switch (regnum)
{
case MT_PC_REGNUM:
Index: gdb-head/gdb/parse.c
===================================================================
--- gdb-head.orig/gdb/parse.c
+++ gdb-head/gdb/parse.c
@@ -1257,7 +1257,6 @@ follow_types (struct type *follow_type)
int make_volatile = 0;
int make_addr_space = 0;
int array_size;
- struct type *range_type;
while (!done)
switch (pop_type ())
@@ -1323,13 +1322,9 @@ follow_types (struct type *follow_type)
array_size = pop_type_int ();
/* FIXME-type-allocation: need a way to free this type when we are
done with it. */
- range_type =
- create_range_type ((struct type *) NULL,
- builtin_type_int32, 0,
- array_size >= 0 ? array_size - 1 : 0);
follow_type =
- create_array_type ((struct type *) NULL,
- follow_type, range_type);
+ lookup_array_range_type (follow_type,
+ 0, array_size >= 0 ? array_size - 1 : 0);
if (array_size < 0)
TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (follow_type) = 1;
break;
Index: gdb-head/gdb/sh-tdep.c
===================================================================
--- gdb-head.orig/gdb/sh-tdep.c
+++ gdb-head/gdb/sh-tdep.c
@@ -2146,10 +2146,8 @@ sh_sh3e_register_type (struct gdbarch *g
static struct type *
sh_sh4_build_float_register_type (struct gdbarch *gdbarch, int high)
{
- struct type *temp;
-
- temp = create_range_type (NULL, builtin_type_int32, 0, high);
- return create_array_type (NULL, builtin_type (gdbarch)->builtin_float, temp);
+ return lookup_array_range_type (builtin_type (gdbarch)->builtin_float,
+ 0, high);
}
static struct type *
Index: gdb-head/gdb/sh64-tdep.c
===================================================================
--- gdb-head.orig/gdb/sh64-tdep.c
+++ gdb-head/gdb/sh64-tdep.c
@@ -1498,10 +1498,8 @@ REGISTER_BYTE returns the register byte
static struct type *
sh64_build_float_register_type (struct gdbarch *gdbarch, int high)
{
- struct type *temp;
-
- temp = create_range_type (NULL, builtin_type_int32, 0, high);
- return create_array_type (NULL, builtin_type (gdbarch)->builtin_float, temp);
+ return lookup_array_range_type (builtin_type (gdbarch)->builtin_float,
+ 0, high);
}
/* Return the GDB type object for the "standard" data type
Index: gdb-head/gdb/valops.c
===================================================================
--- gdb-head.orig/gdb/valops.c
+++ gdb-head/gdb/valops.c
@@ -1294,7 +1294,6 @@ value_array (int lowbound, int highbound
int idx;
unsigned int typelength;
struct value *val;
- struct type *rangetype;
struct type *arraytype;
CORE_ADDR addr;
@@ -1315,12 +1314,8 @@ value_array (int lowbound, int highbound
}
}
- rangetype = create_range_type ((struct type *) NULL,
- builtin_type_int32,
- lowbound, highbound);
- arraytype = create_array_type ((struct type *) NULL,
- value_enclosing_type (elemvec[0]),
- rangetype);
+ arraytype = lookup_array_range_type (value_enclosing_type (elemvec[0]),
+ lowbound, highbound);
if (!current_language->c_style_arrays)
{
@@ -1351,12 +1346,8 @@ value_cstring (char *ptr, int len, struc
struct value *val;
int lowbound = current_language->string_lower_bound;
int highbound = len / TYPE_LENGTH (char_type);
- struct type *rangetype = create_range_type ((struct type *) NULL,
- builtin_type_int32,
- lowbound,
- highbound + lowbound - 1);
struct type *stringtype
- = create_array_type ((struct type *) NULL, char_type, rangetype);
+ = lookup_array_range_type (char_type, lowbound, highbound + lowbound - 1);
val = allocate_value (stringtype);
memcpy (value_contents_raw (val), ptr, len);
@@ -1378,12 +1369,8 @@ value_string (char *ptr, int len, struct
struct value *val;
int lowbound = current_language->string_lower_bound;
int highbound = len / TYPE_LENGTH (char_type);
- struct type *rangetype = create_range_type ((struct type *) NULL,
- builtin_type_int32,
- lowbound,
- highbound + lowbound - 1);
struct type *stringtype
- = create_string_type ((struct type *) NULL, char_type, rangetype);
+ = lookup_string_range_type (char_type, lowbound, highbound + lowbound - 1);
val = allocate_value (stringtype);
memcpy (value_contents_raw (val), ptr, len);
Index: gdb-head/gdb/value.c
===================================================================
--- gdb-head.orig/gdb/value.c
+++ gdb-head/gdb/value.c
@@ -290,13 +290,9 @@ allocate_repeat_value (struct type *type
int low_bound = current_language->string_lower_bound; /* ??? */
/* FIXME-type-allocation: need a way to free this type when we are
done with it. */
- struct type *range_type
- = create_range_type ((struct type *) NULL, builtin_type_int32,
- low_bound, count + low_bound - 1);
- /* FIXME-type-allocation: need a way to free this type when we are
- done with it. */
- return allocate_value (create_array_type ((struct type *) NULL,
- type, range_type));
+ struct type *array_type
+ = lookup_array_range_type (type, low_bound, count + low_bound - 1);
+ return allocate_value (array_type);
}
/* Needed if another module needs to maintain its on list of values. */
--
Dr. Ulrich Weigand
GNU Toolchain for Linux on System z and Cell BE
Ulrich.Weigand@de.ibm.com