This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[rfc] Make Java types per-architecture


Hello,

most languages have been changed to provide per-architecture copies of
their fundamental data type.  The only remaining exception is Java,
where this hasn't been necessary so far as Java types are defined to
be the same on all platforms.

This is no longer true once we want to associate an architecture with
every type in the system.  This patch converts Java over to the same
scheme used by all other languages; it should be completely mechanical.

Tested on amd64-linux and powerpc64-linux.

Bye,
Ulrich

ChangeLog:

	* jv-lang.h (java_int_type, java_byte_type, java_short_type,
	java_long_type, java_boolean_type, java_char_type, java_float_type,
	java_double_type, java_void_type): Remove.
	(struct builtin_java_type): New data type.
	(builtin_java_type): Add prototype.
	(java_primitive_type): Add GDBARCH argument.
	(java_primitive_type_from_name): Likewise.
	(type_from_class): Likewise.

	* jv-lang.c (java_int_type, java_byte_type, java_short_type,
	java_long_type, java_boolean_type, java_char_type, java_float_type,
	java_double_type, java_void_type): Remove.

	(build_java_types, builtin_java_type): New functions.
	(java_type_data): New static variable.
	(_initialize_java_language): Initialize it.  No longer initialize
	global types.
	(java_language_arch_info): Use per-architecture types.

	(java_primitive_type): Add GDBARCH argument.  Return per-architecture
	type instead of refering to global variable.
	(java_primitive_type_from_name): Add GDBARCH argument.
	(java_primitive_type_name): New function.
	(java_demangled_signature_length): Use it instead of
	java_primitive_type_from_name.
	(java_demangled_signature_copy): Likewise.

	(type_from_class): Add GDBARCH argument.  Pass to java_link_class_type,
	java_primitive_type, and recursive type_from_class call.
	(java_link_class_type): Add GDBARCH argument.  Pass to type_from_class
	calls.  Use per-architecture types instead of global types.

	* jv-exp.y (parse_java_type): New define.
	Use per-architecture types instead of global types througout.

	* jv-valprint.c (java_value_print): Pass architecture to
	type_from_class and java_primitive_type_from_name.  Use per-
	architecture types instead of global types.


Index: gdb-head/gdb/jv-exp.y
===================================================================
--- gdb-head.orig/gdb/jv-exp.y
+++ gdb-head/gdb/jv-exp.y
@@ -50,6 +50,7 @@
 #include "block.h"
 
 #define parse_type builtin_type (parse_gdbarch)
+#define parse_java_type builtin_java_type (parse_gdbarch)
 
 /* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
    as well as gratuitiously global symbol names, so we can have multiple
@@ -249,7 +250,7 @@ Literal:
 		  write_exp_elt_opcode (OP_DOUBLE); }
 |	BOOLEAN_LITERAL
 		{ write_exp_elt_opcode (OP_LONG);
-		  write_exp_elt_type (java_boolean_type);
+		  write_exp_elt_type (parse_java_type->builtin_boolean);
 		  write_exp_elt_longcst ((LONGEST)$1);
 		  write_exp_elt_opcode (OP_LONG); }
 |	StringLiteral
@@ -265,7 +266,7 @@ Type:
 PrimitiveType:
 	NumericType
 |	BOOLEAN
-		{ $$ = java_boolean_type; }
+		{ $$ = parse_java_type->builtin_boolean; }
 ;
 
 NumericType:
@@ -275,22 +276,22 @@ NumericType:
 
 IntegralType:
 	BYTE
-		{ $$ = java_byte_type; }
+		{ $$ = parse_java_type->builtin_byte; }
 |	SHORT
-		{ $$ = java_short_type; }
+		{ $$ = parse_java_type->builtin_short; }
 |	INT
-		{ $$ = java_int_type; }
+		{ $$ = parse_java_type->builtin_int; }
 |	LONG
-		{ $$ = java_long_type; }
+		{ $$ = parse_java_type->builtin_long; }
 |	CHAR
-		{ $$ = java_char_type; }
+		{ $$ = parse_java_type->builtin_char; }
 ;
 
 FloatingPointType:
 	FLOAT
-		{ $$ = java_float_type; }
+		{ $$ = parse_java_type->builtin_float; }
 |	DOUBLE
-		{ $$ = java_double_type; }
+		{ $$ = parse_java_type->builtin_double; }
 ;
 
 /* UNUSED:
@@ -765,12 +766,12 @@ parse_number (char *p, int len, int pars
   limit = ((limit << 16) << 16) | limit;
   if (c == 'l' || c == 'L')
     {
-      type = java_long_type;
+      type = parse_java_type->builtin_long;
       len--;
     }
   else
     {
-      type = java_int_type;
+      type = parse_java_type->builtin_int;
     }
   limit_div_base = limit / (ULONGEST) base;
 
@@ -795,10 +796,10 @@ parse_number (char *p, int len, int pars
 
   /* If the type is bigger than a 32-bit signed integer can be, implicitly
      promote to long.  Java does not do this, so mark it as builtin_type_uint64
-     rather than java_long_type.  0x80000000 will become -0x80000000 instead
-     of 0x80000000L, because we don't know the sign at this point.
-  */
-  if (type == java_int_type && n > (ULONGEST)0x80000000)
+     rather than parse_java_type->builtin_long.  0x80000000 will become
+     -0x80000000 instead of 0x80000000L, because we don't know the sign
+     at this point.  */
+  if (type == parse_java_type->builtin_int && n > (ULONGEST)0x80000000)
     type = builtin_type_uint64;
 
   putithere->typed_val_int.val = n;
@@ -902,7 +903,7 @@ yylex (void)
 	error (_("Empty character constant"));
 
       yylval.typed_val_int.val = c;
-      yylval.typed_val_int.type = java_char_type;
+      yylval.typed_val_int.type = parse_java_type->builtin_char;
 
       c = *lexptr++;
       if (c != '\'')
Index: gdb-head/gdb/jv-lang.c
===================================================================
--- gdb-head.orig/gdb/jv-lang.c
+++ gdb-head/gdb/jv-lang.c
@@ -39,16 +39,6 @@
 #include <ctype.h>
 #include "gdb_assert.h"
 
-struct type *java_int_type;
-struct type *java_byte_type;
-struct type *java_short_type;
-struct type *java_long_type;
-struct type *java_boolean_type;
-struct type *java_char_type;
-struct type *java_float_type;
-struct type *java_double_type;
-struct type *java_void_type;
-
 /* Local functions */
 
 extern void _initialize_java_language (void);
@@ -72,7 +62,8 @@ static char *java_class_name_from_physna
 
 static struct objfile *dynamics_objfile = NULL;
 
-static struct type *java_link_class_type (struct type *, struct value *);
+static struct type *java_link_class_type (struct gdbarch *,
+					  struct type *, struct value *);
 
 /* FIXME: carlton/2003-02-04: This is the main or only caller of
    allocate_objfile with first argument NULL; as a result, this code
@@ -246,7 +237,7 @@ java_class_is_primitive (struct value *c
 /* Read a GCJ Class object, and generated a gdb (TYPE_CODE_STRUCT) type. */
 
 struct type *
-type_from_class (struct value *clas)
+type_from_class (struct gdbarch *gdbarch, struct value *clas)
 {
   struct type *type;
   char *name;
@@ -284,7 +275,7 @@ type_from_class (struct value *clas)
       struct value *sig;
       temp = clas;
       sig = value_struct_elt (&temp, NULL, "method_count", NULL, "structure");
-      return java_primitive_type (value_as_long (sig));
+      return java_primitive_type (gdbarch, value_as_long (sig));
     }
 
   /* Get Class name. */
@@ -322,20 +313,21 @@ type_from_class (struct value *clas)
       /* Set array element type. */
       temp = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
       deprecated_set_value_type (temp, lookup_pointer_type (value_type (clas)));
-      TYPE_TARGET_TYPE (type) = type_from_class (temp);
+      TYPE_TARGET_TYPE (type) = type_from_class (gdbarch, temp);
     }
 
   ALLOCATE_CPLUS_STRUCT_TYPE (type);
   TYPE_TAG_NAME (type) = name;
 
   add_class_symtab_symbol (add_class_symbol (type, addr));
-  return java_link_class_type (type, clas);
+  return java_link_class_type (gdbarch, type, clas);
 }
 
 /* Fill in class TYPE with data from the CLAS value. */
 
 struct type *
-java_link_class_type (struct type *type, struct value *clas)
+java_link_class_type (struct gdbarch *gdbarch,
+		      struct type *type, struct value *clas)
 {
   struct value *temp;
   char *unqualified_name;
@@ -367,7 +359,7 @@ java_link_class_type (struct type *type,
       type_is_object = 1;
     }
   else
-    tsuper = type_from_class (temp);
+    tsuper = type_from_class (gdbarch, temp);
 
 #if 1
   ninterfaces = 0;
@@ -482,7 +474,7 @@ java_link_class_type (struct type *type,
 	  struct type *ftype;
 	  temp = field;
 	  temp = value_struct_elt (&temp, NULL, "type", NULL, "structure");
-	  ftype = type_from_class (temp);
+	  ftype = type_from_class (gdbarch, temp);
 	  if (TYPE_CODE (ftype) == TYPE_CODE_STRUCT)
 	    ftype = lookup_pointer_type (ftype);
 	  TYPE_FIELD_TYPE (type, i) = ftype;
@@ -563,7 +555,8 @@ java_link_class_type (struct type *type,
       fn_fields[k].physname = "";
       fn_fields[k].is_stub = 1;
       /* FIXME */
-      fn_fields[k].type = lookup_function_type (java_void_type);
+      fn_fields[k].type = lookup_function_type
+			   (builtin_java_type (gdbarch)->builtin_void);
       TYPE_CODE (fn_fields[k].type) = TYPE_CODE_METHOD;
     }
 
@@ -628,28 +621,30 @@ is_object_type (struct type *type)
 }
 
 struct type *
-java_primitive_type (int signature)
+java_primitive_type (struct gdbarch *gdbarch, int signature)
 {
+  const struct builtin_java_type *builtin = builtin_java_type (gdbarch);
+
   switch (signature)
     {
     case 'B':
-      return java_byte_type;
+      return builtin->builtin_byte;
     case 'S':
-      return java_short_type;
+      return builtin->builtin_short;
     case 'I':
-      return java_int_type;
+      return builtin->builtin_int;
     case 'J':
-      return java_long_type;
+      return builtin->builtin_long;
     case 'Z':
-      return java_boolean_type;
+      return builtin->builtin_boolean;
     case 'C':
-      return java_char_type;
+      return builtin->builtin_char;
     case 'F':
-      return java_float_type;
+      return builtin->builtin_float;
     case 'D':
-      return java_double_type;
+      return builtin->builtin_double;
     case 'V':
-      return java_void_type;
+      return builtin->builtin_void;
     }
   error (_("unknown signature '%c' for primitive type"), (char) signature);
 }
@@ -658,47 +653,77 @@ java_primitive_type (int signature)
    return that type.  Otherwise, return NULL. */
 
 struct type *
-java_primitive_type_from_name (char *name, int namelen)
+java_primitive_type_from_name (struct gdbarch *gdbarch,
+			       char *name, int namelen)
 {
+  const struct builtin_java_type *builtin = builtin_java_type (gdbarch);
+
   switch (name[0])
     {
     case 'b':
       if (namelen == 4 && memcmp (name, "byte", 4) == 0)
-	return java_byte_type;
+	return builtin->builtin_byte;
       if (namelen == 7 && memcmp (name, "boolean", 7) == 0)
-	return java_boolean_type;
+	return builtin->builtin_boolean;
       break;
     case 'c':
       if (namelen == 4 && memcmp (name, "char", 4) == 0)
-	return java_char_type;
+	return builtin->builtin_char;
     case 'd':
       if (namelen == 6 && memcmp (name, "double", 6) == 0)
-	return java_double_type;
+	return builtin->builtin_double;
       break;
     case 'f':
       if (namelen == 5 && memcmp (name, "float", 5) == 0)
-	return java_float_type;
+	return builtin->builtin_float;
       break;
     case 'i':
       if (namelen == 3 && memcmp (name, "int", 3) == 0)
-	return java_int_type;
+	return builtin->builtin_int;
       break;
     case 'l':
       if (namelen == 4 && memcmp (name, "long", 4) == 0)
-	return java_long_type;
+	return builtin->builtin_long;
       break;
     case 's':
       if (namelen == 5 && memcmp (name, "short", 5) == 0)
-	return java_short_type;
+	return builtin->builtin_short;
       break;
     case 'v':
       if (namelen == 4 && memcmp (name, "void", 4) == 0)
-	return java_void_type;
+	return builtin->builtin_void;
       break;
     }
   return NULL;
 }
 
+static char *
+java_primitive_type_name (int signature)
+{
+  switch (signature)
+    {
+    case 'B':
+      return "byte";
+    case 'S':
+      return "short";
+    case 'I':
+      return "int";
+    case 'J':
+      return "long";
+    case 'Z':
+      return "boolean";
+    case 'C':
+      return "char";
+    case 'F':
+      return "float";
+    case 'D':
+      return "double";
+    case 'V':
+      return "void";
+    }
+  error (_("unknown signature '%c' for primitive type"), (char) signature);
+}
+
 /* Return the length (in bytes) of demangled name of the Java type
    signature string SIGNATURE. */
 
@@ -714,7 +739,7 @@ java_demangled_signature_length (char *s
       /* Subtract 2 for 'L' and ';'. */
       return strlen (signature) - 2 + array;
     default:
-      return strlen (TYPE_NAME (java_primitive_type (signature[0]))) + array;
+      return strlen (java_primitive_type_name (signature[0])) + array;
     }
 }
 
@@ -746,7 +771,7 @@ java_demangled_signature_copy (char *res
 	}
       break;
     default:
-      ptr = TYPE_NAME (java_primitive_type (signature[0]));
+      ptr = java_primitive_type_name (signature[0]);
       i = strlen (ptr);
       strcpy (result, ptr);
       ptr = result + i;
@@ -857,7 +882,7 @@ evaluate_subexp_java (struct type *expec
 	{
 	  struct type *type;
 
-	  type = type_from_class (java_class_from_object (arg1));
+	  type = type_from_class (exp->gdbarch, java_class_from_object (arg1));
 	  arg1 = value_cast (lookup_pointer_type (type), arg1);
 	}
       if (noside == EVAL_SKIP)
@@ -896,7 +921,7 @@ evaluate_subexp_java (struct type *expec
 	  temp = value_struct_elt (&temp, NULL, "methods",
 				   NULL, "structure");
 	  deprecated_set_value_type (temp, value_type (clas));
-	  el_type = type_from_class (temp);
+	  el_type = type_from_class (exp->gdbarch, temp);
 	  if (TYPE_CODE (el_type) == TYPE_CODE_STRUCT)
 	    el_type = lookup_pointer_type (el_type);
 
@@ -1065,31 +1090,33 @@ static void
 java_language_arch_info (struct gdbarch *gdbarch,
 			 struct language_arch_info *lai)
 {
-  lai->string_char_type = java_char_type;
+  const struct builtin_java_type *builtin = builtin_java_type (gdbarch);
+
+  lai->string_char_type = builtin->builtin_char;
   lai->primitive_type_vector
     = GDBARCH_OBSTACK_CALLOC (gdbarch, nr_java_primitive_types + 1,
                               struct type *);
   lai->primitive_type_vector [java_primitive_type_int]
-    = java_int_type;
+    = builtin->builtin_int;
   lai->primitive_type_vector [java_primitive_type_short]
-    = java_short_type;
+    = builtin->builtin_short;
   lai->primitive_type_vector [java_primitive_type_long]
-    = java_long_type;
+    = builtin->builtin_long;
   lai->primitive_type_vector [java_primitive_type_byte]
-    = java_byte_type;
+    = builtin->builtin_byte;
   lai->primitive_type_vector [java_primitive_type_boolean]
-    = java_boolean_type;
+    = builtin->builtin_boolean;
   lai->primitive_type_vector [java_primitive_type_char]
-    = java_char_type;
+    = builtin->builtin_char;
   lai->primitive_type_vector [java_primitive_type_float]
-    = java_float_type;
+    = builtin->builtin_float;
   lai->primitive_type_vector [java_primitive_type_double]
-    = java_double_type;
+    = builtin->builtin_double;
   lai->primitive_type_vector [java_primitive_type_void]
-    = java_void_type;
+    = builtin->builtin_void;
 
   lai->bool_type_symbol = "boolean";
-  lai->bool_type_default = java_boolean_type;
+  lai->bool_type_default = builtin->builtin_boolean;
 }
 
 const struct exp_descriptor exp_descriptor_java = 
@@ -1139,19 +1166,46 @@ const struct language_defn java_language
   LANG_MAGIC
 };
 
+static void *
+build_java_types (struct gdbarch *gdbarch)
+{
+  struct builtin_java_type *builtin_java_type
+    = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct builtin_java_type);
+
+  builtin_java_type->builtin_int
+    = init_type (TYPE_CODE_INT, 4, 0, "int", NULL);
+  builtin_java_type->builtin_short
+    = init_type (TYPE_CODE_INT, 2, 0, "short", NULL);
+  builtin_java_type->builtin_long
+    = init_type (TYPE_CODE_INT, 8, 0, "long", NULL);
+  builtin_java_type->builtin_byte
+    = init_type (TYPE_CODE_INT, 1, 0, "byte", NULL);
+  builtin_java_type->builtin_boolean
+    = init_type (TYPE_CODE_BOOL, 1, 0, "boolean", NULL);
+  builtin_java_type->builtin_char
+    = init_type (TYPE_CODE_CHAR, 2, TYPE_FLAG_UNSIGNED, "char", NULL);
+  builtin_java_type->builtin_float
+    = init_type (TYPE_CODE_FLT, 4, 0, "float", NULL);
+  builtin_java_type->builtin_double
+    = init_type (TYPE_CODE_FLT, 8, 0, "double", NULL);
+  builtin_java_type->builtin_void
+    = init_type (TYPE_CODE_VOID, 1, 0, "void", NULL);
+
+  return builtin_java_type;
+}
+
+static struct gdbarch_data *java_type_data;
+
+const struct builtin_java_type *
+builtin_java_type (struct gdbarch *gdbarch)
+{
+  return gdbarch_data (gdbarch, java_type_data);
+}
+
 void
 _initialize_java_language (void)
 {
-
-  java_int_type = init_type (TYPE_CODE_INT, 4, 0, "int", NULL);
-  java_short_type = init_type (TYPE_CODE_INT, 2, 0, "short", NULL);
-  java_long_type = init_type (TYPE_CODE_INT, 8, 0, "long", NULL);
-  java_byte_type = init_type (TYPE_CODE_INT, 1, 0, "byte", NULL);
-  java_boolean_type = init_type (TYPE_CODE_BOOL, 1, 0, "boolean", NULL);
-  java_char_type = init_type (TYPE_CODE_CHAR, 2, TYPE_FLAG_UNSIGNED, "char", NULL);
-  java_float_type = init_type (TYPE_CODE_FLT, 4, 0, "float", NULL);
-  java_double_type = init_type (TYPE_CODE_FLT, 8, 0, "double", NULL);
-  java_void_type = init_type (TYPE_CODE_VOID, 1, 0, "void", NULL);
+  java_type_data = gdbarch_data_register_post_init (build_java_types);
 
   add_language (&java_language_defn);
 }
Index: gdb-head/gdb/jv-lang.h
===================================================================
--- gdb-head.orig/gdb/jv-lang.h
+++ gdb-head/gdb/jv-lang.h
@@ -27,15 +27,20 @@ extern int java_parse (void);	/* Defined
 
 extern void java_error (char *);	/* Defined in jv-exp.y */
 
-extern struct type *java_int_type;
-extern struct type *java_byte_type;
-extern struct type *java_short_type;
-extern struct type *java_long_type;
-extern struct type *java_boolean_type;
-extern struct type *java_char_type;
-extern struct type *java_float_type;
-extern struct type *java_double_type;
-extern struct type *java_void_type;
+struct builtin_java_type
+{
+  struct type *builtin_int;
+  struct type *builtin_byte;
+  struct type *builtin_short;
+  struct type *builtin_long;
+  struct type *builtin_boolean;
+  struct type *builtin_char;
+  struct type *builtin_float;
+  struct type *builtin_double;
+  struct type *builtin_void;
+};
+
+extern const struct builtin_java_type *builtin_java_type (struct gdbarch *);
 
 extern int java_val_print (struct type *, const gdb_byte *, int, CORE_ADDR,
 			   struct ui_file *, int,
@@ -46,11 +51,12 @@ extern int java_value_print (struct valu
 
 extern struct value *java_class_from_object (struct value *);
 
-extern struct type *type_from_class (struct value *);
+extern struct type *type_from_class (struct gdbarch *, struct value *);
 
-extern struct type *java_primitive_type (int signature);
+extern struct type *java_primitive_type (struct gdbarch *, int signature);
 
-extern struct type *java_primitive_type_from_name (char *, int);
+extern struct type *java_primitive_type_from_name (struct gdbarch *,
+						   char *, int);
 
 extern struct type *java_array_type (struct type *, int);
 
Index: gdb-head/gdb/jv-valprint.c
===================================================================
--- gdb-head.orig/gdb/jv-valprint.c
+++ gdb-head/gdb/jv-valprint.c
@@ -58,7 +58,7 @@ java_value_print (struct value *val, str
 
       if (obj_addr != 0)
 	{
-	  type = type_from_class (java_class_from_object (val));
+	  type = type_from_class (gdbarch, java_class_from_object (val));
 	  type = lookup_pointer_type (type);
 
 	  val = value_at (type, address);
@@ -76,7 +76,8 @@ java_value_print (struct value *val, str
       long length;
       unsigned int things_printed = 0;
       int reps;
-      struct type *el_type = java_primitive_type_from_name (name, i - 2);
+      struct type *el_type
+	= java_primitive_type_from_name (gdbarch, name, i - 2);
       i = 0;
       read_memory (address + get_java_object_header_size (gdbarch), buf4, 4);
 
@@ -211,6 +212,7 @@ java_value_print (struct value *val, str
       && address != 0
       && value_as_address (val) != 0)
     {
+      struct type *char_type;
       struct value *data_val;
       CORE_ADDR data;
       struct value *boffset_val;
@@ -232,7 +234,8 @@ java_value_print (struct value *val, str
 
       value_free_to_mark (mark);	/* Release unnecessary values */
 
-      val_print_string (java_char_type, data + boffset, count, stream, options);
+      char_type = builtin_java_type (gdbarch)->builtin_char;
+      val_print_string (char_type, data + boffset, count, stream, options);
 
       return 0;
     }
-- 
  Dr. Ulrich Weigand
  GNU Toolchain for Linux on System z and Cell BE
  Ulrich.Weigand@de.ibm.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]