constants.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /* Handle the constant pool of the Java(TM) Virtual Machine.
  2. Copyright (C) 1997-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>.
  15. Java and all Java-based marks are trademarks or registered trademarks
  16. of Sun Microsystems, Inc. in the United States and other countries.
  17. The Free Software Foundation is independent of Sun Microsystems, Inc. */
  18. #include "config.h"
  19. #include "system.h"
  20. #include "coretypes.h"
  21. #include "tm.h"
  22. #include "jcf.h"
  23. #include "hash-set.h"
  24. #include "machmode.h"
  25. #include "vec.h"
  26. #include "double-int.h"
  27. #include "input.h"
  28. #include "alias.h"
  29. #include "symtab.h"
  30. #include "wide-int.h"
  31. #include "inchash.h"
  32. #include "tree.h"
  33. #include "fold-const.h"
  34. #include "stringpool.h"
  35. #include "stor-layout.h"
  36. #include "java-tree.h"
  37. #include "diagnostic-core.h"
  38. #include "toplev.h"
  39. #include "ggc.h"
  40. static void set_constant_entry (CPool *, int, int, jword);
  41. static int find_tree_constant (CPool *, int, tree);
  42. static int find_name_and_type_constant (CPool *, tree, tree);
  43. static tree get_tag_node (int);
  44. /* Set the INDEX'th constant in CPOOL to have the given TAG and VALUE. */
  45. static void
  46. set_constant_entry (CPool *cpool, int index, int tag, jword value)
  47. {
  48. if (cpool->data == NULL)
  49. {
  50. cpool->capacity = 100;
  51. cpool->tags = ggc_cleared_vec_alloc<uint8> (cpool->capacity);
  52. cpool->data = ggc_cleared_vec_alloc<cpool_entry> (cpool->capacity);
  53. cpool->count = 1;
  54. }
  55. if (index >= cpool->capacity)
  56. {
  57. int old_cap = cpool->capacity;
  58. cpool->capacity *= 2;
  59. if (index >= cpool->capacity)
  60. cpool->capacity = index + 10;
  61. cpool->tags = GGC_RESIZEVEC (uint8, cpool->tags, cpool->capacity);
  62. cpool->data = GGC_RESIZEVEC (union cpool_entry, cpool->data,
  63. cpool->capacity);
  64. /* Make sure GC never sees uninitialized tag values. */
  65. memset (cpool->tags + old_cap, 0, cpool->capacity - old_cap);
  66. memset (cpool->data + old_cap, 0,
  67. (cpool->capacity - old_cap) * sizeof (union cpool_entry));
  68. }
  69. if (index >= cpool->count)
  70. cpool->count = index + 1;
  71. cpool->tags[index] = tag;
  72. cpool->data[index].w = value;
  73. }
  74. /* Find (or create) a constant pool entry matching TAG and VALUE. */
  75. int
  76. find_constant1 (CPool *cpool, int tag, jword value)
  77. {
  78. int i;
  79. for (i = cpool->count; --i > 0; )
  80. {
  81. if (cpool->tags[i] == tag && cpool->data[i].w == value)
  82. return i;
  83. }
  84. i = cpool->count == 0 ? 1 : cpool->count;
  85. set_constant_entry (cpool, i, tag, value);
  86. return i;
  87. }
  88. /* Find a double-word constant pool entry matching TAG and WORD1/WORD2. */
  89. int
  90. find_constant2 (CPool *cpool, int tag, jword word1, jword word2)
  91. {
  92. int i;
  93. for (i = cpool->count - 1; --i > 0; )
  94. {
  95. if (cpool->tags[i] == tag
  96. && cpool->data[i].w == word1
  97. && cpool->data[i+1].w == word2)
  98. return i;
  99. }
  100. i = cpool->count == 0 ? 1 : cpool->count;
  101. set_constant_entry (cpool, i, tag, word1);
  102. set_constant_entry (cpool, i+1, 0, word2);
  103. return i;
  104. }
  105. static int
  106. find_tree_constant (CPool *cpool, int tag, tree value)
  107. {
  108. int i;
  109. for (i = cpool->count; --i > 0; )
  110. {
  111. if (cpool->tags[i] == tag && cpool->data[i].t == value)
  112. return i;
  113. }
  114. i = cpool->count == 0 ? 1 : cpool->count;
  115. set_constant_entry (cpool, i, tag, 0);
  116. cpool->data[i].t = value;
  117. return i;
  118. }
  119. int
  120. find_utf8_constant (CPool *cpool, tree name)
  121. {
  122. if (name == NULL_TREE)
  123. return 0;
  124. return find_tree_constant (cpool, CONSTANT_Utf8, name);
  125. }
  126. int
  127. find_class_or_string_constant (CPool *cpool, int tag, tree name)
  128. {
  129. jword j = find_utf8_constant (cpool, name);
  130. int i;
  131. for (i = cpool->count; --i > 0; )
  132. {
  133. if (cpool->tags[i] == tag && cpool->data[i].w == j)
  134. return i;
  135. }
  136. i = cpool->count;
  137. set_constant_entry (cpool, i, tag, j);
  138. return i;
  139. }
  140. int
  141. find_class_constant (CPool *cpool, tree type)
  142. {
  143. return find_class_or_string_constant (cpool, CONSTANT_Class,
  144. build_internal_class_name (type));
  145. }
  146. /* Allocate a CONSTANT_string entry given a STRING_CST. */
  147. int
  148. find_string_constant (CPool *cpool, tree string)
  149. {
  150. string = get_identifier (TREE_STRING_POINTER (string));
  151. return find_class_or_string_constant (cpool, CONSTANT_String, string);
  152. }
  153. /* Find (or create) a CONSTANT_NameAndType matching NAME and TYPE.
  154. Return its index in the constant pool CPOOL. */
  155. static int
  156. find_name_and_type_constant (CPool *cpool, tree name, tree type)
  157. {
  158. int name_index = find_utf8_constant (cpool, name);
  159. int type_index = find_utf8_constant (cpool, build_java_signature (type));
  160. return find_constant1 (cpool, CONSTANT_NameAndType,
  161. (name_index << 16) | type_index);
  162. }
  163. /* Find (or create) a CONSTANT_Fieldref for DECL (a FIELD_DECL or VAR_DECL).
  164. Return its index in the constant pool CPOOL. */
  165. int
  166. find_fieldref_index (CPool *cpool, tree decl)
  167. {
  168. int class_index = find_class_constant (cpool, DECL_CONTEXT (decl));
  169. int name_type_index
  170. = find_name_and_type_constant (cpool, DECL_NAME (decl), TREE_TYPE (decl));
  171. return find_constant1 (cpool, CONSTANT_Fieldref,
  172. (class_index << 16) | name_type_index);
  173. }
  174. /* Find (or create) a CONSTANT_Methodref for DECL (a FUNCTION_DECL).
  175. Return its index in the constant pool CPOOL. */
  176. int
  177. find_methodref_index (CPool *cpool, tree decl)
  178. {
  179. return find_methodref_with_class_index (cpool, decl, DECL_CONTEXT (decl));
  180. }
  181. int
  182. find_methodref_with_class_index (CPool *cpool, tree decl, tree mclass)
  183. {
  184. int class_index = find_class_constant (cpool, mclass);
  185. tree name = DECL_CONSTRUCTOR_P (decl) ? init_identifier_node
  186. : DECL_NAME (decl);
  187. int name_type_index;
  188. name_type_index =
  189. find_name_and_type_constant (cpool, name, TREE_TYPE (decl));
  190. return find_constant1 (cpool,
  191. CLASS_INTERFACE (TYPE_NAME (mclass))
  192. ? CONSTANT_InterfaceMethodref
  193. : CONSTANT_Methodref,
  194. (class_index << 16) | name_type_index);
  195. }
  196. #define PUT1(X) (*ptr++ = (X))
  197. #define PUT2(X) (PUT1((X) >> 8), PUT1(X))
  198. #define PUT4(X) (PUT2((X) >> 16), PUT2(X))
  199. #define PUTN(P, N) (memcpy(ptr, (P), (N)), ptr += (N))
  200. /* Give the number of bytes needed in a .class file for the CPOOL
  201. constant pool. Includes the 2-byte constant_pool_count. */
  202. int
  203. count_constant_pool_bytes (CPool *cpool)
  204. {
  205. int size = 2;
  206. int i = 1;
  207. for ( ; i < cpool->count; i++)
  208. {
  209. size++;
  210. switch (cpool->tags[i])
  211. {
  212. case CONSTANT_NameAndType:
  213. case CONSTANT_Fieldref:
  214. case CONSTANT_Methodref:
  215. case CONSTANT_InterfaceMethodref:
  216. case CONSTANT_Float:
  217. case CONSTANT_Integer:
  218. size += 4;
  219. break;
  220. case CONSTANT_Class:
  221. case CONSTANT_String:
  222. size += 2;
  223. break;
  224. case CONSTANT_Long:
  225. case CONSTANT_Double:
  226. size += 8;
  227. i++;
  228. break;
  229. case CONSTANT_Utf8:
  230. {
  231. tree t = cpool->data[i].t;
  232. int len = IDENTIFIER_LENGTH (t);
  233. size += len + 2;
  234. }
  235. break;
  236. default:
  237. /* Second word of CONSTANT_Long and CONSTANT_Double. */
  238. size--;
  239. }
  240. }
  241. return size;
  242. }
  243. /* Write the constant pool CPOOL into BUFFER.
  244. The length of BUFFER is LENGTH, which must match the needed length. */
  245. void
  246. write_constant_pool (CPool *cpool, unsigned char *buffer, int length)
  247. {
  248. unsigned char *ptr = buffer;
  249. int i = 1;
  250. union cpool_entry *datap = &cpool->data[1];
  251. PUT2 (cpool->count);
  252. for ( ; i < cpool->count; i++, datap++)
  253. {
  254. int tag = cpool->tags[i];
  255. PUT1 (tag);
  256. switch (tag)
  257. {
  258. case CONSTANT_NameAndType:
  259. case CONSTANT_Fieldref:
  260. case CONSTANT_Methodref:
  261. case CONSTANT_InterfaceMethodref:
  262. case CONSTANT_Float:
  263. case CONSTANT_Integer:
  264. PUT4 (datap->w);
  265. break;
  266. case CONSTANT_Class:
  267. case CONSTANT_String:
  268. PUT2 (datap->w);
  269. break;
  270. break;
  271. case CONSTANT_Long:
  272. case CONSTANT_Double:
  273. PUT4(datap->w);
  274. i++;
  275. datap++;
  276. PUT4 (datap->w);
  277. break;
  278. case CONSTANT_Utf8:
  279. {
  280. tree t = datap->t;
  281. int len = IDENTIFIER_LENGTH (t);
  282. PUT2 (len);
  283. PUTN (IDENTIFIER_POINTER (t), len);
  284. }
  285. break;
  286. }
  287. }
  288. gcc_assert (ptr == buffer + length);
  289. }
  290. static GTY(()) tree tag_nodes[13];
  291. static tree
  292. get_tag_node (int tag)
  293. {
  294. /* A Cache for build_int_cst (CONSTANT_XXX, 0). */
  295. if (tag >= 13)
  296. return build_int_cst (NULL_TREE, tag);
  297. if (tag_nodes[tag] == NULL_TREE)
  298. tag_nodes[tag] = build_int_cst (NULL_TREE, tag);
  299. return tag_nodes[tag];
  300. }
  301. /* Given a class, return its constant pool, creating one if necessary. */
  302. CPool *
  303. cpool_for_class (tree klass)
  304. {
  305. CPool *cpool = TYPE_CPOOL (klass);
  306. if (cpool == NULL)
  307. {
  308. cpool = ggc_cleared_alloc<CPool> ();
  309. TYPE_CPOOL (klass) = cpool;
  310. }
  311. return cpool;
  312. }
  313. /* Look for a constant pool entry that matches TAG and NAME.
  314. Creates a new entry if not found.
  315. TAG is one of CONSTANT_Utf8, CONSTANT_String or CONSTANT_Class.
  316. NAME is an IDENTIFIER_NODE naming the Utf8 constant, string, or class.
  317. Returns the index of the entry. */
  318. int
  319. alloc_name_constant (int tag, tree name)
  320. {
  321. CPool *outgoing_cpool = cpool_for_class (output_class);
  322. return find_tree_constant (outgoing_cpool, tag, name);
  323. }
  324. /* Create a constant pool entry for a name_and_type. This one has '.'
  325. rather than '/' because it isn't going into a class file, it's
  326. going into a compiled object. We don't use the '/' separator in
  327. compiled objects. */
  328. static int
  329. find_name_and_type_constant_tree (CPool *cpool, tree name, tree type)
  330. {
  331. int name_index = find_utf8_constant (cpool, name);
  332. int type_index
  333. = find_utf8_constant (cpool,
  334. identifier_subst (build_java_signature (type),
  335. "", '/', '.', ""));
  336. return find_constant1 (cpool, CONSTANT_NameAndType,
  337. (name_index << 16) | type_index);
  338. }
  339. /* Look for a field ref that matches DECL in the constant pool of
  340. KLASS.
  341. Return the index of the entry. */
  342. int
  343. alloc_constant_fieldref (tree klass, tree decl)
  344. {
  345. CPool *outgoing_cpool = cpool_for_class (klass);
  346. int class_index
  347. = find_tree_constant (outgoing_cpool, CONSTANT_Class,
  348. DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))));
  349. int name_type_index
  350. = find_name_and_type_constant_tree (outgoing_cpool, DECL_NAME (decl),
  351. TREE_TYPE (decl));
  352. return find_constant1 (outgoing_cpool, CONSTANT_Fieldref,
  353. (class_index << 16) | name_type_index);
  354. }
  355. /* Build an identifier for the internal name of reference type TYPE. */
  356. tree
  357. build_internal_class_name (tree type)
  358. {
  359. tree name;
  360. if (TYPE_ARRAY_P (type))
  361. name = build_java_signature (type);
  362. else
  363. {
  364. name = TYPE_NAME (type);
  365. if (TREE_CODE (name) != IDENTIFIER_NODE)
  366. name = DECL_NAME (name);
  367. name = identifier_subst (name, "", '.', '/', "");
  368. }
  369. return name;
  370. }
  371. /* Look for a CONSTANT_Class entry for CLAS, creating a new one if needed. */
  372. int
  373. alloc_class_constant (tree clas)
  374. {
  375. tree class_name = build_internal_class_name (clas);
  376. return alloc_name_constant (CONSTANT_Class,
  377. (unmangle_classname
  378. (IDENTIFIER_POINTER(class_name),
  379. IDENTIFIER_LENGTH(class_name))));
  380. }
  381. /* Return the decl of the data array of the current constant pool. */
  382. tree
  383. build_constant_data_ref (bool indirect)
  384. {
  385. if (indirect)
  386. {
  387. tree d;
  388. tree cpool_type = build_array_type (ptr_type_node, NULL_TREE);
  389. tree decl = build_class_ref (output_class);
  390. tree klass = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (decl)),
  391. decl);
  392. tree constants = build3 (COMPONENT_REF,
  393. TREE_TYPE (constants_field_decl_node), klass,
  394. constants_field_decl_node,
  395. NULL_TREE);
  396. tree data = build3 (COMPONENT_REF,
  397. TREE_TYPE (constants_data_field_decl_node),
  398. constants,
  399. constants_data_field_decl_node,
  400. NULL_TREE);
  401. TREE_THIS_NOTRAP (klass) = 1;
  402. data = fold_convert (build_pointer_type (cpool_type), data);
  403. d = build1 (INDIRECT_REF, cpool_type, data);
  404. return d;
  405. }
  406. else
  407. {
  408. tree decl_name = mangled_classname ("_CD_", output_class);
  409. tree decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
  410. if (! decl)
  411. {
  412. /* Build a type with unspecified bounds. The will make sure
  413. that targets do the right thing with whatever size we end
  414. up with at the end. Using bounds that are too small risks
  415. assuming the data is in the small data section. */
  416. tree type = build_array_type (ptr_type_node, NULL_TREE);
  417. /* We need to lay out the type ourselves, since build_array_type
  418. thinks the type is incomplete. */
  419. layout_type (type);
  420. decl = build_decl (input_location, VAR_DECL, decl_name, type);
  421. TREE_STATIC (decl) = 1;
  422. IDENTIFIER_GLOBAL_VALUE (decl_name) = decl;
  423. }
  424. return decl;
  425. }
  426. }
  427. /* Get the pointer value at the INDEX'th element of the constant pool. */
  428. tree
  429. build_ref_from_constant_pool (int index)
  430. {
  431. tree i;
  432. tree d = TYPE_CPOOL_DATA_REF (output_class);
  433. if (d == NULL_TREE)
  434. d = build_constant_data_ref (flag_indirect_classes);
  435. i = build_int_cst (NULL_TREE, index);
  436. d = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (d)), d, i,
  437. NULL_TREE, NULL_TREE);
  438. return d;
  439. }
  440. /* Build an initializer for the constants field of the current constant pool.
  441. Should only be called at top-level, since it may emit declarations. */
  442. tree
  443. build_constants_constructor (void)
  444. {
  445. CPool *outgoing_cpool = cpool_for_class (current_class);
  446. tree tags_value, data_value;
  447. tree cons;
  448. vec<constructor_elt, va_gc> *v = NULL;
  449. int i;
  450. vec<constructor_elt, va_gc> *tags = NULL;
  451. vec<constructor_elt, va_gc> *data = NULL;
  452. constructor_elt *t = NULL;
  453. constructor_elt *d = NULL;
  454. if (outgoing_cpool->count > 0)
  455. {
  456. int c = outgoing_cpool->count;
  457. vec_safe_grow_cleared (tags, c);
  458. vec_safe_grow_cleared (data, c);
  459. t = &(*tags)[c-1];
  460. d = &(*data)[c-1];
  461. }
  462. #define CONSTRUCTOR_PREPEND_VALUE(E, V) E->value = V, E--
  463. for (i = outgoing_cpool->count; --i > 0; )
  464. switch (outgoing_cpool->tags[i] & ~CONSTANT_LazyFlag)
  465. {
  466. case CONSTANT_None: /* The second half of a Double or Long on a
  467. 32-bit target. */
  468. case CONSTANT_Fieldref:
  469. case CONSTANT_NameAndType:
  470. case CONSTANT_Float:
  471. case CONSTANT_Integer:
  472. case CONSTANT_Double:
  473. case CONSTANT_Long:
  474. case CONSTANT_Methodref:
  475. case CONSTANT_InterfaceMethodref:
  476. {
  477. unsigned HOST_WIDE_INT temp = outgoing_cpool->data[i].w;
  478. /* Make sure that on a big-endian machine with 64-bit
  479. pointers this 32-bit jint appears in the first word.
  480. FIXME: This is a kludge. The field we're initializing is
  481. not a scalar but a union, and that's how we should
  482. represent it in the compiler. We should fix this. */
  483. if (BYTES_BIG_ENDIAN)
  484. temp <<= ((POINTER_SIZE > 32) ? POINTER_SIZE - 32 : 0);
  485. CONSTRUCTOR_PREPEND_VALUE (t, get_tag_node (outgoing_cpool->tags[i]));
  486. CONSTRUCTOR_PREPEND_VALUE (d, build_int_cst (ptr_type_node, temp));
  487. }
  488. break;
  489. case CONSTANT_Class:
  490. case CONSTANT_String:
  491. case CONSTANT_Unicode:
  492. case CONSTANT_Utf8:
  493. CONSTRUCTOR_PREPEND_VALUE (t, get_tag_node (outgoing_cpool->tags[i]));
  494. CONSTRUCTOR_PREPEND_VALUE (d, build_utf8_ref (outgoing_cpool->data[i].t));
  495. break;
  496. default:
  497. gcc_assert (false);
  498. }
  499. #undef CONSTRUCTOR_PREPEND_VALUE
  500. if (outgoing_cpool->count > 0)
  501. {
  502. tree data_decl, tags_decl, tags_type;
  503. tree max_index = build_int_cst (sizetype, outgoing_cpool->count - 1);
  504. tree index_type = build_index_type (max_index);
  505. tree tem;
  506. /* Add dummy 0'th element of constant pool. */
  507. gcc_assert (t == tags->address ());
  508. gcc_assert (d == data->address ());
  509. t->value = get_tag_node (0);
  510. d->value = null_pointer_node;
  511. /* Change the type of the decl to have the proper array size.
  512. ??? Make sure to transition the old type-pointer-to list to this
  513. new type to not invalidate all build address expressions. */
  514. data_decl = build_constant_data_ref (false);
  515. tem = TYPE_POINTER_TO (TREE_TYPE (data_decl));
  516. if (!tem)
  517. tem = build_pointer_type (TREE_TYPE (data_decl));
  518. TYPE_POINTER_TO (TREE_TYPE (data_decl)) = NULL_TREE;
  519. TREE_TYPE (data_decl) = build_array_type (ptr_type_node, index_type);
  520. TYPE_POINTER_TO (TREE_TYPE (data_decl)) = tem;
  521. DECL_INITIAL (data_decl) = build_constructor (TREE_TYPE (data_decl), data);
  522. DECL_SIZE (data_decl) = TYPE_SIZE (TREE_TYPE (data_decl));
  523. DECL_SIZE_UNIT (data_decl) = TYPE_SIZE_UNIT (TREE_TYPE (data_decl));
  524. rest_of_decl_compilation (data_decl, 1, 0);
  525. data_value = build_address_of (data_decl);
  526. tags_type = build_array_type (unsigned_byte_type_node, index_type);
  527. tags_decl = build_decl (input_location,
  528. VAR_DECL, mangled_classname ("_CT_",
  529. current_class),
  530. tags_type);
  531. TREE_STATIC (tags_decl) = 1;
  532. DECL_INITIAL (tags_decl) = build_constructor (tags_type, tags);
  533. rest_of_decl_compilation (tags_decl, 1, 0);
  534. tags_value = build_address_of (tags_decl);
  535. }
  536. else
  537. {
  538. data_value = null_pointer_node;
  539. tags_value = null_pointer_node;
  540. }
  541. START_RECORD_CONSTRUCTOR (v, constants_type_node);
  542. PUSH_FIELD_VALUE (v, "size",
  543. build_int_cst (NULL_TREE, outgoing_cpool->count));
  544. PUSH_FIELD_VALUE (v, "tags", tags_value);
  545. PUSH_FIELD_VALUE (v, "data", data_value);
  546. FINISH_RECORD_CONSTRUCTOR (cons, v, constants_type_node);
  547. return cons;
  548. }
  549. #include "gt-java-constants.h"