interpret.cc 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950
  1. // interpret.cc - Code for the interpreter
  2. /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 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. /* Author: Kresten Krab Thorup <krab@gnu.org> */
  8. #include <config.h>
  9. #include <platform.h>
  10. #pragma implementation "java-interp.h"
  11. #include <jvm.h>
  12. #include <java-cpool.h>
  13. #include <java-interp.h>
  14. #include <java/lang/System.h>
  15. #include <java/lang/String.h>
  16. #include <java/lang/Integer.h>
  17. #include <java/lang/Long.h>
  18. #include <java/lang/StringBuffer.h>
  19. #include <java/lang/Class.h>
  20. #include <java/lang/reflect/Modifier.h>
  21. #include <java/lang/InternalError.h>
  22. #include <java/lang/NullPointerException.h>
  23. #include <java/lang/ArithmeticException.h>
  24. #include <java/lang/IncompatibleClassChangeError.h>
  25. #include <java/lang/InstantiationException.h>
  26. #include <java/lang/Thread.h>
  27. #include <java-insns.h>
  28. #include <java-signal.h>
  29. #include <java/lang/ClassFormatError.h>
  30. #include <execution.h>
  31. #include <java/lang/reflect/Modifier.h>
  32. #include <jvmti.h>
  33. #include "jvmti-int.h"
  34. #include <gnu/gcj/jvmti/Breakpoint.h>
  35. #include <gnu/gcj/jvmti/BreakpointManager.h>
  36. // Execution engine for interpreted code.
  37. _Jv_InterpreterEngine _Jv_soleInterpreterEngine;
  38. #include <stdlib.h>
  39. using namespace gcj;
  40. static void throw_internal_error (const char *msg)
  41. __attribute__ ((__noreturn__));
  42. static void throw_incompatible_class_change_error (jstring msg)
  43. __attribute__ ((__noreturn__));
  44. static void throw_null_pointer_exception ()
  45. __attribute__ ((__noreturn__));
  46. static void throw_class_format_error (jstring msg)
  47. __attribute__ ((__noreturn__));
  48. static void throw_class_format_error (const char *msg)
  49. __attribute__ ((__noreturn__));
  50. static void find_catch_location (jthrowable, jthread, jmethodID *, jlong *);
  51. // A macro to facilitate JVMTI exception reporting
  52. #define REPORT_EXCEPTION(Jthrowable) \
  53. do { \
  54. if (JVMTI_REQUESTED_EVENT (Exception)) \
  55. _Jv_ReportJVMTIExceptionThrow (Jthrowable); \
  56. } \
  57. while (0)
  58. #ifdef DIRECT_THREADED
  59. // Lock to ensure that methods are not compiled concurrently.
  60. // We could use a finer-grained lock here, however it is not safe to use
  61. // the Class monitor as user code in another thread could hold it.
  62. static _Jv_Mutex_t compile_mutex;
  63. // See class ThreadCountAdjuster and REWRITE_INSN for how this is
  64. // used.
  65. _Jv_Mutex_t _Jv_InterpMethod::rewrite_insn_mutex;
  66. void
  67. _Jv_InitInterpreter()
  68. {
  69. _Jv_MutexInit (&compile_mutex);
  70. _Jv_MutexInit (&_Jv_InterpMethod::rewrite_insn_mutex);
  71. }
  72. #else
  73. void _Jv_InitInterpreter() {}
  74. #endif
  75. // The breakpoint instruction. For the direct threaded case,
  76. // _Jv_InterpMethod::compile will initialize breakpoint_insn
  77. // the first time it is called.
  78. #ifdef DIRECT_THREADED
  79. insn_slot _Jv_InterpMethod::bp_insn_slot;
  80. pc_t _Jv_InterpMethod::breakpoint_insn = NULL;
  81. #else
  82. unsigned char _Jv_InterpMethod::bp_insn_opcode
  83. = static_cast<unsigned char> (op_breakpoint);
  84. pc_t _Jv_InterpMethod::breakpoint_insn = &_Jv_InterpMethod::bp_insn_opcode;
  85. #endif
  86. extern "C" double __ieee754_fmod (double,double);
  87. static inline void dupx (_Jv_word *sp, int n, int x)
  88. {
  89. // first "slide" n+x elements n to the right
  90. int top = n-1;
  91. for (int i = 0; i < n+x; i++)
  92. {
  93. sp[(top-i)] = sp[(top-i)-n];
  94. }
  95. // next, copy the n top elements, n+x down
  96. for (int i = 0; i < n; i++)
  97. {
  98. sp[top-(n+x)-i] = sp[top-i];
  99. }
  100. }
  101. // Used to convert from floating types to integral types.
  102. template<typename TO, typename FROM>
  103. static inline TO
  104. convert (FROM val, TO min, TO max)
  105. {
  106. TO ret;
  107. if (val >= (FROM) max)
  108. ret = max;
  109. else if (val <= (FROM) min)
  110. ret = min;
  111. else if (val != val)
  112. ret = 0;
  113. else
  114. ret = (TO) val;
  115. return ret;
  116. }
  117. #define PUSHA(V) (sp++)->o = (V)
  118. #define PUSHI(V) (sp++)->i = (V)
  119. #define PUSHF(V) (sp++)->f = (V)
  120. #if SIZEOF_VOID_P == 8
  121. # define PUSHL(V) (sp->l = (V), sp += 2)
  122. # define PUSHD(V) (sp->d = (V), sp += 2)
  123. #else
  124. # define PUSHL(V) do { _Jv_word2 w2; w2.l=(V); \
  125. (sp++)->ia[0] = w2.ia[0]; \
  126. (sp++)->ia[0] = w2.ia[1]; } while (0)
  127. # define PUSHD(V) do { _Jv_word2 w2; w2.d=(V); \
  128. (sp++)->ia[0] = w2.ia[0]; \
  129. (sp++)->ia[0] = w2.ia[1]; } while (0)
  130. #endif
  131. #define POPA() ((--sp)->o)
  132. #define POPI() ((jint) (--sp)->i) // cast since it may be promoted
  133. #define POPF() ((jfloat) (--sp)->f)
  134. #if SIZEOF_VOID_P == 8
  135. # define POPL() (sp -= 2, (jlong) sp->l)
  136. # define POPD() (sp -= 2, (jdouble) sp->d)
  137. #else
  138. # define POPL() ({ _Jv_word2 w2; \
  139. w2.ia[1] = (--sp)->ia[0]; \
  140. w2.ia[0] = (--sp)->ia[0]; w2.l; })
  141. # define POPD() ({ _Jv_word2 w2; \
  142. w2.ia[1] = (--sp)->ia[0]; \
  143. w2.ia[0] = (--sp)->ia[0]; w2.d; })
  144. #endif
  145. #define LOADA(I) (sp++)->o = locals[I].o
  146. #define LOADI(I) (sp++)->i = locals[I].i
  147. #define LOADF(I) (sp++)->f = locals[I].f
  148. #if SIZEOF_VOID_P == 8
  149. # define LOADL(I) (sp->l = locals[I].l, sp += 2)
  150. # define LOADD(I) (sp->d = locals[I].d, sp += 2)
  151. #else
  152. # define LOADL(I) do { jint __idx = (I); \
  153. (sp++)->ia[0] = locals[__idx].ia[0]; \
  154. (sp++)->ia[0] = locals[__idx+1].ia[0]; \
  155. } while (0)
  156. # define LOADD(I) LOADL(I)
  157. #endif
  158. #define STOREA(I) \
  159. do \
  160. { \
  161. jint __idx = (I); \
  162. DEBUG_LOCALS_INSN (__idx, 'o'); \
  163. locals[__idx].o = (--sp)->o; \
  164. } \
  165. while (0)
  166. #define STOREI(I) \
  167. do \
  168. { \
  169. jint __idx = (I); \
  170. DEBUG_LOCALS_INSN (__idx, 'i'); \
  171. locals[__idx].i = (--sp)->i; \
  172. } while (0)
  173. #define STOREF(I) \
  174. do \
  175. { \
  176. jint __idx = (I); \
  177. DEBUG_LOCALS_INSN (__idx, 'f'); \
  178. locals[__idx].f = (--sp)->f; \
  179. } \
  180. while (0)
  181. #if SIZEOF_VOID_P == 8
  182. # define STOREL(I) \
  183. do \
  184. { \
  185. jint __idx = (I); \
  186. DEBUG_LOCALS_INSN (__idx, 'l'); \
  187. DEBUG_LOCALS_INSN (__idx + 1, 'x'); \
  188. (sp -= 2, locals[__idx].l = sp->l); \
  189. } \
  190. while (0)
  191. # define STORED(I) \
  192. do \
  193. { \
  194. jint __idx = (I); \
  195. DEBUG_LOCALS_INSN (__idx, 'd'); \
  196. DEBUG_LOCALS_INSN (__idx + 1, 'x'); \
  197. (sp -= 2, locals[__idx].d = sp->d); \
  198. } \
  199. while (0)
  200. #else
  201. # define STOREL(I) \
  202. do \
  203. { \
  204. jint __idx = (I); \
  205. DEBUG_LOCALS_INSN (__idx, 'l'); \
  206. DEBUG_LOCALS_INSN (__idx + 1, 'x'); \
  207. locals[__idx + 1].ia[0] = (--sp)->ia[0]; \
  208. locals[__idx].ia[0] = (--sp)->ia[0]; \
  209. } \
  210. while (0)
  211. # define STORED(I) \
  212. do { \
  213. jint __idx = (I); \
  214. DEBUG_LOCALS_INSN (__idx, 'd'); \
  215. DEBUG_LOCALS_INSN (__idx + 1, 'x'); \
  216. locals[__idx + 1].ia[0] = (--sp)->ia[0]; \
  217. locals[__idx].ia[0] = (--sp)->ia[0]; \
  218. } while (0)
  219. #endif
  220. #define PEEKI(I) (locals+(I))->i
  221. #define PEEKA(I) (locals+(I))->o
  222. #define POKEI(I,V) \
  223. do \
  224. { \
  225. jint __idx = (I); \
  226. DEBUG_LOCALS_INSN (__idx, 'i'); \
  227. ((locals + __idx)->i = (V)); \
  228. } \
  229. while (0)
  230. #define BINOPI(OP) { \
  231. jint value2 = POPI(); \
  232. jint value1 = POPI(); \
  233. PUSHI(value1 OP value2); \
  234. }
  235. #define BINOPF(OP) { \
  236. jfloat value2 = POPF(); \
  237. jfloat value1 = POPF(); \
  238. PUSHF(value1 OP value2); \
  239. }
  240. #define BINOPL(OP) { \
  241. jlong value2 = POPL(); \
  242. jlong value1 = POPL(); \
  243. PUSHL(value1 OP value2); \
  244. }
  245. #define BINOPD(OP) { \
  246. jdouble value2 = POPD(); \
  247. jdouble value1 = POPD(); \
  248. PUSHD(value1 OP value2); \
  249. }
  250. static inline jint
  251. get1s (unsigned char* loc)
  252. {
  253. return *(signed char*)loc;
  254. }
  255. static inline jint
  256. get1u (unsigned char* loc)
  257. {
  258. return *loc;
  259. }
  260. static inline jint
  261. get2s(unsigned char* loc)
  262. {
  263. return (((jint)*(signed char*)loc) << 8) | ((jint)*(loc+1));
  264. }
  265. static inline jint
  266. get2u (unsigned char* loc)
  267. {
  268. return (((jint)(*loc)) << 8) | ((jint)*(loc+1));
  269. }
  270. static jint
  271. get4 (unsigned char* loc)
  272. {
  273. return (((jint)(loc[0])) << 24)
  274. | (((jint)(loc[1])) << 16)
  275. | (((jint)(loc[2])) << 8)
  276. | (((jint)(loc[3])) << 0);
  277. }
  278. #define SAVE_PC() frame_desc.pc = pc
  279. // We used to define this conditionally, depending on HANDLE_SEGV.
  280. // However, that runs into a problem if a chunk in low memory is
  281. // mapped and we try to look at a field near the end of a large
  282. // object. See PR 26858 for details. It is, most likely, relatively
  283. // inexpensive to simply do this check always.
  284. #define NULLCHECK(X) \
  285. do { SAVE_PC(); if ((X)==NULL) throw_null_pointer_exception (); } while (0)
  286. // Note that we can still conditionally define NULLARRAYCHECK, since
  287. // we know that all uses of an array will first reference the length
  288. // field, which is first -- and thus will trigger a SEGV.
  289. #ifdef HANDLE_SEGV
  290. #define NULLARRAYCHECK(X) SAVE_PC()
  291. #else
  292. #define NULLARRAYCHECK(X) \
  293. do \
  294. { \
  295. SAVE_PC(); \
  296. if ((X) == NULL) { throw_null_pointer_exception (); } \
  297. } while (0)
  298. #endif
  299. #define ARRAYBOUNDSCHECK(array, index) \
  300. do \
  301. { \
  302. if (((unsigned) index) >= (unsigned) (array->length)) \
  303. _Jv_ThrowBadArrayIndex (index); \
  304. } while (0)
  305. void
  306. _Jv_InterpMethod::run_normal (ffi_cif *,
  307. void *ret,
  308. INTERP_FFI_RAW_TYPE *args,
  309. void *__this)
  310. {
  311. _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
  312. run (ret, args, _this);
  313. }
  314. void
  315. _Jv_InterpMethod::run_normal_debug (ffi_cif *,
  316. void *ret,
  317. INTERP_FFI_RAW_TYPE *args,
  318. void *__this)
  319. {
  320. _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
  321. run_debug (ret, args, _this);
  322. }
  323. void
  324. _Jv_InterpMethod::run_synch_object (ffi_cif *,
  325. void *ret,
  326. INTERP_FFI_RAW_TYPE *args,
  327. void *__this)
  328. {
  329. _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
  330. jobject rcv = (jobject) args[0].ptr;
  331. JvSynchronize mutex (rcv);
  332. run (ret, args, _this);
  333. }
  334. void
  335. _Jv_InterpMethod::run_synch_object_debug (ffi_cif *,
  336. void *ret,
  337. INTERP_FFI_RAW_TYPE *args,
  338. void *__this)
  339. {
  340. _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
  341. jobject rcv = (jobject) args[0].ptr;
  342. JvSynchronize mutex (rcv);
  343. run_debug (ret, args, _this);
  344. }
  345. void
  346. _Jv_InterpMethod::run_class (ffi_cif *,
  347. void *ret,
  348. INTERP_FFI_RAW_TYPE *args,
  349. void *__this)
  350. {
  351. _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
  352. _Jv_InitClass (_this->defining_class);
  353. run (ret, args, _this);
  354. }
  355. void
  356. _Jv_InterpMethod::run_class_debug (ffi_cif *,
  357. void *ret,
  358. INTERP_FFI_RAW_TYPE *args,
  359. void *__this)
  360. {
  361. _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
  362. _Jv_InitClass (_this->defining_class);
  363. run_debug (ret, args, _this);
  364. }
  365. void
  366. _Jv_InterpMethod::run_synch_class (ffi_cif *,
  367. void *ret,
  368. INTERP_FFI_RAW_TYPE *args,
  369. void *__this)
  370. {
  371. _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
  372. jclass sync = _this->defining_class;
  373. _Jv_InitClass (sync);
  374. JvSynchronize mutex (sync);
  375. run (ret, args, _this);
  376. }
  377. void
  378. _Jv_InterpMethod::run_synch_class_debug (ffi_cif *,
  379. void *ret,
  380. INTERP_FFI_RAW_TYPE *args,
  381. void *__this)
  382. {
  383. _Jv_InterpMethod *_this = (_Jv_InterpMethod *) __this;
  384. jclass sync = _this->defining_class;
  385. _Jv_InitClass (sync);
  386. JvSynchronize mutex (sync);
  387. run_debug (ret, args, _this);
  388. }
  389. #ifdef DIRECT_THREADED
  390. // "Compile" a method by turning it from bytecode to direct-threaded
  391. // code.
  392. void
  393. _Jv_InterpMethod::compile (const void * const *insn_targets)
  394. {
  395. insn_slot *insns = NULL;
  396. int next = 0;
  397. unsigned char *codestart = bytecode ();
  398. unsigned char *end = codestart + code_length;
  399. _Jv_word *pool_data = defining_class->constants.data;
  400. #define SET_ONE(Field, Value) \
  401. do \
  402. { \
  403. if (first_pass) \
  404. ++next; \
  405. else \
  406. insns[next++].Field = Value; \
  407. } \
  408. while (0)
  409. #define SET_INSN(Value) SET_ONE (insn, (void *) Value)
  410. #define SET_INT(Value) SET_ONE (int_val, Value)
  411. #define SET_DATUM(Value) SET_ONE (datum, Value)
  412. // Map from bytecode PC to slot in INSNS.
  413. int *pc_mapping = (int *) __builtin_alloca (sizeof (int) * code_length);
  414. for (int i = 0; i < code_length; ++i)
  415. pc_mapping[i] = -1;
  416. for (int i = 0; i < 2; ++i)
  417. {
  418. jboolean first_pass = i == 0;
  419. if (! first_pass)
  420. {
  421. insns = (insn_slot *) _Jv_AllocBytes (sizeof (insn_slot) * next);
  422. number_insn_slots = next;
  423. next = 0;
  424. }
  425. unsigned char *pc = codestart;
  426. while (pc < end)
  427. {
  428. int base_pc_val = pc - codestart;
  429. if (first_pass)
  430. pc_mapping[base_pc_val] = next;
  431. java_opcode opcode = (java_opcode) *pc++;
  432. // Just elide NOPs.
  433. if (opcode == op_nop)
  434. continue;
  435. SET_INSN (insn_targets[opcode]);
  436. switch (opcode)
  437. {
  438. case op_nop:
  439. case op_aconst_null:
  440. case op_iconst_m1:
  441. case op_iconst_0:
  442. case op_iconst_1:
  443. case op_iconst_2:
  444. case op_iconst_3:
  445. case op_iconst_4:
  446. case op_iconst_5:
  447. case op_lconst_0:
  448. case op_lconst_1:
  449. case op_fconst_0:
  450. case op_fconst_1:
  451. case op_fconst_2:
  452. case op_dconst_0:
  453. case op_dconst_1:
  454. case op_iload_0:
  455. case op_iload_1:
  456. case op_iload_2:
  457. case op_iload_3:
  458. case op_lload_0:
  459. case op_lload_1:
  460. case op_lload_2:
  461. case op_lload_3:
  462. case op_fload_0:
  463. case op_fload_1:
  464. case op_fload_2:
  465. case op_fload_3:
  466. case op_dload_0:
  467. case op_dload_1:
  468. case op_dload_2:
  469. case op_dload_3:
  470. case op_aload_0:
  471. case op_aload_1:
  472. case op_aload_2:
  473. case op_aload_3:
  474. case op_iaload:
  475. case op_laload:
  476. case op_faload:
  477. case op_daload:
  478. case op_aaload:
  479. case op_baload:
  480. case op_caload:
  481. case op_saload:
  482. case op_istore_0:
  483. case op_istore_1:
  484. case op_istore_2:
  485. case op_istore_3:
  486. case op_lstore_0:
  487. case op_lstore_1:
  488. case op_lstore_2:
  489. case op_lstore_3:
  490. case op_fstore_0:
  491. case op_fstore_1:
  492. case op_fstore_2:
  493. case op_fstore_3:
  494. case op_dstore_0:
  495. case op_dstore_1:
  496. case op_dstore_2:
  497. case op_dstore_3:
  498. case op_astore_0:
  499. case op_astore_1:
  500. case op_astore_2:
  501. case op_astore_3:
  502. case op_iastore:
  503. case op_lastore:
  504. case op_fastore:
  505. case op_dastore:
  506. case op_aastore:
  507. case op_bastore:
  508. case op_castore:
  509. case op_sastore:
  510. case op_pop:
  511. case op_pop2:
  512. case op_dup:
  513. case op_dup_x1:
  514. case op_dup_x2:
  515. case op_dup2:
  516. case op_dup2_x1:
  517. case op_dup2_x2:
  518. case op_swap:
  519. case op_iadd:
  520. case op_isub:
  521. case op_imul:
  522. case op_idiv:
  523. case op_irem:
  524. case op_ishl:
  525. case op_ishr:
  526. case op_iushr:
  527. case op_iand:
  528. case op_ior:
  529. case op_ixor:
  530. case op_ladd:
  531. case op_lsub:
  532. case op_lmul:
  533. case op_ldiv:
  534. case op_lrem:
  535. case op_lshl:
  536. case op_lshr:
  537. case op_lushr:
  538. case op_land:
  539. case op_lor:
  540. case op_lxor:
  541. case op_fadd:
  542. case op_fsub:
  543. case op_fmul:
  544. case op_fdiv:
  545. case op_frem:
  546. case op_dadd:
  547. case op_dsub:
  548. case op_dmul:
  549. case op_ddiv:
  550. case op_drem:
  551. case op_ineg:
  552. case op_i2b:
  553. case op_i2c:
  554. case op_i2s:
  555. case op_lneg:
  556. case op_fneg:
  557. case op_dneg:
  558. case op_i2l:
  559. case op_i2f:
  560. case op_i2d:
  561. case op_l2i:
  562. case op_l2f:
  563. case op_l2d:
  564. case op_f2i:
  565. case op_f2l:
  566. case op_f2d:
  567. case op_d2i:
  568. case op_d2l:
  569. case op_d2f:
  570. case op_lcmp:
  571. case op_fcmpl:
  572. case op_fcmpg:
  573. case op_dcmpl:
  574. case op_dcmpg:
  575. case op_monitorenter:
  576. case op_monitorexit:
  577. case op_ireturn:
  578. case op_lreturn:
  579. case op_freturn:
  580. case op_dreturn:
  581. case op_areturn:
  582. case op_return:
  583. case op_athrow:
  584. case op_arraylength:
  585. // No argument, nothing else to do.
  586. break;
  587. case op_bipush:
  588. SET_INT (get1s (pc));
  589. ++pc;
  590. break;
  591. case op_ldc:
  592. {
  593. int index = get1u (pc);
  594. ++pc;
  595. // For an unresolved class we want to delay resolution
  596. // until execution.
  597. if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
  598. {
  599. --next;
  600. SET_INSN (insn_targets[int (op_jsr_w) + 1]);
  601. SET_INT (index);
  602. }
  603. else
  604. SET_DATUM (pool_data[index].o);
  605. }
  606. break;
  607. case op_ret:
  608. case op_iload:
  609. case op_lload:
  610. case op_fload:
  611. case op_dload:
  612. case op_aload:
  613. case op_istore:
  614. case op_lstore:
  615. case op_fstore:
  616. case op_dstore:
  617. case op_astore:
  618. case op_newarray:
  619. SET_INT (get1u (pc));
  620. ++pc;
  621. break;
  622. case op_iinc:
  623. SET_INT (get1u (pc));
  624. SET_INT (get1s (pc + 1));
  625. pc += 2;
  626. break;
  627. case op_ldc_w:
  628. {
  629. int index = get2u (pc);
  630. pc += 2;
  631. // For an unresolved class we want to delay resolution
  632. // until execution.
  633. if (defining_class->constants.tags[index] == JV_CONSTANT_Class)
  634. {
  635. --next;
  636. SET_INSN (insn_targets[int (op_jsr_w) + 1]);
  637. SET_INT (index);
  638. }
  639. else
  640. SET_DATUM (pool_data[index].o);
  641. }
  642. break;
  643. case op_ldc2_w:
  644. {
  645. int index = get2u (pc);
  646. pc += 2;
  647. SET_DATUM (&pool_data[index]);
  648. }
  649. break;
  650. case op_sipush:
  651. SET_INT (get2s (pc));
  652. pc += 2;
  653. break;
  654. case op_new:
  655. case op_getstatic:
  656. case op_getfield:
  657. case op_putfield:
  658. case op_putstatic:
  659. case op_anewarray:
  660. case op_instanceof:
  661. case op_checkcast:
  662. case op_invokespecial:
  663. case op_invokestatic:
  664. case op_invokevirtual:
  665. SET_INT (get2u (pc));
  666. pc += 2;
  667. break;
  668. case op_multianewarray:
  669. SET_INT (get2u (pc));
  670. SET_INT (get1u (pc + 2));
  671. pc += 3;
  672. break;
  673. case op_jsr:
  674. case op_ifeq:
  675. case op_ifne:
  676. case op_iflt:
  677. case op_ifge:
  678. case op_ifgt:
  679. case op_ifle:
  680. case op_if_icmpeq:
  681. case op_if_icmpne:
  682. case op_if_icmplt:
  683. case op_if_icmpge:
  684. case op_if_icmpgt:
  685. case op_if_icmple:
  686. case op_if_acmpeq:
  687. case op_if_acmpne:
  688. case op_ifnull:
  689. case op_ifnonnull:
  690. case op_goto:
  691. {
  692. int offset = get2s (pc);
  693. pc += 2;
  694. int new_pc = base_pc_val + offset;
  695. bool orig_was_goto = opcode == op_goto;
  696. // Thread jumps. We limit the loop count; this lets
  697. // us avoid infinite loops if the bytecode contains
  698. // such. `10' is arbitrary.
  699. int count = 10;
  700. while (codestart[new_pc] == op_goto && count-- > 0)
  701. new_pc += get2s (&codestart[new_pc + 1]);
  702. // If the jump takes us to a `return' instruction and
  703. // the original branch was an unconditional goto, then
  704. // we hoist the return.
  705. opcode = (java_opcode) codestart[new_pc];
  706. if (orig_was_goto
  707. && (opcode == op_ireturn || opcode == op_lreturn
  708. || opcode == op_freturn || opcode == op_dreturn
  709. || opcode == op_areturn || opcode == op_return))
  710. {
  711. --next;
  712. SET_INSN (insn_targets[opcode]);
  713. }
  714. else
  715. SET_DATUM (&insns[pc_mapping[new_pc]]);
  716. }
  717. break;
  718. case op_tableswitch:
  719. {
  720. while ((pc - codestart) % 4 != 0)
  721. ++pc;
  722. jint def = get4 (pc);
  723. SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
  724. pc += 4;
  725. int low = get4 (pc);
  726. SET_INT (low);
  727. pc += 4;
  728. int high = get4 (pc);
  729. SET_INT (high);
  730. pc += 4;
  731. for (int i = low; i <= high; ++i)
  732. {
  733. SET_DATUM (&insns[pc_mapping[base_pc_val + get4 (pc)]]);
  734. pc += 4;
  735. }
  736. }
  737. break;
  738. case op_lookupswitch:
  739. {
  740. while ((pc - codestart) % 4 != 0)
  741. ++pc;
  742. jint def = get4 (pc);
  743. SET_DATUM (&insns[pc_mapping[base_pc_val + def]]);
  744. pc += 4;
  745. jint npairs = get4 (pc);
  746. pc += 4;
  747. SET_INT (npairs);
  748. while (npairs-- > 0)
  749. {
  750. jint match = get4 (pc);
  751. jint offset = get4 (pc + 4);
  752. SET_INT (match);
  753. SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
  754. pc += 8;
  755. }
  756. }
  757. break;
  758. case op_invokeinterface:
  759. {
  760. jint index = get2u (pc);
  761. pc += 2;
  762. // We ignore the next two bytes.
  763. pc += 2;
  764. SET_INT (index);
  765. }
  766. break;
  767. case op_wide:
  768. {
  769. opcode = (java_opcode) get1u (pc);
  770. pc += 1;
  771. jint val = get2u (pc);
  772. pc += 2;
  773. // We implement narrow and wide instructions using the
  774. // same code in the interpreter. So we rewrite the
  775. // instruction slot here.
  776. if (! first_pass)
  777. insns[next - 1].insn = (void *) insn_targets[opcode];
  778. SET_INT (val);
  779. if (opcode == op_iinc)
  780. {
  781. SET_INT (get2s (pc));
  782. pc += 2;
  783. }
  784. }
  785. break;
  786. case op_jsr_w:
  787. case op_goto_w:
  788. {
  789. jint offset = get4 (pc);
  790. pc += 4;
  791. SET_DATUM (&insns[pc_mapping[base_pc_val + offset]]);
  792. }
  793. break;
  794. // Some "can't happen" cases that we include for
  795. // error-checking purposes.
  796. case op_putfield_1:
  797. case op_putfield_2:
  798. case op_putfield_4:
  799. case op_putfield_8:
  800. case op_putfield_a:
  801. case op_putstatic_1:
  802. case op_putstatic_2:
  803. case op_putstatic_4:
  804. case op_putstatic_8:
  805. case op_putstatic_a:
  806. case op_getfield_1:
  807. case op_getfield_2s:
  808. case op_getfield_2u:
  809. case op_getfield_4:
  810. case op_getfield_8:
  811. case op_getfield_a:
  812. case op_getstatic_1:
  813. case op_getstatic_2s:
  814. case op_getstatic_2u:
  815. case op_getstatic_4:
  816. case op_getstatic_8:
  817. case op_getstatic_a:
  818. case op_breakpoint:
  819. default:
  820. // Fail somehow.
  821. break;
  822. }
  823. }
  824. }
  825. // Now update exceptions.
  826. _Jv_InterpException *exc = exceptions ();
  827. for (int i = 0; i < exc_count; ++i)
  828. {
  829. exc[i].start_pc.p = &insns[pc_mapping[exc[i].start_pc.i]];
  830. exc[i].end_pc.p = &insns[pc_mapping[exc[i].end_pc.i]];
  831. exc[i].handler_pc.p = &insns[pc_mapping[exc[i].handler_pc.i]];
  832. // FIXME: resolve_pool_entry can throw - we shouldn't be doing this
  833. // during compilation.
  834. jclass handler
  835. = (_Jv_Linker::resolve_pool_entry (defining_class,
  836. exc[i].handler_type.i)).clazz;
  837. exc[i].handler_type.p = handler;
  838. }
  839. // Translate entries in the LineNumberTable from bytecode PC's to direct
  840. // threaded interpreter instruction values.
  841. for (int i = 0; i < line_table_len; i++)
  842. {
  843. int byte_pc = line_table[i].bytecode_pc;
  844. // It isn't worth throwing an exception if this table is
  845. // corrupted, but at the same time we don't want a crash.
  846. if (byte_pc < 0 || byte_pc >= code_length)
  847. byte_pc = 0;
  848. line_table[i].pc = &insns[pc_mapping[byte_pc]];
  849. }
  850. prepared = insns;
  851. // Now remap the variable table for this method.
  852. for (int i = 0; i < local_var_table_len; ++i)
  853. {
  854. int start_byte = local_var_table[i].bytecode_pc;
  855. if (start_byte < 0 || start_byte >= code_length)
  856. start_byte = 0;
  857. jlocation start = pc_mapping[start_byte];
  858. int end_byte = start_byte + local_var_table[i].length;
  859. if (end_byte < 0)
  860. end_byte = 0;
  861. jlocation end = ((end_byte >= code_length)
  862. ? number_insn_slots
  863. : pc_mapping[end_byte]);
  864. local_var_table[i].pc = &insns[start];
  865. local_var_table[i].length = end - start + 1;
  866. }
  867. if (breakpoint_insn == NULL)
  868. {
  869. bp_insn_slot.insn = const_cast<void *> (insn_targets[op_breakpoint]);
  870. breakpoint_insn = &bp_insn_slot;
  871. }
  872. }
  873. #endif /* DIRECT_THREADED */
  874. /* Run the given method.
  875. When args is NULL, don't run anything -- just compile it. */
  876. void
  877. _Jv_InterpMethod::run (void *retp, INTERP_FFI_RAW_TYPE *args,
  878. _Jv_InterpMethod *meth)
  879. {
  880. #undef __GCJ_DEBUG
  881. #undef DEBUG_LOCALS_INSN
  882. #define DEBUG_LOCALS_INSN(s, t) do {} while (0)
  883. #include "interpret-run.cc"
  884. }
  885. void
  886. _Jv_InterpMethod::run_debug (void *retp, INTERP_FFI_RAW_TYPE *args,
  887. _Jv_InterpMethod *meth)
  888. {
  889. #define __GCJ_DEBUG
  890. #undef DEBUG_LOCALS_INSN
  891. #define DEBUG_LOCALS_INSN(s, t) \
  892. do \
  893. { \
  894. frame_desc.locals_type[s] = t; \
  895. } \
  896. while (0)
  897. #include "interpret-run.cc"
  898. }
  899. static void
  900. throw_internal_error (const char *msg)
  901. {
  902. jthrowable t = new java::lang::InternalError (JvNewStringLatin1 (msg));
  903. REPORT_EXCEPTION (t);
  904. throw t;
  905. }
  906. static void
  907. throw_incompatible_class_change_error (jstring msg)
  908. {
  909. jthrowable t = new java::lang::IncompatibleClassChangeError (msg);
  910. REPORT_EXCEPTION (t);
  911. throw t;
  912. }
  913. static void
  914. throw_null_pointer_exception ()
  915. {
  916. jthrowable t = new java::lang::NullPointerException;
  917. REPORT_EXCEPTION (t);
  918. throw t;
  919. }
  920. /* Look up source code line number for given bytecode (or direct threaded
  921. interpreter) PC. */
  922. int
  923. _Jv_InterpMethod::get_source_line(pc_t mpc)
  924. {
  925. int line = line_table_len > 0 ? line_table[0].line : -1;
  926. for (int i = 1; i < line_table_len; i++)
  927. if (line_table[i].pc > mpc)
  928. break;
  929. else
  930. line = line_table[i].line;
  931. return line;
  932. }
  933. /** Do static initialization for fields with a constant initializer */
  934. void
  935. _Jv_InitField (jobject obj, jclass klass, int index)
  936. {
  937. using namespace java::lang::reflect;
  938. if (obj != 0 && klass == 0)
  939. klass = obj->getClass ();
  940. if (!_Jv_IsInterpretedClass (klass))
  941. return;
  942. _Jv_InterpClass *iclass = (_Jv_InterpClass*)klass->aux_info;
  943. _Jv_Field * field = (&klass->fields[0]) + index;
  944. if (index > klass->field_count)
  945. throw_internal_error ("field out of range");
  946. int init = iclass->field_initializers[index];
  947. if (init == 0)
  948. return;
  949. _Jv_Constants *pool = &klass->constants;
  950. int tag = pool->tags[init];
  951. if (! field->isResolved ())
  952. throw_internal_error ("initializing unresolved field");
  953. if (obj==0 && ((field->flags & Modifier::STATIC) == 0))
  954. throw_internal_error ("initializing non-static field with no object");
  955. void *addr = 0;
  956. if ((field->flags & Modifier::STATIC) != 0)
  957. addr = (void*) field->u.addr;
  958. else
  959. addr = (void*) (((char*)obj) + field->u.boffset);
  960. switch (tag)
  961. {
  962. case JV_CONSTANT_String:
  963. {
  964. jstring str;
  965. str = _Jv_NewStringUtf8Const (pool->data[init].utf8);
  966. pool->data[init].string = str;
  967. pool->tags[init] = JV_CONSTANT_ResolvedString;
  968. }
  969. /* fall through */
  970. case JV_CONSTANT_ResolvedString:
  971. if (! (field->type == &java::lang::String::class$
  972. || field->type == &java::lang::Class::class$))
  973. throw_class_format_error ("string initialiser to non-string field");
  974. *(jstring*)addr = pool->data[init].string;
  975. break;
  976. case JV_CONSTANT_Integer:
  977. {
  978. int value = pool->data[init].i;
  979. if (field->type == JvPrimClass (boolean))
  980. *(jboolean*)addr = (jboolean)value;
  981. else if (field->type == JvPrimClass (byte))
  982. *(jbyte*)addr = (jbyte)value;
  983. else if (field->type == JvPrimClass (char))
  984. *(jchar*)addr = (jchar)value;
  985. else if (field->type == JvPrimClass (short))
  986. *(jshort*)addr = (jshort)value;
  987. else if (field->type == JvPrimClass (int))
  988. *(jint*)addr = (jint)value;
  989. else
  990. throw_class_format_error ("erroneous field initializer");
  991. }
  992. break;
  993. case JV_CONSTANT_Long:
  994. if (field->type != JvPrimClass (long))
  995. throw_class_format_error ("erroneous field initializer");
  996. *(jlong*)addr = _Jv_loadLong (&pool->data[init]);
  997. break;
  998. case JV_CONSTANT_Float:
  999. if (field->type != JvPrimClass (float))
  1000. throw_class_format_error ("erroneous field initializer");
  1001. *(jfloat*)addr = pool->data[init].f;
  1002. break;
  1003. case JV_CONSTANT_Double:
  1004. if (field->type != JvPrimClass (double))
  1005. throw_class_format_error ("erroneous field initializer");
  1006. *(jdouble*)addr = _Jv_loadDouble (&pool->data[init]);
  1007. break;
  1008. default:
  1009. throw_class_format_error ("erroneous field initializer");
  1010. }
  1011. }
  1012. inline static unsigned char*
  1013. skip_one_type (unsigned char* ptr)
  1014. {
  1015. int ch = *ptr++;
  1016. while (ch == '[')
  1017. {
  1018. ch = *ptr++;
  1019. }
  1020. if (ch == 'L')
  1021. {
  1022. do { ch = *ptr++; } while (ch != ';');
  1023. }
  1024. return ptr;
  1025. }
  1026. static ffi_type*
  1027. get_ffi_type_from_signature (unsigned char* ptr)
  1028. {
  1029. switch (*ptr)
  1030. {
  1031. case 'L':
  1032. case '[':
  1033. return &ffi_type_pointer;
  1034. break;
  1035. case 'Z':
  1036. // On some platforms a bool is a byte, on others an int.
  1037. if (sizeof (jboolean) == sizeof (jbyte))
  1038. return &ffi_type_sint8;
  1039. else
  1040. {
  1041. JvAssert (sizeof (jbyte) == sizeof (jint));
  1042. return &ffi_type_sint32;
  1043. }
  1044. break;
  1045. case 'B':
  1046. return &ffi_type_sint8;
  1047. break;
  1048. case 'C':
  1049. return &ffi_type_uint16;
  1050. break;
  1051. case 'S':
  1052. return &ffi_type_sint16;
  1053. break;
  1054. case 'I':
  1055. return &ffi_type_sint32;
  1056. break;
  1057. case 'J':
  1058. return &ffi_type_sint64;
  1059. break;
  1060. case 'F':
  1061. return &ffi_type_float;
  1062. break;
  1063. case 'D':
  1064. return &ffi_type_double;
  1065. break;
  1066. case 'V':
  1067. return &ffi_type_void;
  1068. break;
  1069. }
  1070. throw_internal_error ("unknown type in signature");
  1071. }
  1072. /* this function yields the number of actual arguments, that is, if the
  1073. * function is non-static, then one is added to the number of elements
  1074. * found in the signature */
  1075. int
  1076. _Jv_count_arguments (_Jv_Utf8Const *signature,
  1077. jboolean staticp)
  1078. {
  1079. unsigned char *ptr = (unsigned char*) signature->chars();
  1080. int arg_count = staticp ? 0 : 1;
  1081. /* first, count number of arguments */
  1082. // skip '('
  1083. ptr++;
  1084. // count args
  1085. while (*ptr != ')')
  1086. {
  1087. ptr = skip_one_type (ptr);
  1088. arg_count += 1;
  1089. }
  1090. return arg_count;
  1091. }
  1092. /* This beast will build a cif, given the signature. Memory for
  1093. * the cif itself and for the argument types must be allocated by the
  1094. * caller.
  1095. */
  1096. int
  1097. _Jv_init_cif (_Jv_Utf8Const* signature,
  1098. int arg_count,
  1099. jboolean staticp,
  1100. ffi_cif *cif,
  1101. ffi_type **arg_types,
  1102. ffi_type **rtype_p)
  1103. {
  1104. unsigned char *ptr = (unsigned char*) signature->chars();
  1105. int arg_index = 0; // arg number
  1106. int item_count = 0; // stack-item count
  1107. // setup receiver
  1108. if (!staticp)
  1109. {
  1110. arg_types[arg_index++] = &ffi_type_pointer;
  1111. item_count += 1;
  1112. }
  1113. // skip '('
  1114. ptr++;
  1115. // assign arg types
  1116. while (*ptr != ')')
  1117. {
  1118. arg_types[arg_index++] = get_ffi_type_from_signature (ptr);
  1119. if (*ptr == 'J' || *ptr == 'D')
  1120. item_count += 2;
  1121. else
  1122. item_count += 1;
  1123. ptr = skip_one_type (ptr);
  1124. }
  1125. // skip ')'
  1126. ptr++;
  1127. ffi_type *rtype = get_ffi_type_from_signature (ptr);
  1128. ptr = skip_one_type (ptr);
  1129. if (ptr != (unsigned char*)signature->chars() + signature->len())
  1130. throw_internal_error ("did not find end of signature");
  1131. ffi_abi cabi = FFI_DEFAULT_ABI;
  1132. #if defined (X86_WIN32) && !defined (__CYGWIN__)
  1133. if (!staticp)
  1134. cabi = FFI_THISCALL;
  1135. #endif
  1136. if (ffi_prep_cif (cif, cabi,
  1137. arg_count, rtype, arg_types) != FFI_OK)
  1138. throw_internal_error ("ffi_prep_cif failed");
  1139. if (rtype_p != NULL)
  1140. *rtype_p = rtype;
  1141. return item_count;
  1142. }
  1143. /* we put this one here, and not in interpret.cc because it
  1144. * calls the utility routines _Jv_count_arguments
  1145. * which are static to this module. The following struct defines the
  1146. * layout we use for the stubs, it's only used in the ncode method. */
  1147. #if FFI_NATIVE_RAW_API
  1148. # define FFI_PREP_RAW_CLOSURE ffi_prep_raw_closure_loc
  1149. # define FFI_RAW_SIZE ffi_raw_size
  1150. typedef struct {
  1151. ffi_raw_closure closure;
  1152. _Jv_ClosureList list;
  1153. ffi_cif cif;
  1154. ffi_type *arg_types[0];
  1155. } ncode_closure;
  1156. typedef void (*ffi_closure_fun) (ffi_cif*,void*,INTERP_FFI_RAW_TYPE*,void*);
  1157. #else
  1158. # define FFI_PREP_RAW_CLOSURE ffi_prep_java_raw_closure_loc
  1159. # define FFI_RAW_SIZE ffi_java_raw_size
  1160. typedef struct {
  1161. ffi_java_raw_closure closure;
  1162. _Jv_ClosureList list;
  1163. ffi_cif cif;
  1164. ffi_type *arg_types[0];
  1165. } ncode_closure;
  1166. typedef void (*ffi_closure_fun) (ffi_cif*,void*,ffi_java_raw*,void*);
  1167. #endif
  1168. void *
  1169. _Jv_InterpMethod::ncode (jclass klass)
  1170. {
  1171. using namespace java::lang::reflect;
  1172. if (self->ncode != 0)
  1173. return self->ncode;
  1174. jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
  1175. int arg_count = _Jv_count_arguments (self->signature, staticp);
  1176. void *code;
  1177. ncode_closure *closure =
  1178. (ncode_closure*)ffi_closure_alloc (sizeof (ncode_closure)
  1179. + arg_count * sizeof (ffi_type*),
  1180. &code);
  1181. closure->list.registerClosure (klass, closure);
  1182. _Jv_init_cif (self->signature,
  1183. arg_count,
  1184. staticp,
  1185. &closure->cif,
  1186. &closure->arg_types[0],
  1187. NULL);
  1188. ffi_closure_fun fun;
  1189. args_raw_size = FFI_RAW_SIZE (&closure->cif);
  1190. JvAssert ((self->accflags & Modifier::NATIVE) == 0);
  1191. if ((self->accflags & Modifier::SYNCHRONIZED) != 0)
  1192. {
  1193. if (staticp)
  1194. {
  1195. if (JVMTI::enabled)
  1196. fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class_debug;
  1197. else
  1198. fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_class;
  1199. }
  1200. else
  1201. {
  1202. if (JVMTI::enabled)
  1203. fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object_debug;
  1204. else
  1205. fun = (ffi_closure_fun)&_Jv_InterpMethod::run_synch_object;
  1206. }
  1207. }
  1208. else
  1209. {
  1210. if (staticp)
  1211. {
  1212. if (JVMTI::enabled)
  1213. fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class_debug;
  1214. else
  1215. fun = (ffi_closure_fun)&_Jv_InterpMethod::run_class;
  1216. }
  1217. else
  1218. {
  1219. if (JVMTI::enabled)
  1220. fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal_debug;
  1221. else
  1222. fun = (ffi_closure_fun)&_Jv_InterpMethod::run_normal;
  1223. }
  1224. }
  1225. FFI_PREP_RAW_CLOSURE (&closure->closure,
  1226. &closure->cif,
  1227. fun,
  1228. (void*)this,
  1229. code);
  1230. self->ncode = code;
  1231. return self->ncode;
  1232. }
  1233. /* Find the index of the given insn in the array of insn slots
  1234. for this method. Returns -1 if not found. */
  1235. jlong
  1236. _Jv_InterpMethod::insn_index (pc_t pc)
  1237. {
  1238. jlong left = 0;
  1239. #ifdef DIRECT_THREADED
  1240. jlong right = number_insn_slots;
  1241. pc_t insns = prepared;
  1242. #else
  1243. jlong right = code_length;
  1244. pc_t insns = bytecode ();
  1245. #endif
  1246. while (right >= 0)
  1247. {
  1248. jlong mid = (left + right) / 2;
  1249. if (&insns[mid] == pc)
  1250. return mid;
  1251. if (pc < &insns[mid])
  1252. right = mid - 1;
  1253. else
  1254. left = mid + 1;
  1255. }
  1256. return -1;
  1257. }
  1258. // Method to check if an exception is caught at some location in a method
  1259. // (meth). Returns true if this method (meth) contains a catch block for the
  1260. // exception (ex). False otherwise. If there is a catch block, it sets the pc
  1261. // to the location of the beginning of the catch block.
  1262. jboolean
  1263. _Jv_InterpMethod::check_handler (pc_t *pc, _Jv_InterpMethod *meth,
  1264. java::lang::Throwable *ex)
  1265. {
  1266. #ifdef DIRECT_THREADED
  1267. void *logical_pc = (void *) ((insn_slot *) (*pc) - 1);
  1268. #else
  1269. int logical_pc = (*pc) - 1 - meth->bytecode ();
  1270. #endif
  1271. _Jv_InterpException *exc = meth->exceptions ();
  1272. jclass exc_class = ex->getClass ();
  1273. for (int i = 0; i < meth->exc_count; i++)
  1274. {
  1275. if (PCVAL (exc[i].start_pc) <= logical_pc
  1276. && logical_pc < PCVAL (exc[i].end_pc))
  1277. {
  1278. #ifdef DIRECT_THREADED
  1279. jclass handler = (jclass) exc[i].handler_type.p;
  1280. #else
  1281. jclass handler = NULL;
  1282. if (exc[i].handler_type.i != 0)
  1283. handler
  1284. = (_Jv_Linker::resolve_pool_entry (meth->defining_class,
  1285. exc[i].handler_type.i)).clazz;
  1286. #endif /* DIRECT_THREADED */
  1287. if (handler == NULL || handler->isAssignableFrom (exc_class))
  1288. {
  1289. #ifdef DIRECT_THREADED
  1290. (*pc) = (insn_slot *) exc[i].handler_pc.p;
  1291. #else
  1292. (*pc) = meth->bytecode () + exc[i].handler_pc.i;
  1293. #endif /* DIRECT_THREADED */
  1294. return true;
  1295. }
  1296. }
  1297. }
  1298. return false;
  1299. }
  1300. void
  1301. _Jv_InterpMethod::get_line_table (jlong& start, jlong& end,
  1302. jintArray& line_numbers,
  1303. jlongArray& code_indices)
  1304. {
  1305. #ifdef DIRECT_THREADED
  1306. /* For the DIRECT_THREADED case, if the method has not yet been
  1307. * compiled, the linetable will change to insn slots instead of
  1308. * bytecode PCs. It is probably easiest, in this case, to simply
  1309. * compile the method and guarantee that we are using insn
  1310. * slots.
  1311. */
  1312. _Jv_CompileMethod (this);
  1313. if (line_table_len > 0)
  1314. {
  1315. start = 0;
  1316. end = number_insn_slots;
  1317. line_numbers = JvNewIntArray (line_table_len);
  1318. code_indices = JvNewLongArray (line_table_len);
  1319. jint* lines = elements (line_numbers);
  1320. jlong* indices = elements (code_indices);
  1321. for (int i = 0; i < line_table_len; ++i)
  1322. {
  1323. lines[i] = line_table[i].line;
  1324. indices[i] = insn_index (line_table[i].pc);
  1325. }
  1326. }
  1327. #else // !DIRECT_THREADED
  1328. if (line_table_len > 0)
  1329. {
  1330. start = 0;
  1331. end = code_length;
  1332. line_numbers = JvNewIntArray (line_table_len);
  1333. code_indices = JvNewLongArray (line_table_len);
  1334. jint* lines = elements (line_numbers);
  1335. jlong* indices = elements (code_indices);
  1336. for (int i = 0; i < line_table_len; ++i)
  1337. {
  1338. lines[i] = line_table[i].line;
  1339. indices[i] = (jlong) line_table[i].bytecode_pc;
  1340. }
  1341. }
  1342. #endif // !DIRECT_THREADED
  1343. }
  1344. int
  1345. _Jv_InterpMethod::get_local_var_table (char **name, char **sig,
  1346. char **generic_sig, jlong *startloc,
  1347. jint *length, jint *slot,
  1348. int table_slot)
  1349. {
  1350. #ifdef DIRECT_THREADED
  1351. _Jv_CompileMethod (this);
  1352. #endif
  1353. if (local_var_table == NULL)
  1354. return -2;
  1355. if (table_slot >= local_var_table_len)
  1356. return -1;
  1357. else
  1358. {
  1359. *name = local_var_table[table_slot].name;
  1360. *sig = local_var_table[table_slot].descriptor;
  1361. *generic_sig = local_var_table[table_slot].descriptor;
  1362. #ifdef DIRECT_THREADED
  1363. *startloc = insn_index (local_var_table[table_slot].pc);
  1364. #else
  1365. *startloc = static_cast<jlong> (local_var_table[table_slot].bytecode_pc);
  1366. #endif
  1367. *length = static_cast<jint> (local_var_table[table_slot].length);
  1368. *slot = static_cast<jint> (local_var_table[table_slot].slot);
  1369. }
  1370. return local_var_table_len - table_slot - 1;
  1371. }
  1372. pc_t
  1373. _Jv_InterpMethod::install_break (jlong index)
  1374. {
  1375. return set_insn (index, breakpoint_insn);
  1376. }
  1377. pc_t
  1378. _Jv_InterpMethod::get_insn (jlong index)
  1379. {
  1380. pc_t code;
  1381. #ifdef DIRECT_THREADED
  1382. if (index >= number_insn_slots || index < 0)
  1383. return NULL;
  1384. code = prepared;
  1385. #else // !DIRECT_THREADED
  1386. if (index >= code_length || index < 0)
  1387. return NULL;
  1388. code = reinterpret_cast<pc_t> (bytecode ());
  1389. #endif // !DIRECT_THREADED
  1390. return &code[index];
  1391. }
  1392. pc_t
  1393. _Jv_InterpMethod::set_insn (jlong index, pc_t insn)
  1394. {
  1395. #ifdef DIRECT_THREADED
  1396. if (index >= number_insn_slots || index < 0)
  1397. return NULL;
  1398. pc_t code = prepared;
  1399. code[index].insn = insn->insn;
  1400. #else // !DIRECT_THREADED
  1401. if (index >= code_length || index < 0)
  1402. return NULL;
  1403. pc_t code = reinterpret_cast<pc_t> (bytecode ());
  1404. code[index] = *insn;
  1405. #endif // !DIRECT_THREADED
  1406. return &code[index];
  1407. }
  1408. bool
  1409. _Jv_InterpMethod::breakpoint_at (jlong index)
  1410. {
  1411. pc_t insn = get_insn (index);
  1412. if (insn != NULL)
  1413. {
  1414. #ifdef DIRECT_THREADED
  1415. return (insn->insn == breakpoint_insn->insn);
  1416. #else
  1417. pc_t code = reinterpret_cast<pc_t> (bytecode ());
  1418. return (code[index] == bp_insn_opcode);
  1419. #endif
  1420. }
  1421. return false;
  1422. }
  1423. void *
  1424. _Jv_JNIMethod::ncode (jclass klass)
  1425. {
  1426. using namespace java::lang::reflect;
  1427. if (self->ncode != 0)
  1428. return self->ncode;
  1429. jboolean staticp = (self->accflags & Modifier::STATIC) != 0;
  1430. int arg_count = _Jv_count_arguments (self->signature, staticp);
  1431. void *code;
  1432. ncode_closure *closure =
  1433. (ncode_closure*)ffi_closure_alloc (sizeof (ncode_closure)
  1434. + arg_count * sizeof (ffi_type*),
  1435. &code);
  1436. closure->list.registerClosure (klass, closure);
  1437. ffi_type *rtype;
  1438. _Jv_init_cif (self->signature,
  1439. arg_count,
  1440. staticp,
  1441. &closure->cif,
  1442. &closure->arg_types[0],
  1443. &rtype);
  1444. ffi_closure_fun fun;
  1445. args_raw_size = FFI_RAW_SIZE (&closure->cif);
  1446. // Initialize the argument types and CIF that represent the actual
  1447. // underlying JNI function.
  1448. int extra_args = 1;
  1449. if ((self->accflags & Modifier::STATIC))
  1450. ++extra_args;
  1451. jni_arg_types = (ffi_type **) _Jv_AllocBytes ((extra_args + arg_count)
  1452. * sizeof (ffi_type *));
  1453. int offset = 0;
  1454. jni_arg_types[offset++] = &ffi_type_pointer;
  1455. if ((self->accflags & Modifier::STATIC))
  1456. jni_arg_types[offset++] = &ffi_type_pointer;
  1457. memcpy (&jni_arg_types[offset], &closure->arg_types[0],
  1458. arg_count * sizeof (ffi_type *));
  1459. if (ffi_prep_cif (&jni_cif, _Jv_platform_ffi_abi,
  1460. extra_args + arg_count, rtype,
  1461. jni_arg_types) != FFI_OK)
  1462. throw_internal_error ("ffi_prep_cif failed for JNI function");
  1463. JvAssert ((self->accflags & Modifier::NATIVE) != 0);
  1464. // FIXME: for now we assume that all native methods for
  1465. // interpreted code use JNI.
  1466. fun = (ffi_closure_fun) &_Jv_JNIMethod::call;
  1467. FFI_PREP_RAW_CLOSURE (&closure->closure,
  1468. &closure->cif,
  1469. fun,
  1470. (void*) this,
  1471. code);
  1472. self->ncode = code;
  1473. return self->ncode;
  1474. }
  1475. static void
  1476. throw_class_format_error (jstring msg)
  1477. {
  1478. jthrowable t = (msg
  1479. ? new java::lang::ClassFormatError (msg)
  1480. : new java::lang::ClassFormatError);
  1481. REPORT_EXCEPTION (t);
  1482. throw t;
  1483. }
  1484. static void
  1485. throw_class_format_error (const char *msg)
  1486. {
  1487. throw_class_format_error (JvNewStringLatin1 (msg));
  1488. }
  1489. /* This function finds the method and location where the exception EXC
  1490. is caught in the stack frame. On return, it sets CATCH_METHOD and
  1491. CATCH_LOCATION with the method and location where the catch will
  1492. occur. If the exception is not caught, these are set to 0.
  1493. This function should only be used with the __GCJ_DEBUG interpreter. */
  1494. static void
  1495. find_catch_location (::java::lang::Throwable *exc, jthread thread,
  1496. jmethodID *catch_method, jlong *catch_loc)
  1497. {
  1498. *catch_method = 0;
  1499. *catch_loc = 0;
  1500. _Jv_InterpFrame *frame
  1501. = reinterpret_cast<_Jv_InterpFrame *> (thread->interp_frame);
  1502. while (frame != NULL)
  1503. {
  1504. pc_t pc = frame->get_pc ();
  1505. _Jv_InterpMethod *imeth
  1506. = reinterpret_cast<_Jv_InterpMethod *> (frame->self);
  1507. if (imeth->check_handler (&pc, imeth, exc))
  1508. {
  1509. // This method handles the exception.
  1510. *catch_method = imeth->get_method ();
  1511. *catch_loc = imeth->insn_index (pc);
  1512. return;
  1513. }
  1514. frame = frame->next_interp;
  1515. }
  1516. }
  1517. /* This method handles JVMTI notifications of thrown exceptions. It
  1518. calls find_catch_location to figure out where the exception is
  1519. caught (if it is caught).
  1520. Like find_catch_location, this should only be called with the
  1521. __GCJ_DEBUG interpreter. Since a few exceptions occur outside the
  1522. interpreter proper, it is important to not call this function
  1523. without checking JVMTI_REQUESTED_EVENT(Exception) first. */
  1524. void
  1525. _Jv_ReportJVMTIExceptionThrow (jthrowable ex)
  1526. {
  1527. jthread thread = ::java::lang::Thread::currentThread ();
  1528. _Jv_Frame *frame = reinterpret_cast<_Jv_Frame *> (thread->frame);
  1529. jmethodID throw_meth = frame->self->get_method ();
  1530. jlocation throw_loc = -1;
  1531. if (frame->frame_type == frame_interpreter)
  1532. {
  1533. _Jv_InterpFrame * iframe
  1534. = reinterpret_cast<_Jv_InterpFrame *> (frame);
  1535. _Jv_InterpMethod *imeth
  1536. = reinterpret_cast<_Jv_InterpMethod *> (frame->self);
  1537. throw_loc = imeth->insn_index (iframe->get_pc ());
  1538. }
  1539. jlong catch_loc;
  1540. jmethodID catch_method;
  1541. find_catch_location (ex, thread, &catch_method, &catch_loc);
  1542. _Jv_JVMTI_PostEvent (JVMTI_EVENT_EXCEPTION, thread,
  1543. _Jv_GetCurrentJNIEnv (), throw_meth, throw_loc,
  1544. ex, catch_method, catch_loc);
  1545. }
  1546. void
  1547. _Jv_InterpreterEngine::do_verify (jclass klass)
  1548. {
  1549. _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
  1550. for (int i = 0; i < klass->method_count; i++)
  1551. {
  1552. using namespace java::lang::reflect;
  1553. _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
  1554. _Jv_ushort accflags = klass->methods[i].accflags;
  1555. if ((accflags & (Modifier::NATIVE | Modifier::ABSTRACT)) == 0)
  1556. {
  1557. _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
  1558. _Jv_VerifyMethod (im);
  1559. }
  1560. }
  1561. }
  1562. void
  1563. _Jv_InterpreterEngine::do_create_ncode (jclass klass)
  1564. {
  1565. _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
  1566. for (int i = 0; i < klass->method_count; i++)
  1567. {
  1568. // Just skip abstract methods. This is particularly important
  1569. // because we don't resize the interpreted_methods array when
  1570. // miranda methods are added to it.
  1571. if ((klass->methods[i].accflags
  1572. & java::lang::reflect::Modifier::ABSTRACT)
  1573. != 0)
  1574. continue;
  1575. _Jv_MethodBase *imeth = iclass->interpreted_methods[i];
  1576. if ((klass->methods[i].accflags & java::lang::reflect::Modifier::NATIVE)
  1577. != 0)
  1578. {
  1579. // You might think we could use a virtual `ncode' method in
  1580. // the _Jv_MethodBase and unify the native and non-native
  1581. // cases. Well, we can't, because we don't allocate these
  1582. // objects using `new', and thus they don't get a vtable.
  1583. _Jv_JNIMethod *jnim = reinterpret_cast<_Jv_JNIMethod *> (imeth);
  1584. klass->methods[i].ncode = jnim->ncode (klass);
  1585. }
  1586. else if (imeth != 0) // it could be abstract
  1587. {
  1588. _Jv_InterpMethod *im = reinterpret_cast<_Jv_InterpMethod *> (imeth);
  1589. klass->methods[i].ncode = im->ncode (klass);
  1590. }
  1591. }
  1592. }
  1593. _Jv_ClosureList **
  1594. _Jv_InterpreterEngine::do_get_closure_list (jclass klass)
  1595. {
  1596. _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
  1597. if (!iclass->closures)
  1598. iclass->closures = _Jv_ClosureListFinalizer ();
  1599. return iclass->closures;
  1600. }
  1601. void
  1602. _Jv_InterpreterEngine::do_allocate_static_fields (jclass klass,
  1603. int pointer_size,
  1604. int other_size)
  1605. {
  1606. _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
  1607. // Splitting the allocations here lets us scan reference fields and
  1608. // avoid scanning non-reference fields. How reference fields are
  1609. // scanned is a bit tricky: we allocate using _Jv_AllocRawObj, which
  1610. // means that this memory will be scanned conservatively (same
  1611. // difference, since we know all the contents here are pointers).
  1612. // Then we put pointers into this memory into the 'fields'
  1613. // structure. Most of these are interior pointers, which is ok (but
  1614. // even so the pointer to the first reference field will be used and
  1615. // that is not an interior pointer). The 'fields' array is also
  1616. // allocated with _Jv_AllocRawObj (see defineclass.cc), so it will
  1617. // be scanned. A pointer to this array is held by Class and thus
  1618. // seen by the collector.
  1619. char *reference_fields = (char *) _Jv_AllocRawObj (pointer_size);
  1620. char *non_reference_fields = (char *) _Jv_AllocBytes (other_size);
  1621. for (int i = 0; i < klass->field_count; i++)
  1622. {
  1623. _Jv_Field *field = &klass->fields[i];
  1624. if ((field->flags & java::lang::reflect::Modifier::STATIC) == 0)
  1625. continue;
  1626. char *base = field->isRef() ? reference_fields : non_reference_fields;
  1627. field->u.addr = base + field->u.boffset;
  1628. if (iclass->field_initializers[i] != 0)
  1629. {
  1630. _Jv_Linker::resolve_field (field, klass->loader);
  1631. _Jv_InitField (0, klass, i);
  1632. }
  1633. }
  1634. // Now we don't need the field_initializers anymore, so let the
  1635. // collector get rid of it.
  1636. iclass->field_initializers = 0;
  1637. }
  1638. _Jv_ResolvedMethod *
  1639. _Jv_InterpreterEngine::do_resolve_method (_Jv_Method *method, jclass klass,
  1640. jboolean staticp)
  1641. {
  1642. int arg_count = _Jv_count_arguments (method->signature, staticp);
  1643. _Jv_ResolvedMethod* result = (_Jv_ResolvedMethod*)
  1644. _Jv_AllocBytes (sizeof (_Jv_ResolvedMethod)
  1645. + arg_count*sizeof (ffi_type*));
  1646. result->stack_item_count
  1647. = _Jv_init_cif (method->signature,
  1648. arg_count,
  1649. staticp,
  1650. &result->cif,
  1651. &result->arg_types[0],
  1652. NULL);
  1653. result->method = method;
  1654. result->klass = klass;
  1655. return result;
  1656. }
  1657. void
  1658. _Jv_InterpreterEngine::do_post_miranda_hook (jclass klass)
  1659. {
  1660. _Jv_InterpClass *iclass = (_Jv_InterpClass *) klass->aux_info;
  1661. for (int i = 0; i < klass->method_count; i++)
  1662. {
  1663. // Just skip abstract methods. This is particularly important
  1664. // because we don't resize the interpreted_methods array when
  1665. // miranda methods are added to it.
  1666. if ((klass->methods[i].accflags
  1667. & java::lang::reflect::Modifier::ABSTRACT)
  1668. != 0)
  1669. continue;
  1670. // Miranda method additions mean that the `methods' array moves.
  1671. // We cache a pointer into this array, so we have to update.
  1672. iclass->interpreted_methods[i]->self = &klass->methods[i];
  1673. }
  1674. }
  1675. #ifdef DIRECT_THREADED
  1676. void
  1677. _Jv_CompileMethod (_Jv_InterpMethod* method)
  1678. {
  1679. if (method->prepared == NULL)
  1680. {
  1681. if (JVMTI::enabled)
  1682. _Jv_InterpMethod::run_debug (NULL, NULL, method);
  1683. else
  1684. _Jv_InterpMethod::run (NULL, NULL, method);
  1685. }
  1686. }
  1687. #endif // DIRECT_THREADED