Removed v850eq sanitization.
[deliverable/binutils-gdb.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_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_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 value_ptr dtable_val = value_struct_elt (&obj_val, NULL, "dtable", NULL, "structure");
217 return value_struct_elt (&dtable_val, NULL, "class", NULL, "structure");
218 }
219
220 /* Check if CLASS_IS_PRIMITIVE(value of clas): */
221 int
222 java_class_is_primitive (clas)
223 value_ptr clas;
224 {
225 value_ptr dtable = value_struct_elt (&clas, NULL, "dtable", NULL, "struct");
226 CORE_ADDR i = value_as_pointer (dtable);
227 return (int) (i & 0x7fffffff) == (int) 0x7fffffff;
228 }
229
230 /* Read a Kaffe Class object, and generated a gdb (TYPE_CODE_STRUCT) type. */
231
232 struct type *
233 type_from_class (clas)
234 value_ptr clas;
235 {
236 struct type *type;
237 char *name;
238 value_ptr temp;
239 struct objfile *objfile;
240 value_ptr utf8_name;
241 char *nptr;
242 CORE_ADDR addr;
243 struct block *bl;
244 int i;
245 int is_array = 0;
246
247 type = check_typedef (VALUE_TYPE (clas));
248 if (TYPE_CODE (type) == TYPE_CODE_PTR)
249 {
250 if (value_logical_not (clas))
251 return NULL;
252 clas = value_ind (clas);
253 }
254 addr = VALUE_ADDRESS (clas) + VALUE_OFFSET (clas);
255
256 #if 0
257 get_java_class_symtab ();
258 bl = BLOCKVECTOR_BLOCK (BLOCKVECTOR (class_symtab), GLOBAL_BLOCK);
259 for (i = BLOCK_NSYMS (bl); --i >= 0; )
260 {
261 struct symbol *sym = BLOCK_SYM (bl, i);
262 if (SYMBOL_VALUE_ADDRESS (sym) == addr)
263 return SYMBOL_TYPE (sym);
264 }
265 #endif
266
267 objfile = get_dynamics_objfile();
268 if (java_class_is_primitive (clas))
269 {
270 value_ptr sig;
271 temp = clas;
272 sig = value_struct_elt (&temp, NULL, "msize", NULL, "structure");
273 return java_primitive_type (value_as_long (sig));
274 }
275
276 /* Get Class name. */
277 /* if clasloader non-null, prepend loader address. FIXME */
278 temp = clas;
279 utf8_name = value_struct_elt (&temp, NULL, "name", NULL, "structure");
280 name = get_java_utf8_name (&objfile->type_obstack, utf8_name);
281 for (nptr = name; *nptr != 0; nptr++)
282 {
283 if (*nptr == '/')
284 *nptr = '.';
285 }
286
287 type = java_lookup_class (name);
288 if (type != NULL)
289 return type;
290
291 type = alloc_type (objfile);
292 TYPE_CODE (type) = TYPE_CODE_STRUCT;
293 INIT_CPLUS_SPECIFIC (type);
294
295 if (name[0] == '[')
296 {
297 is_array = 1;
298 temp = clas;
299 /* Set array element type. */
300 temp = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
301 VALUE_TYPE (temp) = lookup_pointer_type (VALUE_TYPE (clas));
302 TYPE_TARGET_TYPE (type) = type_from_class (temp);
303 }
304
305 ALLOCATE_CPLUS_STRUCT_TYPE (type);
306 TYPE_NAME (type) = name;
307
308 add_class_symtab_symbol (add_class_symbol (type, addr));
309 return java_link_class_type (type, clas);
310 }
311
312 /* Fill in class TYPE with data from the CLAS value. */
313
314 struct type *
315 java_link_class_type (type, clas)
316 struct type *type;
317 value_ptr clas;
318 {
319 value_ptr temp;
320 char *unqualified_name;
321 char *name = TYPE_NAME (type);
322 int ninterfaces, nfields, nmethods;
323 int type_is_object = 0;
324 struct fn_field *fn_fields;
325 struct fn_fieldlist *fn_fieldlists;
326 value_ptr fields, field, method, methods;
327 int i, j;
328 struct objfile *objfile = get_dynamics_objfile();
329 struct type *tsuper;
330
331 unqualified_name = strrchr (name, '.');
332 if (unqualified_name == NULL)
333 unqualified_name = name;
334
335 temp = clas;
336 temp = value_struct_elt (&temp, NULL, "superclass", NULL, "structure");
337 if (name != NULL && strcmp (name, "java.lang.Object") == 0)
338 {
339 tsuper = get_java_object_type ();
340 if (tsuper && TYPE_CODE (tsuper) == TYPE_CODE_PTR)
341 tsuper = TYPE_TARGET_TYPE (tsuper);
342 type_is_object = 1;
343 }
344 else
345 tsuper = type_from_class (temp);
346
347 #if 1
348 ninterfaces = 0;
349 #else
350 temp = clas;
351 ninterfaces = value_as_long (value_struct_elt (&temp, NULL, "interface_len", NULL, "structure"));
352 #endif
353 TYPE_N_BASECLASSES (type) = (tsuper == NULL ? 0 : 1) + ninterfaces;
354 temp = clas;
355 nfields = value_as_long (value_struct_elt (&temp, NULL, "nfields", NULL, "structure"));
356 nfields += TYPE_N_BASECLASSES (type);
357 nfields++; /* Add one for dummy "class" field. */
358 TYPE_NFIELDS (type) = nfields;
359 TYPE_FIELDS (type) = (struct field *)
360 TYPE_ALLOC (type, sizeof (struct field) * nfields);
361
362 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
363
364 TYPE_FIELD_PRIVATE_BITS (type) =
365 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
366 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
367
368 TYPE_FIELD_PROTECTED_BITS (type) =
369 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
370 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
371
372 TYPE_FIELD_IGNORE_BITS (type) =
373 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
374 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
375
376 TYPE_FIELD_VIRTUAL_BITS (type) = (B_TYPE *)
377 TYPE_ALLOC (type, B_BYTES (TYPE_N_BASECLASSES (type)));
378 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), TYPE_N_BASECLASSES (type));
379
380 if (tsuper != NULL)
381 {
382 TYPE_BASECLASS (type, 0) = tsuper;
383 if (type_is_object)
384 SET_TYPE_FIELD_PRIVATE (type, 0);
385 }
386
387
388 temp = clas;
389 temp = value_struct_elt (&temp, NULL, "bfsize", NULL, "structure");
390 TYPE_LENGTH (type) = value_as_long (temp);
391
392 fields = NULL;
393 nfields--; /* First set up dummy "class" field. */
394 SET_FIELD_PHYSADDR (TYPE_FIELD (type, nfields),
395 VALUE_ADDRESS (clas) + VALUE_OFFSET (clas));
396 TYPE_FIELD_NAME (type, nfields) = "super";
397 TYPE_FIELD_TYPE (type, nfields) = VALUE_TYPE (clas);
398 SET_TYPE_FIELD_PRIVATE (type, nfields);
399
400 for (i = TYPE_N_BASECLASSES (type); i < nfields; i++)
401 {
402 int accflags;
403 int boffset;
404 if (fields == NULL)
405 {
406 temp = clas;
407 fields = value_struct_elt (&temp, NULL, "fields", NULL, "structure");
408 field = value_ind (fields);
409 }
410 else
411 { /* Re-use field value for next field. */
412 VALUE_ADDRESS (field) += TYPE_LENGTH (VALUE_TYPE (field));
413 VALUE_LAZY (field) = 1;
414 }
415 temp = field;
416 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
417 TYPE_FIELD_NAME (type, i) =
418 get_java_utf8_name (&objfile->type_obstack, temp);
419 temp = field;
420 accflags = value_as_long (value_struct_elt (&temp, NULL, "accflags",
421 NULL, "structure"));
422 temp = field;
423 temp = value_struct_elt (&temp, NULL, "info", NULL, "structure");
424 boffset = value_as_long (value_struct_elt (&temp, NULL, "boffset",
425 NULL, "structure"));
426 if (accflags & 0x0001) /* public access */
427 {
428 /* ??? */
429 }
430 if (accflags & 0x0002) /* private access */
431 {
432 SET_TYPE_FIELD_PRIVATE (type, i);
433 }
434 if (accflags & 0x0004) /* protected access */
435 {
436 SET_TYPE_FIELD_PROTECTED (type, i);
437 }
438 if (accflags & 0x0008) /* ACC_STATIC */
439 SET_FIELD_PHYSADDR(TYPE_FIELD(type, i), boffset);
440 else
441 TYPE_FIELD_BITPOS (type, i) = 8 * boffset;
442 if (accflags & 0x8000) /* FIELD_UNRESOLVED_FLAG */
443 {
444 TYPE_FIELD_TYPE (type, i) = get_java_object_type (); /* FIXME */
445 }
446 else
447 {
448 struct type *ftype;
449 temp = field;
450 temp = value_struct_elt (&temp, NULL, "type", NULL, "structure");
451 ftype = type_from_class (temp);
452 if (TYPE_CODE (ftype) == TYPE_CODE_STRUCT)
453 ftype = lookup_pointer_type (ftype);
454 TYPE_FIELD_TYPE (type, i) = ftype;
455 }
456 }
457
458 temp = clas;
459 nmethods = value_as_long (value_struct_elt (&temp, NULL, "nmethods",
460 NULL, "structure"));
461 TYPE_NFN_FIELDS_TOTAL (type) = nmethods;
462 j = nmethods * sizeof (struct fn_field);
463 fn_fields = (struct fn_field*)
464 obstack_alloc (&dynamics_objfile->symbol_obstack, j);
465 memset (fn_fields, 0, j);
466 fn_fieldlists = (struct fn_fieldlist*)
467 alloca (nmethods * sizeof (struct fn_fieldlist));
468
469 methods = NULL;
470 for (i = 0; i < nmethods; i++)
471 {
472 char *mname;
473 int k;
474 if (methods == NULL)
475 {
476 temp = clas;
477 methods = value_struct_elt (&temp, NULL, "methods", NULL, "structure");
478 method = value_ind (methods);
479 }
480 else
481 { /* Re-use method value for next method. */
482 VALUE_ADDRESS (method) += TYPE_LENGTH (VALUE_TYPE (method));
483 VALUE_LAZY (method) = 1;
484 }
485
486 /* Get method name. */
487 temp = method;
488 temp = value_struct_elt (&temp, NULL, "name", NULL, "structure");
489 mname = get_java_utf8_name (&objfile->type_obstack, temp);
490 if (strcmp (mname, "<init>") == 0)
491 mname = unqualified_name;
492
493 /* Check for an existing method with the same name.
494 * This makes building the fn_fieldslists an O(nmethods**2)
495 * operation. That could be using hashing, but I doubt it
496 * is worth it. Note that we do maintain the order of methods
497 * in the inferior's Method table (as long as that is grouped
498 * by method name), which I think is desirable. --PB */
499 for (k = 0, j = TYPE_NFN_FIELDS (type); ; )
500 {
501 if (--j < 0)
502 { /* No match - new method name. */
503 j = TYPE_NFN_FIELDS(type)++;
504 fn_fieldlists[j].name = mname;
505 fn_fieldlists[j].length = 1;
506 fn_fieldlists[j].fn_fields = &fn_fields[i];
507 k = i;
508 break;
509 }
510 if (strcmp (mname, fn_fieldlists[j].name) == 0)
511 { /* Found an existing method with the same name. */
512 int l;
513 if (mname != unqualified_name)
514 obstack_free (&objfile->type_obstack, mname);
515 mname = fn_fieldlists[j].name;
516 fn_fieldlists[j].length++;
517 k = i - k; /* Index of new slot. */
518 /* Shift intervening fn_fields (between k and i) down. */
519 for (l = i; l > k; l--) fn_fields[l] = fn_fields[l-1];
520 for (l = TYPE_NFN_FIELDS (type); --l > j; )
521 fn_fieldlists[l].fn_fields++;
522 break;
523 }
524 k += fn_fieldlists[j].length;
525 }
526 fn_fields[k].physname = "";
527 fn_fields[k].is_stub = 1;
528 fn_fields[k].type = make_function_type (java_void_type, NULL); /* FIXME*/
529 TYPE_CODE (fn_fields[k].type) = TYPE_CODE_METHOD;
530 }
531
532 j = TYPE_NFN_FIELDS(type) * sizeof (struct fn_fieldlist);
533 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist*)
534 obstack_alloc (&dynamics_objfile->symbol_obstack, j);
535 memcpy (TYPE_FN_FIELDLISTS (type), fn_fieldlists, j);
536
537 return type;
538 }
539
540 struct type*
541 get_java_object_type ()
542 {
543 return java_object_type;
544 }
545
546 int
547 is_object_type (type)
548 struct type *type;
549 {
550 CHECK_TYPEDEF (type);
551 if (TYPE_CODE (type) == TYPE_CODE_PTR)
552 {
553 struct type *ttype = check_typedef (TYPE_TARGET_TYPE (type));
554 char *name;
555 if (TYPE_CODE (ttype) != TYPE_CODE_STRUCT)
556 return 0;
557 while (TYPE_N_BASECLASSES (ttype) > 0)
558 ttype = TYPE_BASECLASS (ttype, 0);
559 name = TYPE_NAME (ttype);
560 if (name != NULL && strcmp (name, "java.lang.Object") == 0)
561 return 1;
562 name = TYPE_NFIELDS (ttype) > 0 ? TYPE_FIELD_NAME (ttype, 0) : (char*)0;
563 if (name != NULL && strcmp (name, "dtable") == 0)
564 {
565 if (java_object_type == NULL)
566 java_object_type = type;
567 return 1;
568 }
569 }
570 return 0;
571 }
572
573 struct type*
574 java_primitive_type (signature)
575 int signature;
576 {
577 switch (signature)
578 {
579 case 'B': return java_byte_type;
580 case 'S': return java_short_type;
581 case 'I': return java_int_type;
582 case 'J': return java_long_type;
583 case 'Z': return java_boolean_type;
584 case 'C': return java_char_type;
585 case 'F': return java_float_type;
586 case 'D': return java_double_type;
587 case 'V': return java_void_type;
588 }
589 error ("unknown signature '%c' for primitive type", (char) signature);
590 }
591
592 /* Return the demangled name of the Java type signature string SIGNATURE,
593 as a freshly allocated copy. */
594
595 char *
596 java_demangle_type_signature (signature)
597 char *signature;
598 {
599 int array = 0;
600 char *result;
601 char *ptr;
602 int i;
603 while (*signature == '[')
604 {
605 array++;
606 signature++;
607 }
608 switch (signature[0])
609 {
610 case 'L':
611 /* Substract 2 for 'L' and ';', but add 1 for final nul. */
612 result = xmalloc (strlen (signature) - 1 + 2 * array);
613 signature++;
614 ptr = result;
615 for ( ; *signature != ';' && *signature != '\0'; signature++)
616 {
617 if (*signature == '/')
618 *ptr++ = '.';
619 else
620 *ptr++ = *signature;
621 }
622 break;
623 default:
624 ptr = TYPE_NAME (java_primitive_type (signature[0]));
625 i = strlen (ptr);
626 result = xmalloc (i + 1 + 2 * array);
627 strcpy (result, ptr);
628 ptr = result + i;
629 break;
630 }
631 while (--array >= 0)
632 {
633 *ptr++ = '[';
634 *ptr++ = ']';
635 }
636 *ptr = '\0';
637 return result;
638 }
639
640 struct type *
641 java_lookup_type (signature)
642 char *signature;
643 {
644 switch (signature[0])
645 {
646 case 'L':
647 case '[':
648 error ("java_lookup_type not fully inmplemented");
649 default:
650 return java_primitive_type (signature[0]);
651 }
652 }
653
654 /* Return the type of TYPE followed by DIMS pairs of [ ].
655 If DIMS == 0, TYPE is returned. */
656
657 struct type *
658 java_array_type (type, dims)
659 struct type *type;
660 int dims;
661 {
662 if (dims == 0)
663 return type;
664 error ("array types not implemented");
665 }
666
667 /* Create a Java string in the inferior from a (Utf8) literal. */
668
669 value_ptr
670 java_value_string (ptr, len)
671 char *ptr;
672 int len;
673 {
674 error ("not implemented - java_value_string"); /* FIXME */
675 }
676
677 static value_ptr
678 evaluate_subexp_java (expect_type, exp, pos, noside)
679 struct type *expect_type;
680 register struct expression *exp;
681 register int *pos;
682 enum noside noside;
683 {
684 int pc = *pos;
685 int i;
686 enum exp_opcode op = exp->elts[*pos].opcode;
687 value_ptr arg1;
688 switch (op)
689 {
690 case UNOP_IND:
691 if (noside == EVAL_SKIP)
692 goto standard;
693 (*pos)++;
694 arg1 = evaluate_subexp_standard (expect_type, exp, pos, EVAL_NORMAL);
695 if (is_object_type (VALUE_TYPE (arg1)))
696 {
697 struct type *type = type_from_class (java_class_from_object (arg1));
698 arg1 = value_cast (lookup_pointer_type (type), arg1);
699 }
700 if (noside == EVAL_SKIP)
701 goto nosideret;
702 return value_ind (arg1);
703 case OP_STRING:
704 (*pos)++;
705 i = longest_to_int (exp->elts[pc + 1].longconst);
706 (*pos) += 3 + BYTES_TO_EXP_ELEM (i + 1);
707 if (noside == EVAL_SKIP)
708 goto nosideret;
709 return java_value_string (&exp->elts[pc + 2].string, i);
710 case STRUCTOP_STRUCT:
711 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
712 /* Convert object field (such as .class) to reference. */
713 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_STRUCT)
714 arg1 = value_addr (arg1);
715 return arg1;
716 default:
717 break;
718 }
719 standard:
720 return evaluate_subexp_standard (expect_type, exp, pos, noside);
721 nosideret:
722 return value_from_longest (builtin_type_long, (LONGEST) 1);
723 }
724
725 static struct type *
726 java_create_fundamental_type (objfile, typeid)
727 struct objfile *objfile;
728 int typeid;
729 {
730 switch (typeid)
731 {
732 case FT_VOID: return java_void_type;
733 case FT_BOOLEAN: return java_boolean_type;
734 case FT_CHAR: return java_char_type;
735 case FT_FLOAT: return java_float_type;
736 case FT_DBL_PREC_FLOAT: return java_double_type;
737 case FT_BYTE: case FT_SIGNED_CHAR: return java_byte_type;
738 case FT_SHORT: case FT_SIGNED_SHORT: return java_short_type;
739 case FT_INTEGER: case FT_SIGNED_INTEGER: return java_int_type;
740 case FT_LONG: case FT_SIGNED_LONG: return java_long_type;
741 }
742 return c_create_fundamental_type (objfile, typeid);
743 }
744
745 /* Table mapping opcodes into strings for printing operators
746 and precedences of the operators. */
747
748 const struct op_print java_op_print_tab[] =
749 {
750 {",", BINOP_COMMA, PREC_COMMA, 0},
751 {"=", BINOP_ASSIGN, PREC_ASSIGN, 1},
752 {"||", BINOP_LOGICAL_OR, PREC_LOGICAL_OR, 0},
753 {"&&", BINOP_LOGICAL_AND, PREC_LOGICAL_AND, 0},
754 {"|", BINOP_BITWISE_IOR, PREC_BITWISE_IOR, 0},
755 {"^", BINOP_BITWISE_XOR, PREC_BITWISE_XOR, 0},
756 {"&", BINOP_BITWISE_AND, PREC_BITWISE_AND, 0},
757 {"==", BINOP_EQUAL, PREC_EQUAL, 0},
758 {"!=", BINOP_NOTEQUAL, PREC_EQUAL, 0},
759 {"<=", BINOP_LEQ, PREC_ORDER, 0},
760 {">=", BINOP_GEQ, PREC_ORDER, 0},
761 {">", BINOP_GTR, PREC_ORDER, 0},
762 {"<", BINOP_LESS, PREC_ORDER, 0},
763 {">>", BINOP_RSH, PREC_SHIFT, 0},
764 {"<<", BINOP_LSH, PREC_SHIFT, 0},
765 #if 0
766 {">>>", BINOP_???, PREC_SHIFT, 0},
767 #endif
768 {"+", BINOP_ADD, PREC_ADD, 0},
769 {"-", BINOP_SUB, PREC_ADD, 0},
770 {"*", BINOP_MUL, PREC_MUL, 0},
771 {"/", BINOP_DIV, PREC_MUL, 0},
772 {"%", BINOP_REM, PREC_MUL, 0},
773 {"-", UNOP_NEG, PREC_PREFIX, 0},
774 {"!", UNOP_LOGICAL_NOT, PREC_PREFIX, 0},
775 {"~", UNOP_COMPLEMENT, PREC_PREFIX, 0},
776 {"*", UNOP_IND, PREC_PREFIX, 0},
777 #if 0
778 {"instanceof", ???, ???, 0},
779 #endif
780 {"++", UNOP_PREINCREMENT, PREC_PREFIX, 0},
781 {"--", UNOP_PREDECREMENT, PREC_PREFIX, 0},
782 {NULL, 0, 0, 0}
783 };
784
785 const struct language_defn java_language_defn = {
786 "java", /* Language name */
787 language_java,
788 c_builtin_types,
789 range_check_off,
790 type_check_off,
791 java_parse,
792 java_error,
793 evaluate_subexp_java,
794 c_printchar, /* Print a character constant */
795 c_printstr, /* Function to print string constant */
796 java_create_fundamental_type, /* Create fundamental type in this language */
797 java_print_type, /* Print a type using appropriate syntax */
798 java_val_print, /* Print a value using appropriate syntax */
799 java_value_print, /* Print a top-level value */
800 {"", "", "", ""}, /* Binary format info */
801 {"0%lo", "0", "o", ""}, /* Octal format info */
802 {"%ld", "", "d", ""}, /* Decimal format info */
803 {"0x%lx", "0x", "x", ""}, /* Hex format info */
804 java_op_print_tab, /* expression operators for printing */
805 1, /* c-style arrays */
806 0, /* String lower bound */
807 &builtin_type_char, /* Type of string elements */
808 LANG_MAGIC
809 };
810
811 void
812 _initialize_jave_language ()
813 {
814
815 java_int_type = init_type (TYPE_CODE_INT, 4, 0, "int", NULL);
816 java_short_type = init_type (TYPE_CODE_INT, 2, 0, "short", NULL);
817 java_long_type = init_type (TYPE_CODE_INT, 8, 0, "long", NULL);
818 java_byte_type = init_type (TYPE_CODE_INT, 1, 0, "byte", NULL);
819 java_boolean_type= init_type (TYPE_CODE_BOOL, 1, 0, "boolean", NULL);
820 java_char_type = init_type (TYPE_CODE_CHAR, 2, 0, "char", NULL);
821 java_float_type = init_type (TYPE_CODE_FLT, 4, 0, "float", NULL);
822 java_double_type = init_type (TYPE_CODE_FLT, 8, 0, "double", NULL);
823 java_void_type = init_type (TYPE_CODE_VOID, 1, 0, "void", NULL);
824
825 add_language (&java_language_defn);
826 }
827
828 /* Cleanup code that should be run on every "run".
829 We should use make_run_cleanup to have this be called.
830 But will that mess up values in value histry? FIXME */
831
832 void java_rerun_cleanup ()
833 {
834 if (class_symtab != NULL)
835 {
836 free_symtab (class_symtab); /* ??? */
837 class_symtab = NULL;
838 }
839 if (dynamics_objfile != NULL)
840 {
841 free_objfile (dynamics_objfile);
842 dynamics_objfile = NULL;
843 }
844
845 java_object_type = NULL;
846 }
This page took 0.0476 seconds and 4 git commands to generate.