OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [gdb/] [jv-lang.c] - Blame information for rev 104

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.