execution.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // execution.h - Execution engines. -*- c++ -*-
  2. /* Copyright (C) 2004, 2006, 2007 Free Software Foundation
  3. This file is part of libgcj.
  4. This software is copyrighted work licensed under the terms of the
  5. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  6. details. */
  7. #ifndef __JAVA_EXECUTION_H__
  8. #define __JAVA_EXECUTION_H__
  9. // This represents one execution engine. Note that we use function
  10. // pointers and not virtual methods to avoid calls to
  11. // __cxa_call_unexpected and the like.
  12. struct _Jv_ExecutionEngine
  13. {
  14. public:
  15. void (*unregister) (jclass);
  16. // FIXME: probably should handle this elsewhere, see how
  17. // interpreter does it.
  18. bool (*need_resolve_string_fields) ();
  19. void (*verify) (jclass);
  20. void (*allocate_static_fields) (jclass, int, int);
  21. void (*allocate_field_initializers) (jclass);
  22. void (*create_ncode) (jclass);
  23. _Jv_ResolvedMethod *(*resolve_method) (_Jv_Method *, jclass,
  24. jboolean);
  25. void (*post_miranda_hook) (jclass);
  26. _Jv_ClosureList **(*get_closure_list) (jclass);
  27. };
  28. // This handles gcj-compiled code except that compiled with
  29. // -findirect-classes.
  30. struct _Jv_CompiledEngine : public _Jv_ExecutionEngine
  31. {
  32. public:
  33. static void do_unregister (jclass)
  34. {
  35. }
  36. static bool do_need_resolve_string_fields ()
  37. {
  38. return true;
  39. }
  40. static void do_verify (jclass klass)
  41. {
  42. _Jv_Linker::verify_type_assertions (klass);
  43. }
  44. static _Jv_ResolvedMethod *do_resolve_method (_Jv_Method *, jclass,
  45. jboolean)
  46. {
  47. return NULL;
  48. }
  49. static void do_allocate_static_fields (jclass,
  50. int,
  51. int)
  52. {
  53. }
  54. static void do_allocate_field_initializers (jclass)
  55. {
  56. }
  57. static void do_create_ncode (jclass)
  58. {
  59. // Not needed.
  60. }
  61. static void do_post_miranda_hook (jclass)
  62. {
  63. // Not needed.
  64. }
  65. static _Jv_ClosureList **do_get_closure_list (jclass)
  66. {
  67. return NULL;
  68. }
  69. _Jv_CompiledEngine ()
  70. {
  71. unregister = do_unregister;
  72. need_resolve_string_fields = do_need_resolve_string_fields;
  73. verify = do_verify;
  74. allocate_static_fields = do_allocate_static_fields;
  75. allocate_field_initializers = do_allocate_field_initializers;
  76. create_ncode = do_create_ncode;
  77. resolve_method = do_resolve_method;
  78. post_miranda_hook = do_post_miranda_hook;
  79. get_closure_list = do_get_closure_list;
  80. }
  81. // These operators make it so we don't have to link in libstdc++.
  82. void *operator new (size_t bytes)
  83. {
  84. return _Jv_Malloc(bytes);
  85. }
  86. void operator delete (void *mem)
  87. {
  88. _Jv_Free(mem);
  89. }
  90. };
  91. class _Jv_IndirectCompiledClass
  92. {
  93. public:
  94. void **field_initializers;
  95. _Jv_ClosureList **closures;
  96. };
  97. // This handles gcj-compiled code compiled with -findirect-classes.
  98. struct _Jv_IndirectCompiledEngine : public _Jv_CompiledEngine
  99. {
  100. _Jv_IndirectCompiledEngine () : _Jv_CompiledEngine ()
  101. {
  102. allocate_static_fields = do_allocate_static_fields;
  103. allocate_field_initializers = do_allocate_field_initializers;
  104. get_closure_list = do_get_closure_list;
  105. }
  106. static _Jv_IndirectCompiledClass *get_aux_info (jclass klass)
  107. {
  108. _Jv_IndirectCompiledClass *aux =
  109. (_Jv_IndirectCompiledClass*)klass->aux_info;
  110. if (!aux)
  111. {
  112. aux = (_Jv_IndirectCompiledClass*)
  113. _Jv_AllocRawObj (sizeof (_Jv_IndirectCompiledClass));
  114. klass->aux_info = aux;
  115. }
  116. return aux;
  117. }
  118. static void do_allocate_field_initializers (jclass klass)
  119. {
  120. _Jv_IndirectCompiledClass *aux = get_aux_info (klass);
  121. if (!aux)
  122. {
  123. aux = (_Jv_IndirectCompiledClass*)
  124. _Jv_AllocRawObj (sizeof (_Jv_IndirectCompiledClass));
  125. klass->aux_info = aux;
  126. }
  127. aux->field_initializers = (void **)_Jv_Malloc (klass->field_count
  128. * sizeof (void*));
  129. for (int i = 0; i < klass->field_count; i++)
  130. {
  131. _Jv_Field *field = &klass->fields[i];
  132. if (field->flags & java::lang::reflect::Modifier::STATIC)
  133. {
  134. aux->field_initializers[i] = field->u.addr;
  135. field->u.addr = NULL;
  136. }
  137. }
  138. }
  139. static void do_allocate_static_fields (jclass klass,
  140. int pointer_size,
  141. int other_size)
  142. {
  143. // Splitting the allocations here lets us scan reference fields
  144. // and avoid scanning non-reference fields.
  145. char *reference_fields = (char *) _Jv_AllocRawObj (pointer_size);
  146. char *non_reference_fields = (char *) _Jv_AllocBytes (other_size);
  147. _Jv_IndirectCompiledClass *aux
  148. = (_Jv_IndirectCompiledClass*)klass->aux_info;
  149. for (int i = 0; i < klass->field_count; i++)
  150. {
  151. _Jv_Field *field = &klass->fields[i];
  152. if ((field->flags & java::lang::reflect::Modifier::STATIC) == 0)
  153. continue;
  154. char *base = field->isRef() ? reference_fields : non_reference_fields;
  155. field->u.addr = base + field->u.boffset;
  156. if (aux->field_initializers[i])
  157. {
  158. int field_size;
  159. if (! field->isRef ())
  160. field_size = field->type->size ();
  161. else
  162. field_size = sizeof (jobject);
  163. memcpy (field->u.addr, aux->field_initializers[i], field_size);
  164. }
  165. }
  166. _Jv_Free (aux->field_initializers);
  167. }
  168. #ifdef INTERPRETER
  169. static _Jv_ClosureList **do_get_closure_list (jclass klass)
  170. {
  171. _Jv_IndirectCompiledClass *aux = get_aux_info (klass);
  172. if (!aux->closures)
  173. aux->closures = _Jv_ClosureListFinalizer ();
  174. return aux->closures;
  175. }
  176. #endif
  177. };
  178. #ifdef INTERPRETER
  179. // This handles interpreted code.
  180. class _Jv_InterpreterEngine : public _Jv_ExecutionEngine
  181. {
  182. public:
  183. static void do_verify (jclass);
  184. static void do_allocate_static_fields (jclass, int, int);
  185. static void do_create_ncode (jclass);
  186. static _Jv_ResolvedMethod *do_resolve_method (_Jv_Method *, jclass,
  187. jboolean);
  188. static bool do_need_resolve_string_fields ()
  189. {
  190. return false;
  191. }
  192. static void do_unregister(jclass klass)
  193. {
  194. _Jv_UnregisterClass(klass);
  195. }
  196. static void do_allocate_field_initializers (jclass)
  197. {
  198. }
  199. static void do_post_miranda_hook (jclass);
  200. static _Jv_ClosureList **do_get_closure_list (jclass klass);
  201. _Jv_InterpreterEngine ()
  202. {
  203. unregister = do_unregister;
  204. need_resolve_string_fields = do_need_resolve_string_fields;
  205. verify = do_verify;
  206. allocate_static_fields = do_allocate_static_fields;
  207. allocate_field_initializers = do_allocate_field_initializers;
  208. create_ncode = do_create_ncode;
  209. resolve_method = do_resolve_method;
  210. post_miranda_hook = do_post_miranda_hook;
  211. get_closure_list = do_get_closure_list;
  212. }
  213. // These operators make it so we don't have to link in libstdc++.
  214. void *operator new (size_t bytes)
  215. {
  216. return _Jv_Malloc(bytes);
  217. }
  218. void operator delete (void *mem)
  219. {
  220. _Jv_Free(mem);
  221. }
  222. };
  223. extern _Jv_InterpreterEngine _Jv_soleInterpreterEngine;
  224. #endif // INTERPRETER
  225. extern _Jv_CompiledEngine _Jv_soleCompiledEngine;
  226. extern _Jv_IndirectCompiledEngine _Jv_soleIndirectCompiledEngine;
  227. #endif // __JAVA_EXECUTION_H__