PR target/37072
[official-gcc.git] / gcc / java / boehm.c
blob51714b4dae7be620f8528b36441880410e76e63a
1 /* Functions related to the Boehm garbage collector.
2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC 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 3, or (at your option)
9 any later version.
11 GCC 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.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
20 Java and all Java-based marks are trademarks or registered trademarks
21 of Sun Microsystems, Inc. in the United States and other countries.
22 The Free Software Foundation is independent of Sun Microsystems, Inc. */
24 /* Written by Tom Tromey <tromey@cygnus.com>. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "options.h"
33 #include "tree.h"
34 #include "java-tree.h"
35 #include "parse.h"
36 #include "diagnostic-core.h"
38 static void mark_reference_fields (tree, wide_int *, unsigned int,
39 int *, int *, int *, HOST_WIDE_INT *);
41 /* A procedure-based object descriptor. We know that our
42 `kind' is 0, and `env' is likewise 0, so we have a simple
43 computation. From the GC sources:
44 (((((env) << LOG_MAX_MARK_PROCS) | (proc_index)) << DS_TAG_BITS) \
45 | DS_PROC)
46 Here DS_PROC == 2. */
47 #define PROCEDURE_OBJECT_DESCRIPTOR 2
49 /* Recursively mark reference fields. */
50 static void
51 mark_reference_fields (tree field,
52 wide_int *mask,
53 unsigned int ubit,
54 int *pointer_after_end,
55 int *all_bits_set,
56 int *last_set_index,
57 HOST_WIDE_INT *last_view_index)
59 /* See if we have fields from our superclass. */
60 if (DECL_NAME (field) == NULL_TREE)
62 mark_reference_fields (TYPE_FIELDS (TREE_TYPE (field)),
63 mask, ubit,
64 pointer_after_end, all_bits_set,
65 last_set_index, last_view_index);
66 field = DECL_CHAIN (field);
69 for (; field != NULL_TREE; field = DECL_CHAIN (field))
71 HOST_WIDE_INT offset;
72 HOST_WIDE_INT size_bytes;
74 if (FIELD_STATIC (field))
75 continue;
77 offset = int_byte_position (field);
78 size_bytes = int_size_in_bytes (TREE_TYPE (field));
80 if (JREFERENCE_TYPE_P (TREE_TYPE (field))
81 /* An `object' of type gnu.gcj.RawData is actually non-Java
82 data. */
83 && TREE_TYPE (field) != rawdata_ptr_type_node)
85 unsigned int count;
86 unsigned int size_words;
87 unsigned int i;
89 /* If this reference slot appears to overlay a slot we think
90 we already covered, then we are doomed. */
91 gcc_assert (offset > *last_view_index);
93 if (offset % (HOST_WIDE_INT) (POINTER_SIZE / BITS_PER_UNIT))
95 *all_bits_set = -1;
96 *pointer_after_end = 1;
97 break;
100 count = offset * BITS_PER_UNIT / POINTER_SIZE;
101 size_words = size_bytes * BITS_PER_UNIT / POINTER_SIZE;
103 *last_set_index = count;
105 if (count >= ubit - 2)
106 *pointer_after_end = 1;
107 else
108 /* First word in object corresponds to most significant byte of
109 bitmap.
111 In the case of a multiple-word record, we set pointer
112 bits for all words in the record. This is conservative, but the
113 size_words != 1 case is impossible in regular java code. */
114 for (i = 0; i < size_words; ++i)
115 *mask = wi::set_bit (*mask, ubit - count - i - 1);
117 /* If we saw a non-reference field earlier, then we can't
118 use the count representation. We keep track of that in
119 *ALL_BITS_SET. */
120 if (! *all_bits_set)
121 *all_bits_set = -1;
123 else if (*all_bits_set > 0)
124 *all_bits_set = 0;
126 *last_view_index = offset;
130 /* Return the marking bitmap for the class TYPE. For now this is a
131 single word describing the type. */
132 tree
133 get_boehm_type_descriptor (tree type)
135 unsigned int count, log2_size, ubit;
136 int bit;
137 int all_bits_set = 1;
138 int last_set_index = 0;
139 HOST_WIDE_INT last_view_index = -1;
140 int pointer_after_end = 0;
141 tree field, value, value_type;
143 /* If the GC wasn't requested, just use a null pointer. */
144 if (! flag_use_boehm_gc)
145 return null_pointer_node;
147 value_type = java_type_for_mode (ptr_mode, 1);
148 wide_int mask = wi::zero (TYPE_PRECISION (value_type));
150 /* If we have a type of unknown size, use a proc. */
151 if (int_size_in_bytes (type) == -1)
152 goto procedure_object_descriptor;
154 bit = POINTER_SIZE / BITS_PER_UNIT;
155 /* The size of this node has to be known. And, we only support 32
156 and 64 bit targets, so we need to know that the log2 is one of
157 our values. */
158 log2_size = exact_log2 (bit);
159 if (bit == -1 || (log2_size != 2 && log2_size != 3))
161 /* This means the GC isn't supported. We should probably
162 abort or give an error. Instead, for now, we just silently
163 revert. FIXME. */
164 return null_pointer_node;
166 bit *= BITS_PER_UNIT;
168 /* Warning avoidance. */
169 ubit = (unsigned int) bit;
171 if (type == class_type_node)
172 goto procedure_object_descriptor;
174 field = TYPE_FIELDS (type);
175 mark_reference_fields (field, &mask, ubit,
176 &pointer_after_end, &all_bits_set,
177 &last_set_index, &last_view_index);
179 /* If the object is all pointers, or if the part with pointers fits
180 in our bitmap, then we are ok. Otherwise we have to allocate it
181 a different way. */
182 if (all_bits_set != -1 || (pointer_after_end && flag_reduced_reflection))
184 /* In this case the initial part of the object is all reference
185 fields, and the end of the object is all non-reference
186 fields. We represent the mark as a count of the fields,
187 shifted. In the GC the computation looks something like
188 this:
189 value = DS_LENGTH | WORDS_TO_BYTES (last_set_index + 1);
190 DS_LENGTH is 0.
191 WORDS_TO_BYTES shifts by log2(bytes-per-pointer).
193 In the case of flag_reduced_reflection and the bitmap would
194 overflow, we tell the gc that the object is all pointers so
195 that we don't have to emit reflection data for run time
196 marking. */
197 count = 0;
198 mask = wi::zero (TYPE_PRECISION (value_type));
199 ++last_set_index;
200 while (last_set_index)
202 if ((last_set_index & 1))
203 mask = wi::set_bit (mask, log2_size + count);
204 last_set_index >>= 1;
205 ++count;
207 value = wide_int_to_tree (value_type, mask);
209 else if (! pointer_after_end)
211 /* Bottom two bits for bitmap mark type are 01. */
212 mask = wi::set_bit (mask, 0);
213 value = wide_int_to_tree (value_type, mask);
215 else
217 procedure_object_descriptor:
218 value = build_int_cst (value_type, PROCEDURE_OBJECT_DESCRIPTOR);
221 return value;
224 /* The fourth (index of 3) element in the vtable is the GC descriptor.
225 A value of 2 indicates that the class uses _Jv_MarkObj. */
226 bool
227 uses_jv_markobj_p (tree dtable)
229 tree v;
230 /* FIXME: what do we return if !flag_use_boehm_gc ? */
231 gcc_assert (flag_use_boehm_gc);
232 /* FIXME: this is wrong if TARGET_VTABLE_USES_DESCRIPTORS. However,
233 this function is only used with flag_reduced_reflection. No
234 point in asserting unless we hit the bad case. */
235 gcc_assert (!flag_reduced_reflection || TARGET_VTABLE_USES_DESCRIPTORS == 0);
236 v = (*CONSTRUCTOR_ELTS (dtable))[3].value;
237 return (PROCEDURE_OBJECT_DESCRIPTOR == TREE_INT_CST_LOW (v));