* config/i386/i386.h (TARGET_SUPPORTS_WIDE_INT): New define.
[official-gcc.git] / gcc / java / boehm.c
blob897f07d61250fd436dc759834a06c9fcb9d9e22e
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 "double-int.h"
30 #include "tm.h"
31 #include "hash-set.h"
32 #include "machmode.h"
33 #include "vec.h"
34 #include "input.h"
35 #include "alias.h"
36 #include "symtab.h"
37 #include "options.h"
38 #include "wide-int.h"
39 #include "inchash.h"
40 #include "tree.h"
41 #include "java-tree.h"
42 #include "parse.h"
43 #include "diagnostic-core.h"
45 static void mark_reference_fields (tree, wide_int *, unsigned int,
46 int *, int *, int *, HOST_WIDE_INT *);
48 /* A procedure-based object descriptor. We know that our
49 `kind' is 0, and `env' is likewise 0, so we have a simple
50 computation. From the GC sources:
51 (((((env) << LOG_MAX_MARK_PROCS) | (proc_index)) << DS_TAG_BITS) \
52 | DS_PROC)
53 Here DS_PROC == 2. */
54 #define PROCEDURE_OBJECT_DESCRIPTOR 2
56 /* Recursively mark reference fields. */
57 static void
58 mark_reference_fields (tree field,
59 wide_int *mask,
60 unsigned int ubit,
61 int *pointer_after_end,
62 int *all_bits_set,
63 int *last_set_index,
64 HOST_WIDE_INT *last_view_index)
66 /* See if we have fields from our superclass. */
67 if (DECL_NAME (field) == NULL_TREE)
69 mark_reference_fields (TYPE_FIELDS (TREE_TYPE (field)),
70 mask, ubit,
71 pointer_after_end, all_bits_set,
72 last_set_index, last_view_index);
73 field = DECL_CHAIN (field);
76 for (; field != NULL_TREE; field = DECL_CHAIN (field))
78 HOST_WIDE_INT offset;
79 HOST_WIDE_INT size_bytes;
81 if (FIELD_STATIC (field))
82 continue;
84 offset = int_byte_position (field);
85 size_bytes = int_size_in_bytes (TREE_TYPE (field));
87 if (JREFERENCE_TYPE_P (TREE_TYPE (field))
88 /* An `object' of type gnu.gcj.RawData is actually non-Java
89 data. */
90 && TREE_TYPE (field) != rawdata_ptr_type_node)
92 unsigned int count;
93 unsigned int size_words;
94 unsigned int i;
96 /* If this reference slot appears to overlay a slot we think
97 we already covered, then we are doomed. */
98 gcc_assert (offset > *last_view_index);
100 if (offset % (HOST_WIDE_INT) (POINTER_SIZE / BITS_PER_UNIT))
102 *all_bits_set = -1;
103 *pointer_after_end = 1;
104 break;
107 count = offset * BITS_PER_UNIT / POINTER_SIZE;
108 size_words = size_bytes * BITS_PER_UNIT / POINTER_SIZE;
110 *last_set_index = count;
112 if (count >= ubit - 2)
113 *pointer_after_end = 1;
114 else
115 /* First word in object corresponds to most significant byte of
116 bitmap.
118 In the case of a multiple-word record, we set pointer
119 bits for all words in the record. This is conservative, but the
120 size_words != 1 case is impossible in regular java code. */
121 for (i = 0; i < size_words; ++i)
122 *mask = wi::set_bit (*mask, ubit - count - i - 1);
124 /* If we saw a non-reference field earlier, then we can't
125 use the count representation. We keep track of that in
126 *ALL_BITS_SET. */
127 if (! *all_bits_set)
128 *all_bits_set = -1;
130 else if (*all_bits_set > 0)
131 *all_bits_set = 0;
133 *last_view_index = offset;
137 /* Return the marking bitmap for the class TYPE. For now this is a
138 single word describing the type. */
139 tree
140 get_boehm_type_descriptor (tree type)
142 unsigned int count, log2_size, ubit;
143 int bit;
144 int all_bits_set = 1;
145 int last_set_index = 0;
146 HOST_WIDE_INT last_view_index = -1;
147 int pointer_after_end = 0;
148 tree field, value, value_type;
150 /* If the GC wasn't requested, just use a null pointer. */
151 if (! flag_use_boehm_gc)
152 return null_pointer_node;
154 value_type = java_type_for_mode (ptr_mode, 1);
155 wide_int mask = wi::zero (TYPE_PRECISION (value_type));
157 /* If we have a type of unknown size, use a proc. */
158 if (int_size_in_bytes (type) == -1)
159 goto procedure_object_descriptor;
161 bit = POINTER_SIZE / BITS_PER_UNIT;
162 /* The size of this node has to be known. And, we only support 32
163 and 64 bit targets, so we need to know that the log2 is one of
164 our values. */
165 log2_size = exact_log2 (bit);
166 if (bit == -1 || (log2_size != 2 && log2_size != 3))
168 /* This means the GC isn't supported. We should probably
169 abort or give an error. Instead, for now, we just silently
170 revert. FIXME. */
171 return null_pointer_node;
173 bit *= BITS_PER_UNIT;
175 /* Warning avoidance. */
176 ubit = (unsigned int) bit;
178 if (type == class_type_node)
179 goto procedure_object_descriptor;
181 field = TYPE_FIELDS (type);
182 mark_reference_fields (field, &mask, ubit,
183 &pointer_after_end, &all_bits_set,
184 &last_set_index, &last_view_index);
186 /* If the object is all pointers, or if the part with pointers fits
187 in our bitmap, then we are ok. Otherwise we have to allocate it
188 a different way. */
189 if (all_bits_set != -1 || (pointer_after_end && flag_reduced_reflection))
191 /* In this case the initial part of the object is all reference
192 fields, and the end of the object is all non-reference
193 fields. We represent the mark as a count of the fields,
194 shifted. In the GC the computation looks something like
195 this:
196 value = DS_LENGTH | WORDS_TO_BYTES (last_set_index + 1);
197 DS_LENGTH is 0.
198 WORDS_TO_BYTES shifts by log2(bytes-per-pointer).
200 In the case of flag_reduced_reflection and the bitmap would
201 overflow, we tell the gc that the object is all pointers so
202 that we don't have to emit reflection data for run time
203 marking. */
204 count = 0;
205 mask = wi::zero (TYPE_PRECISION (value_type));
206 ++last_set_index;
207 while (last_set_index)
209 if ((last_set_index & 1))
210 mask = wi::set_bit (mask, log2_size + count);
211 last_set_index >>= 1;
212 ++count;
214 value = wide_int_to_tree (value_type, mask);
216 else if (! pointer_after_end)
218 /* Bottom two bits for bitmap mark type are 01. */
219 mask = wi::set_bit (mask, 0);
220 value = wide_int_to_tree (value_type, mask);
222 else
224 procedure_object_descriptor:
225 value = build_int_cst (value_type, PROCEDURE_OBJECT_DESCRIPTOR);
228 return value;
231 /* The fourth (index of 3) element in the vtable is the GC descriptor.
232 A value of 2 indicates that the class uses _Jv_MarkObj. */
233 bool
234 uses_jv_markobj_p (tree dtable)
236 tree v;
237 /* FIXME: what do we return if !flag_use_boehm_gc ? */
238 gcc_assert (flag_use_boehm_gc);
239 /* FIXME: this is wrong if TARGET_VTABLE_USES_DESCRIPTORS. However,
240 this function is only used with flag_reduced_reflection. No
241 point in asserting unless we hit the bad case. */
242 gcc_assert (!flag_reduced_reflection || TARGET_VTABLE_USES_DESCRIPTORS == 0);
243 v = (*CONSTRUCTOR_ELTS (dtable))[3].value;
244 return (PROCEDURE_OBJECT_DESCRIPTOR == TREE_INT_CST_LOW (v));