* mn10300.igen (OP_F0F4): Need to load contents of register AN0
[platform/upstream/binutils.git] / gdb / jv-lang.c
1 /* Java language support routines for GDB, the GNU debugger.
2    Copyright 1997 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "expression.h"
24 #include "parser-defs.h"
25 #include "language.h"
26 #include "gdbtypes.h"
27 #include "symtab.h"
28 #include "symfile.h"
29 #include "objfiles.h"
30 #include "gdb_string.h"
31 #include "value.h"
32 #include "c-lang.h"
33 #include "jv-lang.h"
34 #include "gdbcore.h"
35
36 struct type *java_int_type;
37 struct type *java_byte_type;
38 struct type *java_short_type;
39 struct type *java_long_type;
40 struct type *java_boolean_type;
41 struct type *java_char_type;
42 struct type *java_float_type;
43 struct type *java_double_type;
44 struct type *java_void_type;
45
46 struct type *java_object_type;
47
48 /* This objfile contains symtabs that have been dynamically created
49    to record dynamically loaded Java classes and dynamically
50    compiled java methods. */
51 struct objfile *dynamics_objfile = NULL;
52
53 struct type *java_link_class_type PARAMS((struct type*, value_ptr));
54
55 struct objfile *
56 get_dynamics_objfile ()
57 {
58   if (dynamics_objfile == NULL)
59     {
60       dynamics_objfile = allocate_objfile (NULL, 0);
61     }
62   return dynamics_objfile;
63 }
64
65 #if 1
66 /* symtab contains classes read from the inferior. */
67
68 static struct symtab *class_symtab = NULL;
69
70 /* Maximum number of class in class_symtab before relocation is needed. */
71
72 static int class_symtab_space;
73
74 struct symtab *
75 get_java_class_symtab ()
76 {
77   if (class_symtab == NULL)
78     {
79       struct objfile *objfile = get_dynamics_objfile();
80       struct blockvector *bv;
81       struct block *bl;
82       class_symtab = allocate_symtab ("<java-classes>", objfile);
83       class_symtab->language = language_java;
84       bv = (struct blockvector *)
85         obstack_alloc (&objfile->symbol_obstack, sizeof (struct blockvector));
86       BLOCKVECTOR_NBLOCKS (bv) = 1;
87       BLOCKVECTOR (class_symtab) = bv;
88
89       /* Allocate dummy STATIC_BLOCK. */
90       bl = (struct block *)
91         obstack_alloc (&objfile->symbol_obstack, sizeof (struct block));
92       BLOCK_NSYMS (bl) = 0;
93       BLOCK_START (bl) = 0;
94       BLOCK_END (bl) = 0;
95       BLOCK_FUNCTION (bl) = NULL;
96       BLOCK_SUPERBLOCK (bl) = NULL;
97       BLOCK_GCC_COMPILED (bl) = 0;
98       BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl;
99
100       /* Allocate GLOBAL_BLOCK.  This has to be relocatable. */
101       class_symtab_space = 128;
102       bl = (struct block *)
103         mmalloc (objfile->md,
104                  sizeof (struct block)
105                  + ((class_symtab_space - 1) * sizeof (struct symbol *)));
106       *bl = *BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
107       BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
108       class_symtab->free_ptr = (char *) bl;
109     }
110   return class_symtab;
111 }
112
113 static void
114 add_class_symtab_symbol (sym)
115      struct symbol *sym;
116 {
117   struct symtab *symtab = get_java_class_symtab ();
118   struct blockvector *bv = BLOCKVECTOR (symtab);
119   struct block *bl = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
120   if (BLOCK_NSYMS (bl) >= class_symtab_space)
121     {
122       /* Need to re-allocate. */
123       class_symtab_space *= 2;
124       bl = (struct block *)
125         mrealloc (symtab->objfile->md, bl,
126                   sizeof (struct block)
127                   + ((class_symtab_space - 1) * sizeof (struct symbol *)));
128       class_symtab->free_ptr = (char *) bl;
129       BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
130     }
131   
132   BLOCK_SYM (bl, BLOCK_NSYMS (bl)) = sym;
133   BLOCK_NSYMS (bl) = BLOCK_NSYMS (bl) + 1;
134 }
135
136 struct symbol *
137 add_class_symbol (type, addr)
138      struct type *type;
139      CORE_ADDR addr;
140 {
141   struct symbol *sym;
142   sym = (struct symbol *)
143     obstack_alloc (&dynamics_objfile->symbol_obstack, sizeof (struct symbol));
144   memset (sym, 0, sizeof (struct symbol));
145   SYMBOL_LANGUAGE (sym) = language_java;
146   SYMBOL_NAME (sym) = TYPE_TAG_NAME (type);
147   SYMBOL_CLASS (sym) = LOC_TYPEDEF;
148   /*  SYMBOL_VALUE (sym) = valu;*/
149   SYMBOL_TYPE (sym) = type;
150   SYMBOL_NAMESPACE (sym) = STRUCT_NAMESPACE;
151   SYMBOL_VALUE_ADDRESS (sym) = addr;
152   return sym;
153 }
154 #endif
155
156 struct type *
157 java_lookup_class (name)
158      char *name;
159 {
160   struct symbol *sym;
161   sym = lookup_symbol (name, expression_context_block, STRUCT_NAMESPACE,
162                        (int *) 0, (struct symtab **) NULL);
163   if (sym != NULL)
164     return SYMBOL_TYPE (sym);
165 #if 0
166   CORE_ADDR addr;
167   if (called from parser)
168     {
169       call lookup_class (or similar) in inferior;
170       if not found:
171         return NULL;
172       addr = found in inferior;
173     }
174   else
175     addr = 0;
176   struct type *type;
177   type = alloc_type (objfile);
178   TYPE_CODE (type) = TYPE_CODE_STRUCT;
179   INIT_CPLUS_SPECIFIC (type);
180   TYPE_TAG_NAME (type) = obsavestring (name, strlen(name), &objfile->type_obstack);
181   TYPE_FLAGS (type) |= TYPE_FLAG_STUB;
182   TYPE ? = addr;
183   return type;
184 #else
185   /* FIXME - should search inferior's symbol table. */
186   return NULL;
187 #endif
188 }
189
190 /* Return a nul-terminated string (allocated on OBSTACK) for
191    a name given by NAME (which has type Utf8Const*). */
192
193 char *
194 get_java_utf8_name (obstack, name)
195      struct obstack *obstack;
196      value_ptr name;
197 {
198   char *chrs;
199   value_ptr temp = name;
200   int name_length;
201   CORE_ADDR data_addr;
202   temp = value_struct_elt (&temp, NULL, "length", NULL, "structure");
203   name_length = (int) value_as_long (temp);
204   data_addr = VALUE_ADDRESS (temp) + VALUE_OFFSET (temp)
205     + TYPE_LENGTH (VALUE_TYPE (temp));
206   chrs = obstack_alloc (obstack, name_length+1);
207   chrs [name_length] = '\0';
208   read_memory_section (data_addr, chrs, name_length, NULL);
209   return chrs;
210 }
211
212 value_ptr
213 java_class_from_object (obj_val)
214      value_ptr obj_val;
215 {
216   /* This is all rather inefficient, since the offsets of dtable and
217      class are fixed.  FIXME */
218   value_ptr dtable_val;
219
220   if (TYPE_CODE (VALUE_TYPE (obj_val)) == TYPE_CODE_PTR
221       && TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (obj_val))) == 0)
222     {
223       struct symbol *sym;
224       sym = lookup_symbol ("Hjava_lang_Object", NULL, STRUCT_NAMESPACE,
225                            (int *) 0, (struct symtab **) NULL);
226       if (sym != NULL)
227         obj_val = value_at (VALUE_TYPE (sym),
228                             value_as_pointer (obj_val), NULL);
229     }
230
231   dtable_val = value_struct_elt (&obj_val, NULL, "dtable", NULL, "structure");
232   return value_struct_elt (&dtable_val, NULL, "class", NULL, "structure");
233 }
234
235 /* Check if CLASS_IS_PRIMITIVE(value of clas): */
236 int
237 java_class_is_primitive (clas)
238      value_ptr clas;
239 {
240   value_ptr dtable = value_struct_elt (&clas, NULL, "dtable", NULL, "struct");
241   CORE_ADDR i = value_as_pointer (dtable);
242   return (int) (i & 0x7fffffff) == (int) 0x7fffffff;
243 }
244
245 /* Read a Kaffe Class object, and generated a gdb (TYPE_CODE_STRUCT) type. */
246
247 struct type *
248 type_from_class (clas)
249      value_ptr clas;
250 {
251   struct type *type;
252   char *name;
253   value_ptr temp;
254   struct objfile *objfile;
255   value_ptr utf8_name;
256   char *nptr;
257   CORE_ADDR addr;
258   struct block *bl;
259   int i;
260   int is_array = 0;
261
262   type = check_typedef (VALUE_TYPE (clas));
263   if (TYPE_CODE (type) == TYPE_CODE_PTR)
264     {
265       if (value_logical_not (clas))
266         return NULL;
267       clas = value_ind (clas);
268     }
269   addr = VALUE_ADDRESS (clas) + VALUE_OFFSET (clas);
270
271 #if 0
272   get_java_class_symtab ();
273   bl = BLOCKVECTOR_BLOCK (BLOCKVECTOR (class_symtab), GLOBAL_BLOCK);
274   for (i = BLOCK_NSYMS (bl);  --i >= 0; )
275     {
276       struct symbol *sym = BLOCK_SYM (bl, i);
277       if (SYMBOL_VALUE_ADDRESS (sym) == addr)
278         return SYMBOL_TYPE (sym);
279     }
280 #endif
281
282   objfile = get_dynamics_objfile();
283   if (java_class_is_primitive (clas))
284     {
285       value_ptr sig;
286       temp = clas;
287       sig = value_struct_elt (&temp, NULL, "msize", NULL, "structure");
288       return java_primitive_type (value_as_long (sig));
289     }
290
291   /* Get Class name. */
292   /* if clasloader non-null, prepend loader address. FIXME */
293   temp = clas;
294   utf8_name = value_struct_elt (&temp, NULL, "name", NULL, "structure");
295   name = get_java_utf8_name (&objfile->type_obstack, utf8_name);
296   for (nptr = name;  *nptr != 0;  nptr++)
297     {
298       if (*nptr == '/')
299         *nptr = '.';
300     }
301
302   type = java_lookup_class (name);
303   if (type != NULL)
304     return type;
305
306   type = alloc_type (objfile);
307   TYPE_CODE (type) = TYPE_CODE_STRUCT;
308   INIT_CPLUS_SPECIFIC (type);
309
310   if (name[0] == '[')
311     {
312       is_array = 1;
313       temp = clas;
314       /* Set array element type. */
315       temp = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
316       VALUE_TYPE (temp) = lookup_pointer_type (VALUE_TYPE (clas));
317       TYPE_TARGET_TYPE (type) = type_from_class (temp);
318     }
319
320   ALLOCATE_CPLUS_STRUCT_TYPE (type);
321   TYPE_TAG_NAME (type) = name;
322
323   add_class_symtab_symbol (add_class_symbol (type, addr));
324   return java_link_class_type (type, clas);
325 }
326
327 /* Fill in class TYPE with data from the CLAS value. */ 
328
329 struct type *
330 java_link_class_type (type, clas)
331      struct type *type;
332      value_ptr clas;
333 {
334   value_ptr temp;
335   char *unqualified_name;
336   char *name = TYPE_TAG_NAME (type);
337   int ninterfaces, nfields, nmethods;
338   int type_is_object = 0;
339   struct fn_field *fn_fields;
340   struct fn_fieldlist *fn_fieldlists;
341   value_ptr fields, field, method, methods;
342   int i, j;
343   struct objfile *objfile = get_dynamics_objfile();
344   struct type *tsuper;
345
346   unqualified_name = strrchr (name, '.');
347   if (unqualified_name == NULL)
348     unqualified_name = name;
349
350   temp = clas;
351   temp = value_struct_elt (&temp, NULL, "superclass", NULL, "structure");
352   if (name != NULL && strcmp (name, "java.lang.Object") == 0)
353     {
354       tsuper = get_java_object_type ();
355       if (tsuper && TYPE_CODE (tsuper) == TYPE_CODE_PTR)
356         tsuper = TYPE_TARGET_TYPE (tsuper);
357       type_is_object = 1;
358     }
359   else
360     tsuper = type_from_class (temp);
361
362 #if 1
363   ninterfaces = 0;
364 #else
365   temp = clas;
366   ninterfaces = value_as_long (value_struct_elt (&temp, NULL, "interface_len", NULL, "structure"));
367 #endif
368   TYPE_N_BASECLASSES (type) = (tsuper == NULL ? 0 : 1) + ninterfaces;
369   temp = clas;
370   nfields = value_as_long (value_struct_elt (&temp, NULL, "nfields", NULL, "structure"));
371   nfields += TYPE_N_BASECLASSES (type);
372   nfields++;  /* Add one for dummy "class" field. */
373   TYPE_NFIELDS (type) = nfields;
374   TYPE_FIELDS (type) = (struct field *)
375     TYPE_ALLOC (type, sizeof (struct field) * nfields);
376
377   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
378
379   TYPE_FIELD_PRIVATE_BITS (type) =
380     (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
381   B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
382
383   TYPE_FIELD_PROTECTED_BITS (type) =
384     (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
385   B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
386
387   TYPE_FIELD_IGNORE_BITS (type) =
388     (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
389   B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
390
391   TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *)
392     TYPE_ALLOC (type, B_BYTES (TYPE_N_BASECLASSES (type)));
393   B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), TYPE_N_BASECLASSES (type));
394
395   if (tsuper != NULL)
396     {
397       TYPE_BASECLASS (type, 0) = tsuper;
398       if (type_is_object)
399         SET_TYPE_FIELD_PRIVATE (type, 0);
400     }
401
402
403   if (name[0] == '[' && tsuper != NULL)
404     {
405       TYPE_LENGTH (type) = TYPE_LENGTH (tsuper) + 4;  /* size with "length" */
406     }
407   else
408     {
409       temp = clas;
410       temp = value_struct_elt (&temp, NULL, "bfsize", NULL, "structure");
411       TYPE_LENGTH (type) = value_as_long (temp);
412     }
413
414   fields = NULL;
415   nfields--;  /* First set up dummy "class" field. */
416   SET_FIELD_PHYSADDR (TYPE_FIELD (type, nfields),
417                       VALUE_ADDRESS (clas) + VALUE_OFFSET (clas));
418   TYPE_FIELD_NAME (type, nfields) = "class";
419   TYPE_FIELD_TYPE (type, nfields) = VALUE_TYPE (clas);
420   SET_TYPE_FIELD_PRIVATE (type, nfields);
421   
422   for (i = TYPE_N_BASECLASSES (type);  i < nfields;  i++)
423     {
424       int accflags;
425       int boffset;
426       if (fields == NULL)
427         {
428           temp = clas;
429           fields = value_struct_elt (&temp, NULL, "fields", NULL, "structure");
430           field = value_ind (fields);
431         }
432       else
433         { /* Re-use field value for next field. */
434           VALUE_ADDRESS (field) += TYPE_LENGTH (VALUE_TYPE (field));
435           VALUE_LAZY (field) = 1;
436         }
437       temp = field;
438       temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
439       TYPE_FIELD_NAME (type, i) =
440         get_java_utf8_name (&objfile->type_obstack, temp);
441       temp = field;
442       accflags = value_as_long (value_struct_elt (&temp, NULL, "accflags",
443                                                   NULL, "structure"));
444       temp = field;
445       temp = value_struct_elt (&temp, NULL, "info", NULL, "structure");
446       boffset = value_as_long (value_struct_elt (&temp, NULL, "boffset",
447                                                   NULL, "structure"));
448       if (accflags & 0x0001) /* public access */
449         {
450           /* ??? */
451         }
452       if (accflags & 0x0002) /* private access */
453         {
454           SET_TYPE_FIELD_PRIVATE (type, i);
455         }
456       if (accflags & 0x0004) /* protected access */
457         {
458           SET_TYPE_FIELD_PROTECTED (type, i);
459         }
460       if (accflags & 0x0008)  /* ACC_STATIC */
461         SET_FIELD_PHYSADDR(TYPE_FIELD(type, i), boffset);
462       else
463         TYPE_FIELD_BITPOS (type, i) = 8 * boffset;
464       if (accflags & 0x8000) /* FIELD_UNRESOLVED_FLAG */
465         {
466           TYPE_FIELD_TYPE (type, i) = get_java_object_type (); /* FIXME */
467         }
468       else
469         {
470           struct type *ftype;
471           temp = field;
472           temp = value_struct_elt (&temp, NULL, "type", NULL, "structure");
473           ftype = type_from_class (temp);
474           if (TYPE_CODE (ftype) == TYPE_CODE_STRUCT)
475             ftype = lookup_pointer_type (ftype);
476           TYPE_FIELD_TYPE (type, i) = ftype;
477         }
478     }
479
480   temp = clas;
481   nmethods = value_as_long (value_struct_elt (&temp, NULL, "nmethods",
482                                               NULL, "structure"));
483   TYPE_NFN_FIELDS_TOTAL (type) = nmethods;
484   j = nmethods * sizeof (struct fn_field);
485   fn_fields = (struct fn_field*)
486     obstack_alloc (&dynamics_objfile->symbol_obstack, j);
487   memset (fn_fields, 0, j);
488   fn_fieldlists = (struct fn_fieldlist*)
489     alloca (nmethods * sizeof (struct fn_fieldlist));
490
491   methods = NULL;
492   for (i = 0;  i < nmethods;  i++)
493     {
494       char *mname;
495       int k;
496       if (methods == NULL)
497         {
498           temp = clas;
499           methods = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
500           method = value_ind (methods);
501         }
502       else
503         { /* Re-use method value for next method. */
504           VALUE_ADDRESS (method) += TYPE_LENGTH (VALUE_TYPE (method));
505           VALUE_LAZY (method) = 1;
506         }
507
508       /* Get method name. */
509       temp = method;
510       temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
511       mname = get_java_utf8_name (&objfile->type_obstack, temp);
512       if (strcmp (mname, "<init>") == 0)
513         mname = unqualified_name;
514
515       /* Check for an existing method with the same name.
516        * This makes building the fn_fieldslists an O(nmethods**2)
517        * operation.  That could be using hashing, but I doubt it
518        * is worth it.  Note that we do maintain the order of methods
519        * in the inferior's Method table (as long as that is grouped
520        * by method name), which I think is desirable.  --PB */
521       for (k = 0, j = TYPE_NFN_FIELDS (type);  ; )
522         {
523           if (--j < 0)
524             { /* No match - new method name. */
525               j = TYPE_NFN_FIELDS(type)++;
526               fn_fieldlists[j].name = mname;
527               fn_fieldlists[j].length = 1;
528               fn_fieldlists[j].fn_fields = &fn_fields[i];
529               k = i;
530               break;
531             }
532           if (strcmp (mname, fn_fieldlists[j].name) == 0)
533             { /* Found an existing method with the same name. */
534               int l;
535               if (mname != unqualified_name)
536                 obstack_free (&objfile->type_obstack, mname);
537               mname = fn_fieldlists[j].name;
538               fn_fieldlists[j].length++;
539               k = i - k;  /* Index of new slot. */
540               /* Shift intervening fn_fields (between k and i) down. */
541               for (l = i;  l > k;  l--) fn_fields[l] = fn_fields[l-1];
542               for (l = TYPE_NFN_FIELDS (type);  --l > j; )
543                 fn_fieldlists[l].fn_fields++;
544               break;
545             }
546           k += fn_fieldlists[j].length;
547         }
548       fn_fields[k].physname = "";
549       fn_fields[k].is_stub = 1;
550       fn_fields[k].type = make_function_type (java_void_type, NULL); /* FIXME*/
551       TYPE_CODE (fn_fields[k].type) = TYPE_CODE_METHOD;
552     }
553
554   j = TYPE_NFN_FIELDS(type) * sizeof (struct fn_fieldlist);
555   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist*)
556     obstack_alloc (&dynamics_objfile->symbol_obstack, j);
557   memcpy (TYPE_FN_FIELDLISTS (type), fn_fieldlists, j);
558  
559   return type;
560 }
561
562 struct type*
563 get_java_object_type ()
564 {
565   return java_object_type;
566 }
567
568 int
569 is_object_type (type)
570      struct type *type;
571 {
572   CHECK_TYPEDEF (type);
573   if (TYPE_CODE (type) == TYPE_CODE_PTR)
574     {
575       struct type *ttype = check_typedef (TYPE_TARGET_TYPE (type));
576       char *name;
577       if (TYPE_CODE (ttype) != TYPE_CODE_STRUCT)
578         return 0;
579       while (TYPE_N_BASECLASSES (ttype) > 0)
580         ttype = TYPE_BASECLASS (ttype, 0);
581       name = TYPE_TAG_NAME (ttype);
582       if (name != NULL && strcmp (name, "java.lang.Object") == 0)
583         return 1;
584       name = TYPE_NFIELDS (ttype) > 0 ? TYPE_FIELD_NAME (ttype, 0) : (char*)0;
585       if (name != NULL && strcmp (name, "dtable") == 0)
586         {
587           if (java_object_type == NULL)
588             java_object_type = type;
589           return 1;
590         }
591     }
592   return 0;
593 }
594
595 struct type*
596 java_primitive_type (signature)
597      int signature;
598 {
599   switch (signature)
600     {
601     case 'B':  return java_byte_type;
602     case 'S':  return java_short_type;
603     case 'I':  return java_int_type;
604     case 'J':  return java_long_type;
605     case 'Z':  return java_boolean_type;
606     case 'C':  return java_char_type;
607     case 'F':  return java_float_type;
608     case 'D':  return java_double_type;
609     case 'V':  return java_void_type;
610     }
611   error ("unknown signature '%c' for primitive type", (char) signature);
612 }
613
614 /* Return the demangled name of the Java type signature string SIGNATURE,
615    as a freshly allocated copy. */
616
617 char *
618 java_demangle_type_signature (signature)
619      char *signature;
620 {
621   int array = 0;
622   char *result;
623   char *ptr;
624   int i;
625   while (*signature == '[')
626     {
627       array++;
628       signature++;
629     }
630   switch (signature[0])
631     {
632     case 'L':
633       /* Substract 2 for 'L' and ';', but add 1 for final nul. */
634       result = xmalloc (strlen (signature) - 1 + 2 * array);
635       signature++;
636       ptr = result;
637       for ( ; *signature != ';' && *signature != '\0'; signature++)
638         {
639           if (*signature == '/')
640             *ptr++ = '.';
641           else
642             *ptr++ = *signature;
643         }
644       break;
645     default:
646       ptr = TYPE_NAME (java_primitive_type (signature[0]));
647       i = strlen (ptr);
648       result = xmalloc (i + 1 + 2 * array);
649       strcpy (result, ptr);
650       ptr = result + i;
651       break;
652     }
653   while (--array >= 0)
654     {
655       *ptr++ = '[';
656       *ptr++ = ']';
657     }
658   *ptr = '\0';
659   return result;
660 }
661
662 struct type *
663 java_lookup_type (signature)
664      char *signature;
665 {
666   switch (signature[0])
667     {
668     case 'L':
669     case '[':
670       error ("java_lookup_type not fully inmplemented");
671     default:
672       return java_primitive_type (signature[0]);
673     }
674 }
675
676 /* Return the type of TYPE followed by DIMS pairs of [ ].
677    If DIMS == 0, TYPE is returned. */
678
679 struct type *
680 java_array_type (type, dims)
681      struct type *type;
682      int dims;
683 {
684   struct type *range_type;
685
686   while (dims-- > 0)
687     {
688       range_type = create_range_type (NULL, builtin_type_int, 0, 0);
689
690       type = create_array_type (NULL, type, range_type);
691     }
692
693   return type;
694 }
695
696 /* Create a Java string in the inferior from a (Utf8) literal. */
697
698 value_ptr
699 java_value_string (ptr, len)
700      char *ptr;
701      int len;
702 {
703   error ("not implemented - java_value_string"); /* FIXME */
704 }
705
706 /* Print the character C on STREAM as part of the contents of a literal
707    string whose delimiter is QUOTER.  Note that that format for printing
708    characters and strings is language specific. */
709
710 void
711 java_emit_char (c, stream, quoter)
712      register int c;
713      GDB_FILE *stream;
714      int quoter;
715 {
716   if (PRINT_LITERAL_FORM (c))
717     {
718       if (c == '\\' || c == quoter)
719         {
720           fputs_filtered ("\\", stream);
721         }
722       fprintf_filtered (stream, "%c", c);
723     }
724   else
725     {
726       switch (c)
727         {
728         case '\n':
729           fputs_filtered ("\\n", stream);
730           break;
731         case '\b':
732           fputs_filtered ("\\b", stream);
733           break;
734         case '\t':
735           fputs_filtered ("\\t", stream);
736           break;
737         case '\f':
738           fputs_filtered ("\\f", stream);
739           break;
740         case '\r':
741           fputs_filtered ("\\r", stream);
742           break;
743         case '\033':
744           fputs_filtered ("\\e", stream);
745           break;
746         case '\007':
747           fputs_filtered ("\\a", stream);
748           break;
749         default:
750           if (c < 256)
751             fprintf_filtered (stream, "\\%.3o", (unsigned int) c);
752           else
753             fprintf_filtered (stream, "\\u%.4x", (unsigned int) c);
754           break;
755         }
756     }
757 }
758
759 void
760 java_printchar (c, stream)
761      int c;
762      GDB_FILE *stream;
763 {
764   fputs_filtered ("'", stream);
765   java_emit_char (c, stream, '\'');
766   fputs_filtered ("'", stream);
767 }
768
769 static value_ptr
770 evaluate_subexp_java (expect_type, exp, pos, noside)
771      struct type *expect_type;
772      register struct expression *exp;
773      register int *pos;
774      enum noside noside;
775 {
776   int pc = *pos;
777   int i;
778   char *name;
779   enum exp_opcode op = exp->elts[*pos].opcode;
780   value_ptr arg1, arg2;
781   struct type *type;
782   switch (op)
783     {
784     case UNOP_IND:
785       if (noside == EVAL_SKIP)
786         goto standard;
787       (*pos)++;
788       arg1 = evaluate_subexp_standard (expect_type, exp, pos, EVAL_NORMAL);
789       if (is_object_type (VALUE_TYPE (arg1)))
790         {
791           struct type *type = type_from_class (java_class_from_object (arg1));
792           arg1 = value_cast (lookup_pointer_type (type), arg1);
793         }
794       if (noside == EVAL_SKIP)
795         goto nosideret;
796       return value_ind (arg1);
797
798     case BINOP_SUBSCRIPT:
799       (*pos)++;
800       arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
801       arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
802       if (noside == EVAL_SKIP)
803         goto nosideret;
804       /* If the user attempts to subscript something that is not an
805          array or pointer type (like a plain int variable for example),
806          then report this as an error. */
807       
808       COERCE_REF (arg1);
809       type = check_typedef (VALUE_TYPE (arg1));
810       name = TYPE_NAME (type);
811       if (TYPE_CODE (type) == TYPE_CODE_PTR)
812         {
813           type = check_typedef (TYPE_TARGET_TYPE (type));
814           if (TYPE_CODE (type) == TYPE_CODE_STRUCT
815               && TYPE_TAG_NAME (type) != NULL 
816               && TYPE_TAG_NAME (type)[0] == '[')
817             {
818               CORE_ADDR address;
819               long length, index;
820               struct type *el_type;
821               char buf4[4];
822
823               value_ptr clas = java_class_from_object(arg1);
824               value_ptr temp = clas;
825               /* Get CLASS_ELEMENT_TYPE of the array type. */
826               temp = value_struct_elt (&temp, NULL, "methods",
827                                        NULL, "structure"); 
828               VALUE_TYPE (temp) = VALUE_TYPE (clas);
829               el_type = type_from_class (temp);
830               if (TYPE_CODE (el_type) == TYPE_CODE_STRUCT)
831                 el_type = lookup_pointer_type (el_type);
832
833               if (noside == EVAL_AVOID_SIDE_EFFECTS)
834                 return value_zero (el_type, VALUE_LVAL (arg1));
835               address = value_as_pointer (arg1);
836               address += JAVA_OBJECT_SIZE;
837               read_memory (address, buf4, 4);
838               length = (long) extract_signed_integer (buf4, 4);
839               index = (long) value_as_long (arg2);
840               if (index >= length || index < 0)
841                 error ("array index (%ld) out of bounds (length: %ld)",
842                        index, length);
843               address = (address + 4) + index * TYPE_LENGTH (el_type);
844               return value_at (el_type, address, NULL);
845             }
846         }
847       else if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
848         {
849           if (noside == EVAL_AVOID_SIDE_EFFECTS)
850             return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
851           else
852             return value_subscript (arg1, arg2);
853         }
854       if (name == NULL)
855         name == TYPE_TAG_NAME (type);
856       if (name)
857         error ("cannot subscript something of type `%s'", name);
858       else
859         error ("cannot subscript requested type");
860
861     case OP_STRING:
862       (*pos)++;
863       i = longest_to_int (exp->elts[pc + 1].longconst);
864       (*pos) += 3 + BYTES_TO_EXP_ELEM (i + 1);
865       if (noside == EVAL_SKIP)
866         goto nosideret;
867       return java_value_string (&exp->elts[pc + 2].string, i);
868
869     case STRUCTOP_STRUCT:
870       arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
871       /* Convert object field (such as TYPE.class) to reference. */
872       if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT)
873         arg1 = value_addr (arg1);
874       return arg1;
875     default:
876       break;
877     }
878 standard:
879   return evaluate_subexp_standard (expect_type, exp, pos, noside);
880  nosideret:
881   return value_from_longest (builtin_type_long, (LONGEST) 1);
882 }
883
884 static struct type *
885 java_create_fundamental_type (objfile, typeid)
886      struct objfile *objfile;
887      int typeid;
888 {
889   switch (typeid)
890     {
891     case FT_VOID:           return java_void_type;
892     case FT_BOOLEAN:        return java_boolean_type;
893     case FT_CHAR:           return java_char_type;
894     case FT_FLOAT:          return java_float_type;
895     case FT_DBL_PREC_FLOAT: return java_double_type;
896     case FT_BYTE: case FT_SIGNED_CHAR:       return java_byte_type;
897     case FT_SHORT: case FT_SIGNED_SHORT:     return java_short_type;
898     case FT_INTEGER: case FT_SIGNED_INTEGER: return java_int_type;
899     case FT_LONG: case FT_SIGNED_LONG:       return java_long_type;
900     }
901   return c_create_fundamental_type (objfile, typeid);
902 }
903
904 /* Table mapping opcodes into strings for printing operators
905    and precedences of the operators.  */
906
907 const struct op_print java_op_print_tab[] =
908   {
909     {",",  BINOP_COMMA, PREC_COMMA, 0},
910     {"=",  BINOP_ASSIGN, PREC_ASSIGN, 1},
911     {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
912     {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
913     {"|",  BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
914     {"^",  BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
915     {"&",  BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
916     {"==", BINOP_EQUAL, PREC_EQUAL, 0},
917     {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
918     {"<=", BINOP_LEQ, PREC_ORDER, 0},
919     {">=", BINOP_GEQ, PREC_ORDER, 0},
920     {">",  BINOP_GTR, PREC_ORDER, 0},
921     {"<",  BINOP_LESS, PREC_ORDER, 0},
922     {">>", BINOP_RSH, PREC_SHIFT, 0},
923     {"<<", BINOP_LSH, PREC_SHIFT, 0},
924 #if 0
925     {">>>", BINOP_???, PREC_SHIFT, 0},
926 #endif
927     {"+",  BINOP_ADD, PREC_ADD, 0},
928     {"-",  BINOP_SUB, PREC_ADD, 0},
929     {"*",  BINOP_MUL, PREC_MUL, 0},
930     {"/",  BINOP_DIV, PREC_MUL, 0},
931     {"%",  BINOP_REM, PREC_MUL, 0},
932     {"-",  UNOP_NEG, PREC_PREFIX, 0},
933     {"!",  UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
934     {"~",  UNOP_COMPLEMENT, PREC_PREFIX, 0},
935     {"*",  UNOP_IND, PREC_PREFIX, 0},
936 #if 0
937     {"instanceof", ???, ???, 0},
938 #endif
939     {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
940     {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
941     {NULL, 0, 0, 0}
942 };
943
944 const struct language_defn java_language_defn = {
945   "java",                               /* Language name */
946   language_java,
947   c_builtin_types,
948   range_check_off,
949   type_check_off,
950   java_parse,
951   java_error,
952   evaluate_subexp_java,
953   java_printchar,               /* Print a character constant */
954   c_printstr,                   /* Function to print string constant */
955   java_create_fundamental_type, /* Create fundamental type in this language */
956   java_print_type,              /* Print a type using appropriate syntax */
957   java_val_print,               /* Print a value using appropriate syntax */
958   java_value_print,             /* Print a top-level value */
959   {"",      "",    "",   ""},   /* Binary format info */
960   {"0%lo",   "0",   "o",  ""},  /* Octal format info */
961   {"%ld",    "",    "d",  ""},  /* Decimal format info */
962   {"0x%lx",  "0x",  "x",  ""},  /* Hex format info */
963   java_op_print_tab,            /* expression operators for printing */
964   0,                            /* not c-style arrays */
965   0,                            /* String lower bound */
966   &builtin_type_char,           /* Type of string elements */ 
967   LANG_MAGIC
968 };
969
970 void
971 _initialize_java_language ()
972 {
973
974   java_int_type    = init_type (TYPE_CODE_INT,  4, 0, "int", NULL);
975   java_short_type  = init_type (TYPE_CODE_INT,  2, 0, "short", NULL);
976   java_long_type   = init_type (TYPE_CODE_INT,  8, 0, "long", NULL);
977   java_byte_type   = init_type (TYPE_CODE_INT,  1, 0, "byte", NULL);
978   java_boolean_type= init_type (TYPE_CODE_BOOL, 1, 0, "boolean", NULL);
979   java_char_type   = init_type (TYPE_CODE_CHAR, 2, 0, "char", NULL);
980   java_float_type  = init_type (TYPE_CODE_FLT,  4, 0, "float", NULL);
981   java_double_type = init_type (TYPE_CODE_FLT,  8, 0, "double", NULL);
982   java_void_type   = init_type (TYPE_CODE_VOID, 1, 0, "void", NULL);
983
984   add_language (&java_language_defn);
985 }
986
987 /* Cleanup code that should be run on every "run".
988    We should use make_run_cleanup to have this be called.
989    But will that mess up values in value histry?  FIXME */
990
991 void java_rerun_cleanup ()
992 {
993   if (class_symtab != NULL)
994     {
995       free_symtab (class_symtab); /* ??? */
996       class_symtab = NULL;
997     }
998   if (dynamics_objfile != NULL)
999     {
1000       free_objfile (dynamics_objfile);
1001       dynamics_objfile = NULL;
1002     }
1003
1004   java_object_type = NULL;
1005 }