expr.c 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754
  1. /* Process expressions for the GNU compiler for the Java(TM) language.
  2. Copyright (C) 1996-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. /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
  19. #include "config.h"
  20. #include "system.h"
  21. #include "coretypes.h"
  22. #include "tm.h" /* For INT_TYPE_SIZE,
  23. TARGET_VTABLE_USES_DESCRIPTORS,
  24. BITS_PER_UNIT,
  25. MODIFY_JNI_METHOD_CALL and
  26. PARM_BOUNDARY. */
  27. #include "hash-set.h"
  28. #include "machmode.h"
  29. #include "vec.h"
  30. #include "double-int.h"
  31. #include "input.h"
  32. #include "alias.h"
  33. #include "symtab.h"
  34. #include "wide-int.h"
  35. #include "inchash.h"
  36. #include "real.h"
  37. #include "tree.h"
  38. #include "fold-const.h"
  39. #include "stringpool.h"
  40. #include "stor-layout.h"
  41. #include "flags.h"
  42. #include "java-tree.h"
  43. #include "javaop.h"
  44. #include "java-opcodes.h"
  45. #include "jcf.h"
  46. #include "java-except.h"
  47. #include "parse.h"
  48. #include "diagnostic-core.h"
  49. #include "ggc.h"
  50. #include "tree-iterator.h"
  51. #include "target.h"
  52. static void flush_quick_stack (void);
  53. static void push_value (tree);
  54. static tree pop_value (tree);
  55. static void java_stack_swap (void);
  56. static void java_stack_dup (int, int);
  57. static void build_java_athrow (tree);
  58. static void build_java_jsr (int, int);
  59. static void build_java_ret (tree);
  60. static void expand_java_multianewarray (tree, int);
  61. static void expand_java_arraystore (tree);
  62. static void expand_java_arrayload (tree);
  63. static void expand_java_array_length (void);
  64. static tree build_java_monitor (tree, tree);
  65. static void expand_java_pushc (int, tree);
  66. static void expand_java_return (tree);
  67. static void expand_load_internal (int, tree, int);
  68. static void expand_java_NEW (tree);
  69. static void expand_java_INSTANCEOF (tree);
  70. static void expand_java_CHECKCAST (tree);
  71. static void expand_iinc (unsigned int, int, int);
  72. static void expand_java_binop (tree, enum tree_code);
  73. static void note_label (int, int);
  74. static void expand_compare (enum tree_code, tree, tree, int);
  75. static void expand_test (enum tree_code, tree, int);
  76. static void expand_cond (enum tree_code, tree, int);
  77. static void expand_java_goto (int);
  78. static tree expand_java_switch (tree, int);
  79. static void expand_java_add_case (tree, int, int);
  80. static vec<tree, va_gc> *pop_arguments (tree);
  81. static void expand_invoke (int, int, int);
  82. static void expand_java_field_op (int, int, int);
  83. static void java_push_constant_from_pool (struct JCF *, int);
  84. static void java_stack_pop (int);
  85. static tree build_java_throw_out_of_bounds_exception (tree);
  86. static tree build_java_check_indexed_type (tree, tree);
  87. static unsigned char peek_opcode_at_pc (struct JCF *, int, int);
  88. static void promote_arguments (void);
  89. static void cache_cpool_data_ref (void);
  90. static GTY(()) tree operand_type[59];
  91. static GTY(()) tree methods_ident;
  92. static GTY(()) tree ncode_ident;
  93. tree dtable_ident = NULL_TREE;
  94. /* Set to nonzero value in order to emit class initialization code
  95. before static field references. */
  96. int always_initialize_class_p = 0;
  97. /* We store the stack state in two places:
  98. Within a basic block, we use the quick_stack, which is a vec of expression
  99. nodes.
  100. This is the top part of the stack; below that we use find_stack_slot.
  101. At the end of a basic block, the quick_stack must be flushed
  102. to the stack slot array (as handled by find_stack_slot).
  103. Using quick_stack generates better code (especially when
  104. compiled without optimization), because we do not have to
  105. explicitly store and load trees to temporary variables.
  106. If a variable is on the quick stack, it means the value of variable
  107. when the quick stack was last flushed. Conceptually, flush_quick_stack
  108. saves all the quick_stack elements in parallel. However, that is
  109. complicated, so it actually saves them (i.e. copies each stack value
  110. to is home virtual register) from low indexes. This allows a quick_stack
  111. element at index i (counting from the bottom of stack the) to references
  112. slot virtuals for register that are >= i, but not those that are deeper.
  113. This convention makes most operations easier. For example iadd works
  114. even when the stack contains (reg[0], reg[1]): It results in the
  115. stack containing (reg[0]+reg[1]), which is OK. However, some stack
  116. operations are more complicated. For example dup given a stack
  117. containing (reg[0]) would yield (reg[0], reg[0]), which would violate
  118. the convention, since stack value 1 would refer to a register with
  119. lower index (reg[0]), which flush_quick_stack does not safely handle.
  120. So dup cannot just add an extra element to the quick_stack, but iadd can.
  121. */
  122. static GTY(()) vec<tree, va_gc> *quick_stack;
  123. /* The physical memory page size used in this computer. See
  124. build_field_ref(). */
  125. static GTY(()) tree page_size;
  126. /* The stack pointer of the Java virtual machine.
  127. This does include the size of the quick_stack. */
  128. int stack_pointer;
  129. const unsigned char *linenumber_table;
  130. int linenumber_count;
  131. /* Largest pc so far in this method that has been passed to lookup_label. */
  132. int highest_label_pc_this_method = -1;
  133. /* Base value for this method to add to pc to get generated label. */
  134. int start_label_pc_this_method = 0;
  135. void
  136. init_expr_processing (void)
  137. {
  138. operand_type[21] = operand_type[54] = int_type_node;
  139. operand_type[22] = operand_type[55] = long_type_node;
  140. operand_type[23] = operand_type[56] = float_type_node;
  141. operand_type[24] = operand_type[57] = double_type_node;
  142. operand_type[25] = operand_type[58] = ptr_type_node;
  143. }
  144. tree
  145. java_truthvalue_conversion (tree expr)
  146. {
  147. /* It is simpler and generates better code to have only TRUTH_*_EXPR
  148. or comparison expressions as truth values at this level.
  149. This function should normally be identity for Java. */
  150. switch (TREE_CODE (expr))
  151. {
  152. case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
  153. case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
  154. case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
  155. case ORDERED_EXPR: case UNORDERED_EXPR:
  156. case TRUTH_ANDIF_EXPR:
  157. case TRUTH_ORIF_EXPR:
  158. case TRUTH_AND_EXPR:
  159. case TRUTH_OR_EXPR:
  160. case TRUTH_XOR_EXPR:
  161. case TRUTH_NOT_EXPR:
  162. case ERROR_MARK:
  163. return expr;
  164. case INTEGER_CST:
  165. return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
  166. case REAL_CST:
  167. return real_zerop (expr) ? boolean_false_node : boolean_true_node;
  168. /* are these legal? XXX JH */
  169. case NEGATE_EXPR:
  170. case ABS_EXPR:
  171. case FLOAT_EXPR:
  172. /* These don't change whether an object is nonzero or zero. */
  173. return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
  174. case COND_EXPR:
  175. /* Distribute the conversion into the arms of a COND_EXPR. */
  176. return fold_build3 (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
  177. java_truthvalue_conversion (TREE_OPERAND (expr, 1)),
  178. java_truthvalue_conversion (TREE_OPERAND (expr, 2)));
  179. case NOP_EXPR:
  180. /* If this is widening the argument, we can ignore it. */
  181. if (TYPE_PRECISION (TREE_TYPE (expr))
  182. >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
  183. return java_truthvalue_conversion (TREE_OPERAND (expr, 0));
  184. /* fall through to default */
  185. default:
  186. return fold_build2 (NE_EXPR, boolean_type_node,
  187. expr, boolean_false_node);
  188. }
  189. }
  190. /* Save any stack slots that happen to be in the quick_stack into their
  191. home virtual register slots.
  192. The copy order is from low stack index to high, to support the invariant
  193. that the expression for a slot may contain decls for stack slots with
  194. higher (or the same) index, but not lower. */
  195. static void
  196. flush_quick_stack (void)
  197. {
  198. int stack_index = stack_pointer;
  199. unsigned ix;
  200. tree t;
  201. /* Count the number of slots the quick stack is holding. */
  202. for (ix = 0; vec_safe_iterate (quick_stack, ix, &t); ix++)
  203. stack_index -= 1 + TYPE_IS_WIDE (TREE_TYPE (t));
  204. for (ix = 0; vec_safe_iterate (quick_stack, ix, &t); ix++)
  205. {
  206. tree decl, type = TREE_TYPE (t);
  207. decl = find_stack_slot (stack_index, type);
  208. if (decl != t)
  209. java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (t), decl, t));
  210. stack_index += 1 + TYPE_IS_WIDE (type);
  211. }
  212. vec_safe_truncate (quick_stack, 0);
  213. }
  214. /* Push TYPE on the type stack.
  215. Return true on success, 0 on overflow. */
  216. int
  217. push_type_0 (tree type)
  218. {
  219. int n_words;
  220. type = promote_type (type);
  221. n_words = 1 + TYPE_IS_WIDE (type);
  222. if (stack_pointer + n_words > DECL_MAX_STACK (current_function_decl))
  223. return 0;
  224. /* Allocate decl for this variable now, so we get a temporary that
  225. survives the whole method. */
  226. find_stack_slot (stack_pointer, type);
  227. stack_type_map[stack_pointer++] = type;
  228. n_words--;
  229. while (--n_words >= 0)
  230. stack_type_map[stack_pointer++] = TYPE_SECOND;
  231. return 1;
  232. }
  233. void
  234. push_type (tree type)
  235. {
  236. int r = push_type_0 (type);
  237. gcc_assert (r);
  238. }
  239. static void
  240. push_value (tree value)
  241. {
  242. tree type = TREE_TYPE (value);
  243. if (TYPE_PRECISION (type) < 32 && INTEGRAL_TYPE_P (type))
  244. {
  245. type = promote_type (type);
  246. value = convert (type, value);
  247. }
  248. push_type (type);
  249. vec_safe_push (quick_stack, value);
  250. /* If the value has a side effect, then we need to evaluate it
  251. whether or not the result is used. If the value ends up on the
  252. quick stack and is then popped, this won't happen -- so we flush
  253. the quick stack. It is safest to simply always flush, though,
  254. since TREE_SIDE_EFFECTS doesn't capture COMPONENT_REF, and for
  255. the latter we may need to strip conversions. */
  256. flush_quick_stack ();
  257. }
  258. /* Pop a type from the type stack.
  259. TYPE is the expected type. Return the actual type, which must be
  260. convertible to TYPE.
  261. On an error, *MESSAGEP is set to a freshly malloc'd error message. */
  262. tree
  263. pop_type_0 (tree type, char **messagep)
  264. {
  265. int n_words;
  266. tree t;
  267. *messagep = NULL;
  268. if (TREE_CODE (type) == RECORD_TYPE)
  269. type = promote_type (type);
  270. n_words = 1 + TYPE_IS_WIDE (type);
  271. if (stack_pointer < n_words)
  272. {
  273. *messagep = xstrdup ("stack underflow");
  274. return type;
  275. }
  276. while (--n_words > 0)
  277. {
  278. if (stack_type_map[--stack_pointer] != void_type_node)
  279. {
  280. *messagep = xstrdup ("Invalid multi-word value on type stack");
  281. return type;
  282. }
  283. }
  284. t = stack_type_map[--stack_pointer];
  285. if (type == NULL_TREE || t == type)
  286. return t;
  287. if (TREE_CODE (t) == TREE_LIST)
  288. {
  289. do
  290. {
  291. tree tt = TREE_PURPOSE (t);
  292. if (! can_widen_reference_to (tt, type))
  293. {
  294. t = tt;
  295. goto fail;
  296. }
  297. t = TREE_CHAIN (t);
  298. }
  299. while (t);
  300. return t;
  301. }
  302. if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (t)
  303. && TYPE_PRECISION (type) <= 32 && TYPE_PRECISION (t) <= 32)
  304. return t;
  305. if (TREE_CODE (type) == POINTER_TYPE && TREE_CODE (t) == POINTER_TYPE)
  306. {
  307. /* If the expected type we've been passed is object or ptr
  308. (i.e. void*), the caller needs to know the real type. */
  309. if (type == ptr_type_node || type == object_ptr_type_node)
  310. return t;
  311. /* Since the verifier has already run, we know that any
  312. types we see will be compatible. In BC mode, this fact
  313. may be checked at runtime, but if that is so then we can
  314. assume its truth here as well. So, we always succeed
  315. here, with the expected type. */
  316. return type;
  317. }
  318. if (! flag_verify_invocations && flag_indirect_dispatch
  319. && t == object_ptr_type_node)
  320. {
  321. if (type != ptr_type_node)
  322. warning (0, "need to insert runtime check for %s",
  323. xstrdup (lang_printable_name (type, 0)));
  324. return type;
  325. }
  326. /* lang_printable_name uses a static buffer, so we must save the result
  327. from calling it the first time. */
  328. fail:
  329. {
  330. char *temp = xstrdup (lang_printable_name (type, 0));
  331. /* If the stack contains a multi-word type, keep popping the stack until
  332. the real type is found. */
  333. while (t == void_type_node)
  334. t = stack_type_map[--stack_pointer];
  335. *messagep = concat ("expected type '", temp,
  336. "' but stack contains '", lang_printable_name (t, 0),
  337. "'", NULL);
  338. free (temp);
  339. }
  340. return type;
  341. }
  342. /* Pop a type from the type stack.
  343. TYPE is the expected type. Return the actual type, which must be
  344. convertible to TYPE, otherwise call error. */
  345. tree
  346. pop_type (tree type)
  347. {
  348. char *message = NULL;
  349. type = pop_type_0 (type, &message);
  350. if (message != NULL)
  351. {
  352. error ("%s", message);
  353. free (message);
  354. }
  355. return type;
  356. }
  357. /* Return true if two type assertions are equal. */
  358. bool
  359. type_assertion_hasher::equal (type_assertion *k1, type_assertion *k2)
  360. {
  361. return (k1->assertion_code == k2->assertion_code
  362. && k1->op1 == k2->op1
  363. && k1->op2 == k2->op2);
  364. }
  365. /* Hash a type assertion. */
  366. hashval_t
  367. type_assertion_hasher::hash (type_assertion *k_p)
  368. {
  369. hashval_t hash = iterative_hash (&k_p->assertion_code, sizeof
  370. k_p->assertion_code, 0);
  371. switch (k_p->assertion_code)
  372. {
  373. case JV_ASSERT_TYPES_COMPATIBLE:
  374. hash = iterative_hash (&TYPE_UID (k_p->op2), sizeof TYPE_UID (k_p->op2),
  375. hash);
  376. /* Fall through. */
  377. case JV_ASSERT_IS_INSTANTIABLE:
  378. hash = iterative_hash (&TYPE_UID (k_p->op1), sizeof TYPE_UID (k_p->op1),
  379. hash);
  380. /* Fall through. */
  381. case JV_ASSERT_END_OF_TABLE:
  382. break;
  383. default:
  384. gcc_unreachable ();
  385. }
  386. return hash;
  387. }
  388. /* Add an entry to the type assertion table for the given class.
  389. KLASS is the class for which this assertion will be evaluated by the
  390. runtime during loading/initialization.
  391. ASSERTION_CODE is the 'opcode' or type of this assertion: see java-tree.h.
  392. OP1 and OP2 are the operands. The tree type of these arguments may be
  393. specific to each assertion_code. */
  394. void
  395. add_type_assertion (tree klass, int assertion_code, tree op1, tree op2)
  396. {
  397. hash_table<type_assertion_hasher> *assertions_htab;
  398. type_assertion as;
  399. type_assertion **as_pp;
  400. assertions_htab = TYPE_ASSERTIONS (klass);
  401. if (assertions_htab == NULL)
  402. {
  403. assertions_htab = hash_table<type_assertion_hasher>::create_ggc (7);
  404. TYPE_ASSERTIONS (current_class) = assertions_htab;
  405. }
  406. as.assertion_code = assertion_code;
  407. as.op1 = op1;
  408. as.op2 = op2;
  409. as_pp = assertions_htab->find_slot (&as, INSERT);
  410. /* Don't add the same assertion twice. */
  411. if (*as_pp)
  412. return;
  413. *as_pp = ggc_alloc<type_assertion> ();
  414. **as_pp = as;
  415. }
  416. /* Return 1 if SOURCE_TYPE can be safely widened to TARGET_TYPE.
  417. Handles array types and interfaces. */
  418. int
  419. can_widen_reference_to (tree source_type, tree target_type)
  420. {
  421. if (source_type == ptr_type_node || target_type == object_ptr_type_node)
  422. return 1;
  423. /* Get rid of pointers */
  424. if (TREE_CODE (source_type) == POINTER_TYPE)
  425. source_type = TREE_TYPE (source_type);
  426. if (TREE_CODE (target_type) == POINTER_TYPE)
  427. target_type = TREE_TYPE (target_type);
  428. if (source_type == target_type)
  429. return 1;
  430. /* FIXME: This is very pessimistic, in that it checks everything,
  431. even if we already know that the types are compatible. If we're
  432. to support full Java class loader semantics, we need this.
  433. However, we could do something more optimal. */
  434. if (! flag_verify_invocations)
  435. {
  436. add_type_assertion (current_class, JV_ASSERT_TYPES_COMPATIBLE,
  437. source_type, target_type);
  438. if (!quiet_flag)
  439. warning (0, "assert: %s is assign compatible with %s",
  440. xstrdup (lang_printable_name (target_type, 0)),
  441. xstrdup (lang_printable_name (source_type, 0)));
  442. /* Punt everything to runtime. */
  443. return 1;
  444. }
  445. if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
  446. {
  447. return 1;
  448. }
  449. else
  450. {
  451. if (TYPE_ARRAY_P (source_type) || TYPE_ARRAY_P (target_type))
  452. {
  453. HOST_WIDE_INT source_length, target_length;
  454. if (TYPE_ARRAY_P (source_type) != TYPE_ARRAY_P (target_type))
  455. {
  456. /* An array implements Cloneable and Serializable. */
  457. tree name = DECL_NAME (TYPE_NAME (target_type));
  458. return (name == java_lang_cloneable_identifier_node
  459. || name == java_io_serializable_identifier_node);
  460. }
  461. target_length = java_array_type_length (target_type);
  462. if (target_length >= 0)
  463. {
  464. source_length = java_array_type_length (source_type);
  465. if (source_length != target_length)
  466. return 0;
  467. }
  468. source_type = TYPE_ARRAY_ELEMENT (source_type);
  469. target_type = TYPE_ARRAY_ELEMENT (target_type);
  470. if (source_type == target_type)
  471. return 1;
  472. if (TREE_CODE (source_type) != POINTER_TYPE
  473. || TREE_CODE (target_type) != POINTER_TYPE)
  474. return 0;
  475. return can_widen_reference_to (source_type, target_type);
  476. }
  477. else
  478. {
  479. int source_depth = class_depth (source_type);
  480. int target_depth = class_depth (target_type);
  481. if (TYPE_DUMMY (source_type) || TYPE_DUMMY (target_type))
  482. {
  483. if (! quiet_flag)
  484. warning (0, "assert: %s is assign compatible with %s",
  485. xstrdup (lang_printable_name (target_type, 0)),
  486. xstrdup (lang_printable_name (source_type, 0)));
  487. return 1;
  488. }
  489. /* class_depth can return a negative depth if an error occurred */
  490. if (source_depth < 0 || target_depth < 0)
  491. return 0;
  492. if (CLASS_INTERFACE (TYPE_NAME (target_type)))
  493. {
  494. /* target_type is OK if source_type or source_type ancestors
  495. implement target_type. We handle multiple sub-interfaces */
  496. tree binfo, base_binfo;
  497. int i;
  498. for (binfo = TYPE_BINFO (source_type), i = 0;
  499. BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
  500. if (can_widen_reference_to
  501. (BINFO_TYPE (base_binfo), target_type))
  502. return 1;
  503. if (!i)
  504. return 0;
  505. }
  506. for ( ; source_depth > target_depth; source_depth--)
  507. {
  508. source_type
  509. = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (source_type), 0));
  510. }
  511. return source_type == target_type;
  512. }
  513. }
  514. }
  515. static tree
  516. pop_value (tree type)
  517. {
  518. type = pop_type (type);
  519. if (vec_safe_length (quick_stack) != 0)
  520. return quick_stack->pop ();
  521. else
  522. return find_stack_slot (stack_pointer, promote_type (type));
  523. }
  524. /* Pop and discard the top COUNT stack slots. */
  525. static void
  526. java_stack_pop (int count)
  527. {
  528. while (count > 0)
  529. {
  530. tree type;
  531. gcc_assert (stack_pointer != 0);
  532. type = stack_type_map[stack_pointer - 1];
  533. if (type == TYPE_SECOND)
  534. {
  535. count--;
  536. gcc_assert (stack_pointer != 1 && count > 0);
  537. type = stack_type_map[stack_pointer - 2];
  538. }
  539. pop_value (type);
  540. count--;
  541. }
  542. }
  543. /* Implement the 'swap' operator (to swap two top stack slots). */
  544. static void
  545. java_stack_swap (void)
  546. {
  547. tree type1, type2;
  548. tree temp;
  549. tree decl1, decl2;
  550. if (stack_pointer < 2
  551. || (type1 = stack_type_map[stack_pointer - 1]) == TYPE_SECOND
  552. || (type2 = stack_type_map[stack_pointer - 2]) == TYPE_SECOND
  553. || TYPE_IS_WIDE (type1) || TYPE_IS_WIDE (type2))
  554. /* Bad stack swap. */
  555. abort ();
  556. /* Bad stack swap. */
  557. flush_quick_stack ();
  558. decl1 = find_stack_slot (stack_pointer - 1, type1);
  559. decl2 = find_stack_slot (stack_pointer - 2, type2);
  560. temp = build_decl (input_location, VAR_DECL, NULL_TREE, type1);
  561. java_add_local_var (temp);
  562. java_add_stmt (build2 (MODIFY_EXPR, type1, temp, decl1));
  563. java_add_stmt (build2 (MODIFY_EXPR, type2,
  564. find_stack_slot (stack_pointer - 1, type2),
  565. decl2));
  566. java_add_stmt (build2 (MODIFY_EXPR, type1,
  567. find_stack_slot (stack_pointer - 2, type1),
  568. temp));
  569. stack_type_map[stack_pointer - 1] = type2;
  570. stack_type_map[stack_pointer - 2] = type1;
  571. }
  572. static void
  573. java_stack_dup (int size, int offset)
  574. {
  575. int low_index = stack_pointer - size - offset;
  576. int dst_index;
  577. if (low_index < 0)
  578. error ("stack underflow - dup* operation");
  579. flush_quick_stack ();
  580. stack_pointer += size;
  581. dst_index = stack_pointer;
  582. for (dst_index = stack_pointer; --dst_index >= low_index; )
  583. {
  584. tree type;
  585. int src_index = dst_index - size;
  586. if (src_index < low_index)
  587. src_index = dst_index + size + offset;
  588. type = stack_type_map [src_index];
  589. if (type == TYPE_SECOND)
  590. {
  591. /* Dup operation splits 64-bit number. */
  592. gcc_assert (src_index > low_index);
  593. stack_type_map[dst_index] = type;
  594. src_index--; dst_index--;
  595. type = stack_type_map[src_index];
  596. gcc_assert (TYPE_IS_WIDE (type));
  597. }
  598. else
  599. gcc_assert (! TYPE_IS_WIDE (type));
  600. if (src_index != dst_index)
  601. {
  602. tree src_decl = find_stack_slot (src_index, type);
  603. tree dst_decl = find_stack_slot (dst_index, type);
  604. java_add_stmt
  605. (build2 (MODIFY_EXPR, TREE_TYPE (dst_decl), dst_decl, src_decl));
  606. stack_type_map[dst_index] = type;
  607. }
  608. }
  609. }
  610. /* Calls _Jv_Throw or _Jv_Sjlj_Throw. Discard the contents of the
  611. value stack. */
  612. static void
  613. build_java_athrow (tree node)
  614. {
  615. tree call;
  616. call = build_call_nary (void_type_node,
  617. build_address_of (throw_node),
  618. 1, node);
  619. TREE_SIDE_EFFECTS (call) = 1;
  620. java_add_stmt (call);
  621. java_stack_pop (stack_pointer);
  622. }
  623. /* Implementation for jsr/ret */
  624. static void
  625. build_java_jsr (int target_pc, int return_pc)
  626. {
  627. tree where = lookup_label (target_pc);
  628. tree ret = lookup_label (return_pc);
  629. tree ret_label = fold_build1 (ADDR_EXPR, return_address_type_node, ret);
  630. push_value (ret_label);
  631. flush_quick_stack ();
  632. java_add_stmt (build1 (GOTO_EXPR, void_type_node, where));
  633. /* Do not need to emit the label here. We noted the existence of the
  634. label as a jump target in note_instructions; we'll emit the label
  635. for real at the beginning of the expand_byte_code loop. */
  636. }
  637. static void
  638. build_java_ret (tree location)
  639. {
  640. java_add_stmt (build1 (GOTO_EXPR, void_type_node, location));
  641. }
  642. /* Implementation of operations on array: new, load, store, length */
  643. tree
  644. decode_newarray_type (int atype)
  645. {
  646. switch (atype)
  647. {
  648. case 4: return boolean_type_node;
  649. case 5: return char_type_node;
  650. case 6: return float_type_node;
  651. case 7: return double_type_node;
  652. case 8: return byte_type_node;
  653. case 9: return short_type_node;
  654. case 10: return int_type_node;
  655. case 11: return long_type_node;
  656. default: return NULL_TREE;
  657. }
  658. }
  659. /* Map primitive type to the code used by OPCODE_newarray. */
  660. int
  661. encode_newarray_type (tree type)
  662. {
  663. if (type == boolean_type_node)
  664. return 4;
  665. else if (type == char_type_node)
  666. return 5;
  667. else if (type == float_type_node)
  668. return 6;
  669. else if (type == double_type_node)
  670. return 7;
  671. else if (type == byte_type_node)
  672. return 8;
  673. else if (type == short_type_node)
  674. return 9;
  675. else if (type == int_type_node)
  676. return 10;
  677. else if (type == long_type_node)
  678. return 11;
  679. else
  680. gcc_unreachable ();
  681. }
  682. /* Build a call to _Jv_ThrowBadArrayIndex(), the
  683. ArrayIndexOfBoundsException exception handler. */
  684. static tree
  685. build_java_throw_out_of_bounds_exception (tree index)
  686. {
  687. tree node;
  688. /* We need to build a COMPOUND_EXPR because _Jv_ThrowBadArrayIndex()
  689. has void return type. We cannot just set the type of the CALL_EXPR below
  690. to int_type_node because we would lose it during gimplification. */
  691. gcc_assert (VOID_TYPE_P (TREE_TYPE (TREE_TYPE (soft_badarrayindex_node))));
  692. node = build_call_nary (void_type_node,
  693. build_address_of (soft_badarrayindex_node),
  694. 1, index);
  695. TREE_SIDE_EFFECTS (node) = 1;
  696. node = build2 (COMPOUND_EXPR, int_type_node, node, integer_zero_node);
  697. TREE_SIDE_EFFECTS (node) = 1; /* Allows expansion within ANDIF */
  698. return (node);
  699. }
  700. /* Return the length of an array. Doesn't perform any checking on the nature
  701. or value of the array NODE. May be used to implement some bytecodes. */
  702. tree
  703. build_java_array_length_access (tree node)
  704. {
  705. tree type = TREE_TYPE (node);
  706. tree array_type = TREE_TYPE (type);
  707. HOST_WIDE_INT length;
  708. if (!is_array_type_p (type))
  709. {
  710. /* With the new verifier, we will see an ordinary pointer type
  711. here. In this case, we just use an arbitrary array type. */
  712. array_type = build_java_array_type (object_ptr_type_node, -1);
  713. type = promote_type (array_type);
  714. }
  715. length = java_array_type_length (type);
  716. if (length >= 0)
  717. return build_int_cst (NULL_TREE, length);
  718. node = build3 (COMPONENT_REF, int_type_node,
  719. build_java_indirect_ref (array_type, node,
  720. flag_check_references),
  721. lookup_field (&array_type, get_identifier ("length")),
  722. NULL_TREE);
  723. IS_ARRAY_LENGTH_ACCESS (node) = 1;
  724. return node;
  725. }
  726. /* Optionally checks a reference against the NULL pointer. ARG1: the
  727. expr, ARG2: we should check the reference. Don't generate extra
  728. checks if we're not generating code. */
  729. tree
  730. java_check_reference (tree expr, int check)
  731. {
  732. if (!flag_syntax_only && check)
  733. {
  734. expr = save_expr (expr);
  735. expr = build3 (COND_EXPR, TREE_TYPE (expr),
  736. build2 (EQ_EXPR, boolean_type_node,
  737. expr, null_pointer_node),
  738. build_call_nary (void_type_node,
  739. build_address_of (soft_nullpointer_node),
  740. 0),
  741. expr);
  742. }
  743. return expr;
  744. }
  745. /* Reference an object: just like an INDIRECT_REF, but with checking. */
  746. tree
  747. build_java_indirect_ref (tree type, tree expr, int check)
  748. {
  749. tree t;
  750. t = java_check_reference (expr, check);
  751. t = convert (build_pointer_type (type), t);
  752. return build1 (INDIRECT_REF, type, t);
  753. }
  754. /* Implement array indexing (either as l-value or r-value).
  755. Returns a tree for ARRAY[INDEX], assume TYPE is the element type.
  756. Optionally performs bounds checking and/or test to NULL.
  757. At this point, ARRAY should have been verified as an array. */
  758. tree
  759. build_java_arrayaccess (tree array, tree type, tree index)
  760. {
  761. tree node, throw_expr = NULL_TREE;
  762. tree data_field;
  763. tree ref;
  764. tree array_type = TREE_TYPE (TREE_TYPE (array));
  765. tree size_exp = fold_convert (sizetype, size_in_bytes (type));
  766. if (!is_array_type_p (TREE_TYPE (array)))
  767. {
  768. /* With the new verifier, we will see an ordinary pointer type
  769. here. In this case, we just use the correct array type. */
  770. array_type = build_java_array_type (type, -1);
  771. }
  772. if (flag_bounds_check)
  773. {
  774. /* Generate:
  775. * (unsigned jint) INDEX >= (unsigned jint) LEN
  776. * && throw ArrayIndexOutOfBoundsException.
  777. * Note this is equivalent to and more efficient than:
  778. * INDEX < 0 || INDEX >= LEN && throw ... */
  779. tree test;
  780. tree len = convert (unsigned_int_type_node,
  781. build_java_array_length_access (array));
  782. test = fold_build2 (GE_EXPR, boolean_type_node,
  783. convert (unsigned_int_type_node, index),
  784. len);
  785. if (! integer_zerop (test))
  786. {
  787. throw_expr
  788. = build2 (TRUTH_ANDIF_EXPR, int_type_node, test,
  789. build_java_throw_out_of_bounds_exception (index));
  790. /* allows expansion within COMPOUND */
  791. TREE_SIDE_EFFECTS( throw_expr ) = 1;
  792. }
  793. }
  794. /* If checking bounds, wrap the index expr with a COMPOUND_EXPR in order
  795. to have the bounds check evaluated first. */
  796. if (throw_expr != NULL_TREE)
  797. index = build2 (COMPOUND_EXPR, int_type_node, throw_expr, index);
  798. data_field = lookup_field (&array_type, get_identifier ("data"));
  799. ref = build3 (COMPONENT_REF, TREE_TYPE (data_field),
  800. build_java_indirect_ref (array_type, array,
  801. flag_check_references),
  802. data_field, NULL_TREE);
  803. /* Take the address of the data field and convert it to a pointer to
  804. the element type. */
  805. node = build1 (NOP_EXPR, build_pointer_type (type), build_address_of (ref));
  806. /* Multiply the index by the size of an element to obtain a byte
  807. offset. Convert the result to a pointer to the element type. */
  808. index = build2 (MULT_EXPR, sizetype,
  809. fold_convert (sizetype, index),
  810. size_exp);
  811. /* Sum the byte offset and the address of the data field. */
  812. node = fold_build_pointer_plus (node, index);
  813. /* Finally, return
  814. *((&array->data) + index*size_exp)
  815. */
  816. return build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (node)), node);
  817. }
  818. /* Generate code to throw an ArrayStoreException if OBJECT is not assignable
  819. (at runtime) to an element of ARRAY. A NOP_EXPR is returned if it can
  820. determine that no check is required. */
  821. tree
  822. build_java_arraystore_check (tree array, tree object)
  823. {
  824. tree check, element_type, source;
  825. tree array_type_p = TREE_TYPE (array);
  826. tree object_type = TYPE_NAME (TREE_TYPE (TREE_TYPE (object)));
  827. if (! flag_verify_invocations)
  828. {
  829. /* With the new verifier, we don't track precise types. FIXME:
  830. performance regression here. */
  831. element_type = TYPE_NAME (object_type_node);
  832. }
  833. else
  834. {
  835. gcc_assert (is_array_type_p (array_type_p));
  836. /* Get the TYPE_DECL for ARRAY's element type. */
  837. element_type
  838. = TYPE_NAME (TREE_TYPE (TREE_TYPE (TREE_TYPE (array_type_p))));
  839. }
  840. gcc_assert (TREE_CODE (element_type) == TYPE_DECL
  841. && TREE_CODE (object_type) == TYPE_DECL);
  842. if (!flag_store_check)
  843. return build1 (NOP_EXPR, array_type_p, array);
  844. /* No check is needed if the element type is final. Also check that
  845. element_type matches object_type, since in the bytecode
  846. compilation case element_type may be the actual element type of
  847. the array rather than its declared type. However, if we're doing
  848. indirect dispatch, we can't do the `final' optimization. */
  849. if (element_type == object_type
  850. && ! flag_indirect_dispatch
  851. && CLASS_FINAL (element_type))
  852. return build1 (NOP_EXPR, array_type_p, array);
  853. /* OBJECT might be wrapped by a SAVE_EXPR. */
  854. if (TREE_CODE (object) == SAVE_EXPR)
  855. source = TREE_OPERAND (object, 0);
  856. else
  857. source = object;
  858. /* Avoid the check if OBJECT was just loaded from the same array. */
  859. if (TREE_CODE (source) == ARRAY_REF)
  860. {
  861. tree target;
  862. source = TREE_OPERAND (source, 0); /* COMPONENT_REF. */
  863. source = TREE_OPERAND (source, 0); /* INDIRECT_REF. */
  864. source = TREE_OPERAND (source, 0); /* Source array's DECL or SAVE_EXPR. */
  865. if (TREE_CODE (source) == SAVE_EXPR)
  866. source = TREE_OPERAND (source, 0);
  867. target = array;
  868. if (TREE_CODE (target) == SAVE_EXPR)
  869. target = TREE_OPERAND (target, 0);
  870. if (source == target)
  871. return build1 (NOP_EXPR, array_type_p, array);
  872. }
  873. /* Build an invocation of _Jv_CheckArrayStore */
  874. check = build_call_nary (void_type_node,
  875. build_address_of (soft_checkarraystore_node),
  876. 2, array, object);
  877. TREE_SIDE_EFFECTS (check) = 1;
  878. return check;
  879. }
  880. /* Makes sure that INDEXED_TYPE is appropriate. If not, make it from
  881. ARRAY_NODE. This function is used to retrieve something less vague than
  882. a pointer type when indexing the first dimension of something like [[<t>.
  883. May return a corrected type, if necessary, otherwise INDEXED_TYPE is
  884. return unchanged. */
  885. static tree
  886. build_java_check_indexed_type (tree array_node ATTRIBUTE_UNUSED,
  887. tree indexed_type)
  888. {
  889. /* We used to check to see if ARRAY_NODE really had array type.
  890. However, with the new verifier, this is not necessary, as we know
  891. that the object will be an array of the appropriate type. */
  892. return indexed_type;
  893. }
  894. /* newarray triggers a call to _Jv_NewPrimArray. This function should be
  895. called with an integer code (the type of array to create), and the length
  896. of the array to create. */
  897. tree
  898. build_newarray (int atype_value, tree length)
  899. {
  900. tree type_arg;
  901. tree prim_type = decode_newarray_type (atype_value);
  902. tree type
  903. = build_java_array_type (prim_type,
  904. tree_fits_shwi_p (length)
  905. ? tree_to_shwi (length) : -1);
  906. /* Pass a reference to the primitive type class and save the runtime
  907. some work. */
  908. type_arg = build_class_ref (prim_type);
  909. return build_call_nary (promote_type (type),
  910. build_address_of (soft_newarray_node),
  911. 2, type_arg, length);
  912. }
  913. /* Generates anewarray from a given CLASS_TYPE. Gets from the stack the size
  914. of the dimension. */
  915. tree
  916. build_anewarray (tree class_type, tree length)
  917. {
  918. tree type
  919. = build_java_array_type (class_type,
  920. tree_fits_shwi_p (length)
  921. ? tree_to_shwi (length) : -1);
  922. return build_call_nary (promote_type (type),
  923. build_address_of (soft_anewarray_node),
  924. 3,
  925. length,
  926. build_class_ref (class_type),
  927. null_pointer_node);
  928. }
  929. /* Return a node the evaluates 'new TYPE[LENGTH]'. */
  930. tree
  931. build_new_array (tree type, tree length)
  932. {
  933. if (JPRIMITIVE_TYPE_P (type))
  934. return build_newarray (encode_newarray_type (type), length);
  935. else
  936. return build_anewarray (TREE_TYPE (type), length);
  937. }
  938. /* Generates a call to _Jv_NewMultiArray. multianewarray expects a
  939. class pointer, a number of dimensions and the matching number of
  940. dimensions. The argument list is NULL terminated. */
  941. static void
  942. expand_java_multianewarray (tree class_type, int ndim)
  943. {
  944. int i;
  945. vec<tree, va_gc> *args = NULL;
  946. vec_safe_grow (args, 3 + ndim);
  947. (*args)[0] = build_class_ref (class_type);
  948. (*args)[1] = build_int_cst (NULL_TREE, ndim);
  949. for(i = ndim - 1; i >= 0; i-- )
  950. (*args)[(unsigned)(2 + i)] = pop_value (int_type_node);
  951. (*args)[2 + ndim] = null_pointer_node;
  952. push_value (build_call_vec (promote_type (class_type),
  953. build_address_of (soft_multianewarray_node),
  954. args));
  955. }
  956. /* ARRAY[INDEX] <- RHS. build_java_check_indexed_type makes sure that
  957. ARRAY is an array type. May expand some bound checking and NULL
  958. pointer checking. RHS_TYPE_NODE we are going to store. In the case
  959. of the CHAR/BYTE/BOOLEAN SHORT, the type popped of the stack is an
  960. INT. In those cases, we make the conversion.
  961. if ARRAy is a reference type, the assignment is checked at run-time
  962. to make sure that the RHS can be assigned to the array element
  963. type. It is not necessary to generate this code if ARRAY is final. */
  964. static void
  965. expand_java_arraystore (tree rhs_type_node)
  966. {
  967. tree rhs_node = pop_value ((INTEGRAL_TYPE_P (rhs_type_node)
  968. && TYPE_PRECISION (rhs_type_node) <= 32) ?
  969. int_type_node : rhs_type_node);
  970. tree index = pop_value (int_type_node);
  971. tree array_type, array, temp, access;
  972. /* If we're processing an `aaload' we might as well just pick
  973. `Object'. */
  974. if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
  975. {
  976. array_type = build_java_array_type (object_ptr_type_node, -1);
  977. rhs_type_node = object_ptr_type_node;
  978. }
  979. else
  980. array_type = build_java_array_type (rhs_type_node, -1);
  981. array = pop_value (array_type);
  982. array = build1 (NOP_EXPR, promote_type (array_type), array);
  983. rhs_type_node = build_java_check_indexed_type (array, rhs_type_node);
  984. flush_quick_stack ();
  985. index = save_expr (index);
  986. array = save_expr (array);
  987. /* We want to perform the bounds check (done by
  988. build_java_arrayaccess) before the type check (done by
  989. build_java_arraystore_check). So, we call build_java_arrayaccess
  990. -- which returns an ARRAY_REF lvalue -- and we then generate code
  991. to stash the address of that lvalue in a temp. Then we call
  992. build_java_arraystore_check, and finally we generate a
  993. MODIFY_EXPR to set the array element. */
  994. access = build_java_arrayaccess (array, rhs_type_node, index);
  995. temp = build_decl (input_location, VAR_DECL, NULL_TREE,
  996. build_pointer_type (TREE_TYPE (access)));
  997. java_add_local_var (temp);
  998. java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (temp),
  999. temp,
  1000. build_fold_addr_expr (access)));
  1001. if (TREE_CODE (rhs_type_node) == POINTER_TYPE)
  1002. {
  1003. tree check = build_java_arraystore_check (array, rhs_node);
  1004. java_add_stmt (check);
  1005. }
  1006. java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (access),
  1007. build1 (INDIRECT_REF, TREE_TYPE (access), temp),
  1008. rhs_node));
  1009. }
  1010. /* Expand the evaluation of ARRAY[INDEX]. build_java_check_indexed_type makes
  1011. sure that LHS is an array type. May expand some bound checking and NULL
  1012. pointer checking.
  1013. LHS_TYPE_NODE is the type of ARRAY[INDEX]. But in the case of CHAR/BYTE/
  1014. BOOLEAN/SHORT, we push a promoted type back to the stack.
  1015. */
  1016. static void
  1017. expand_java_arrayload (tree lhs_type_node)
  1018. {
  1019. tree load_node;
  1020. tree index_node = pop_value (int_type_node);
  1021. tree array_type;
  1022. tree array_node;
  1023. /* If we're processing an `aaload' we might as well just pick
  1024. `Object'. */
  1025. if (TREE_CODE (lhs_type_node) == POINTER_TYPE)
  1026. {
  1027. array_type = build_java_array_type (object_ptr_type_node, -1);
  1028. lhs_type_node = object_ptr_type_node;
  1029. }
  1030. else
  1031. array_type = build_java_array_type (lhs_type_node, -1);
  1032. array_node = pop_value (array_type);
  1033. array_node = build1 (NOP_EXPR, promote_type (array_type), array_node);
  1034. index_node = save_expr (index_node);
  1035. array_node = save_expr (array_node);
  1036. lhs_type_node = build_java_check_indexed_type (array_node,
  1037. lhs_type_node);
  1038. load_node = build_java_arrayaccess (array_node,
  1039. lhs_type_node,
  1040. index_node);
  1041. if (INTEGRAL_TYPE_P (lhs_type_node) && TYPE_PRECISION (lhs_type_node) <= 32)
  1042. load_node = fold_build1 (NOP_EXPR, int_type_node, load_node);
  1043. push_value (load_node);
  1044. }
  1045. /* Expands .length. Makes sure that we deal with and array and may expand
  1046. a NULL check on the array object. */
  1047. static void
  1048. expand_java_array_length (void)
  1049. {
  1050. tree array = pop_value (ptr_type_node);
  1051. tree length = build_java_array_length_access (array);
  1052. push_value (length);
  1053. }
  1054. /* Emit code for the call to _Jv_Monitor{Enter,Exit}. CALL can be
  1055. either soft_monitorenter_node or soft_monitorexit_node. */
  1056. static tree
  1057. build_java_monitor (tree call, tree object)
  1058. {
  1059. return build_call_nary (void_type_node,
  1060. build_address_of (call),
  1061. 1, object);
  1062. }
  1063. /* Emit code for one of the PUSHC instructions. */
  1064. static void
  1065. expand_java_pushc (int ival, tree type)
  1066. {
  1067. tree value;
  1068. if (type == ptr_type_node && ival == 0)
  1069. value = null_pointer_node;
  1070. else if (type == int_type_node || type == long_type_node)
  1071. value = build_int_cst (type, ival);
  1072. else if (type == float_type_node || type == double_type_node)
  1073. {
  1074. REAL_VALUE_TYPE x;
  1075. real_from_integer (&x, TYPE_MODE (type), ival, SIGNED);
  1076. value = build_real (type, x);
  1077. }
  1078. else
  1079. gcc_unreachable ();
  1080. push_value (value);
  1081. }
  1082. static void
  1083. expand_java_return (tree type)
  1084. {
  1085. if (type == void_type_node)
  1086. java_add_stmt (build1 (RETURN_EXPR, void_type_node, NULL));
  1087. else
  1088. {
  1089. tree retval = pop_value (type);
  1090. tree res = DECL_RESULT (current_function_decl);
  1091. retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, retval);
  1092. /* Handle the situation where the native integer type is smaller
  1093. than the JVM integer. It can happen for many cross compilers.
  1094. The whole if expression just goes away if INT_TYPE_SIZE < 32
  1095. is false. */
  1096. if (INT_TYPE_SIZE < 32
  1097. && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (res)))
  1098. < GET_MODE_SIZE (TYPE_MODE (type))))
  1099. retval = build1(NOP_EXPR, TREE_TYPE(res), retval);
  1100. TREE_SIDE_EFFECTS (retval) = 1;
  1101. java_add_stmt (build1 (RETURN_EXPR, void_type_node, retval));
  1102. }
  1103. }
  1104. static void
  1105. expand_load_internal (int index, tree type, int pc)
  1106. {
  1107. tree copy;
  1108. tree var = find_local_variable (index, type, pc);
  1109. /* Now VAR is the VAR_DECL (or PARM_DECL) that we are going to push
  1110. on the stack. If there is an assignment to this VAR_DECL between
  1111. the stack push and the use, then the wrong code could be
  1112. generated. To avoid this we create a new local and copy our
  1113. value into it. Then we push this new local on the stack.
  1114. Hopefully this all gets optimized out. */
  1115. copy = build_decl (input_location, VAR_DECL, NULL_TREE, type);
  1116. if ((INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
  1117. && TREE_TYPE (copy) != TREE_TYPE (var))
  1118. var = convert (type, var);
  1119. java_add_local_var (copy);
  1120. java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (var), copy, var));
  1121. push_value (copy);
  1122. }
  1123. tree
  1124. build_address_of (tree value)
  1125. {
  1126. return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (value)), value);
  1127. }
  1128. bool
  1129. class_has_finalize_method (tree type)
  1130. {
  1131. tree super = CLASSTYPE_SUPER (type);
  1132. if (super == NULL_TREE)
  1133. return false; /* Every class with a real finalizer inherits */
  1134. /* from java.lang.Object. */
  1135. else
  1136. return HAS_FINALIZER_P (type) || class_has_finalize_method (super);
  1137. }
  1138. tree
  1139. java_create_object (tree type)
  1140. {
  1141. tree alloc_node = (class_has_finalize_method (type)
  1142. ? alloc_object_node
  1143. : alloc_no_finalizer_node);
  1144. return build_call_nary (promote_type (type),
  1145. build_address_of (alloc_node),
  1146. 1, build_class_ref (type));
  1147. }
  1148. static void
  1149. expand_java_NEW (tree type)
  1150. {
  1151. tree alloc_node;
  1152. alloc_node = (class_has_finalize_method (type) ? alloc_object_node
  1153. : alloc_no_finalizer_node);
  1154. if (! CLASS_LOADED_P (type))
  1155. load_class (type, 1);
  1156. safe_layout_class (type);
  1157. push_value (build_call_nary (promote_type (type),
  1158. build_address_of (alloc_node),
  1159. 1, build_class_ref (type)));
  1160. }
  1161. /* This returns an expression which will extract the class of an
  1162. object. */
  1163. tree
  1164. build_get_class (tree value)
  1165. {
  1166. tree class_field = lookup_field (&dtable_type, get_identifier ("class"));
  1167. tree vtable_field = lookup_field (&object_type_node,
  1168. get_identifier ("vtable"));
  1169. tree tmp = build3 (COMPONENT_REF, dtable_ptr_type,
  1170. build_java_indirect_ref (object_type_node, value,
  1171. flag_check_references),
  1172. vtable_field, NULL_TREE);
  1173. return build3 (COMPONENT_REF, class_ptr_type,
  1174. build1 (INDIRECT_REF, dtable_type, tmp),
  1175. class_field, NULL_TREE);
  1176. }
  1177. /* This builds the tree representation of the `instanceof' operator.
  1178. It tries various tricks to optimize this in cases where types are
  1179. known. */
  1180. tree
  1181. build_instanceof (tree value, tree type)
  1182. {
  1183. tree expr;
  1184. tree itype = TREE_TYPE (TREE_TYPE (soft_instanceof_node));
  1185. tree valtype = TREE_TYPE (TREE_TYPE (value));
  1186. tree valclass = TYPE_NAME (valtype);
  1187. tree klass;
  1188. /* When compiling from bytecode, we need to ensure that TYPE has
  1189. been loaded. */
  1190. if (CLASS_P (type) && ! CLASS_LOADED_P (type))
  1191. {
  1192. load_class (type, 1);
  1193. safe_layout_class (type);
  1194. if (! TYPE_SIZE (type) || TREE_CODE (TYPE_SIZE (type)) == ERROR_MARK)
  1195. return error_mark_node;
  1196. }
  1197. klass = TYPE_NAME (type);
  1198. if (type == object_type_node || inherits_from_p (valtype, type))
  1199. {
  1200. /* Anything except `null' is an instance of Object. Likewise,
  1201. if the object is known to be an instance of the class, then
  1202. we only need to check for `null'. */
  1203. expr = build2 (NE_EXPR, itype, value, null_pointer_node);
  1204. }
  1205. else if (flag_verify_invocations
  1206. && ! TYPE_ARRAY_P (type)
  1207. && ! TYPE_ARRAY_P (valtype)
  1208. && DECL_P (klass) && DECL_P (valclass)
  1209. && ! CLASS_INTERFACE (valclass)
  1210. && ! CLASS_INTERFACE (klass)
  1211. && ! inherits_from_p (type, valtype)
  1212. && (CLASS_FINAL (klass)
  1213. || ! inherits_from_p (valtype, type)))
  1214. {
  1215. /* The classes are from different branches of the derivation
  1216. tree, so we immediately know the answer. */
  1217. expr = boolean_false_node;
  1218. }
  1219. else if (DECL_P (klass) && CLASS_FINAL (klass))
  1220. {
  1221. tree save = save_expr (value);
  1222. expr = build3 (COND_EXPR, itype,
  1223. build2 (NE_EXPR, boolean_type_node,
  1224. save, null_pointer_node),
  1225. build2 (EQ_EXPR, itype,
  1226. build_get_class (save),
  1227. build_class_ref (type)),
  1228. boolean_false_node);
  1229. }
  1230. else
  1231. {
  1232. expr = build_call_nary (itype,
  1233. build_address_of (soft_instanceof_node),
  1234. 2, value, build_class_ref (type));
  1235. }
  1236. TREE_SIDE_EFFECTS (expr) = TREE_SIDE_EFFECTS (value);
  1237. return expr;
  1238. }
  1239. static void
  1240. expand_java_INSTANCEOF (tree type)
  1241. {
  1242. tree value = pop_value (object_ptr_type_node);
  1243. value = build_instanceof (value, type);
  1244. push_value (value);
  1245. }
  1246. static void
  1247. expand_java_CHECKCAST (tree type)
  1248. {
  1249. tree value = pop_value (ptr_type_node);
  1250. value = build_call_nary (promote_type (type),
  1251. build_address_of (soft_checkcast_node),
  1252. 2, build_class_ref (type), value);
  1253. push_value (value);
  1254. }
  1255. static void
  1256. expand_iinc (unsigned int local_var_index, int ival, int pc)
  1257. {
  1258. tree local_var, res;
  1259. tree constant_value;
  1260. flush_quick_stack ();
  1261. local_var = find_local_variable (local_var_index, int_type_node, pc);
  1262. constant_value = build_int_cst (NULL_TREE, ival);
  1263. res = fold_build2 (PLUS_EXPR, int_type_node, local_var, constant_value);
  1264. java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (local_var), local_var, res));
  1265. }
  1266. tree
  1267. build_java_soft_divmod (enum tree_code op, tree type, tree op1, tree op2)
  1268. {
  1269. tree call = NULL;
  1270. tree arg1 = convert (type, op1);
  1271. tree arg2 = convert (type, op2);
  1272. if (type == int_type_node)
  1273. {
  1274. switch (op)
  1275. {
  1276. case TRUNC_DIV_EXPR:
  1277. call = soft_idiv_node;
  1278. break;
  1279. case TRUNC_MOD_EXPR:
  1280. call = soft_irem_node;
  1281. break;
  1282. default:
  1283. break;
  1284. }
  1285. }
  1286. else if (type == long_type_node)
  1287. {
  1288. switch (op)
  1289. {
  1290. case TRUNC_DIV_EXPR:
  1291. call = soft_ldiv_node;
  1292. break;
  1293. case TRUNC_MOD_EXPR:
  1294. call = soft_lrem_node;
  1295. break;
  1296. default:
  1297. break;
  1298. }
  1299. }
  1300. gcc_assert (call);
  1301. call = build_call_nary (type, build_address_of (call), 2, arg1, arg2);
  1302. return call;
  1303. }
  1304. tree
  1305. build_java_binop (enum tree_code op, tree type, tree arg1, tree arg2)
  1306. {
  1307. tree mask;
  1308. switch (op)
  1309. {
  1310. case URSHIFT_EXPR:
  1311. {
  1312. tree u_type = unsigned_type_for (type);
  1313. arg1 = convert (u_type, arg1);
  1314. arg1 = build_java_binop (RSHIFT_EXPR, u_type, arg1, arg2);
  1315. return convert (type, arg1);
  1316. }
  1317. case LSHIFT_EXPR:
  1318. case RSHIFT_EXPR:
  1319. mask = build_int_cst (int_type_node,
  1320. TYPE_PRECISION (TREE_TYPE (arg1)) - 1);
  1321. arg2 = fold_build2 (BIT_AND_EXPR, int_type_node, arg2, mask);
  1322. break;
  1323. case COMPARE_L_EXPR: /* arg1 > arg2 ? 1 : arg1 == arg2 ? 0 : -1 */
  1324. case COMPARE_G_EXPR: /* arg1 < arg2 ? -1 : arg1 == arg2 ? 0 : 1 */
  1325. arg1 = save_expr (arg1); arg2 = save_expr (arg2);
  1326. {
  1327. tree ifexp1 = fold_build2 (op == COMPARE_L_EXPR ? GT_EXPR : LT_EXPR,
  1328. boolean_type_node, arg1, arg2);
  1329. tree ifexp2 = fold_build2 (EQ_EXPR, boolean_type_node, arg1, arg2);
  1330. tree second_compare = fold_build3 (COND_EXPR, int_type_node,
  1331. ifexp2, integer_zero_node,
  1332. op == COMPARE_L_EXPR
  1333. ? integer_minus_one_node
  1334. : integer_one_node);
  1335. return fold_build3 (COND_EXPR, int_type_node, ifexp1,
  1336. op == COMPARE_L_EXPR ? integer_one_node
  1337. : integer_minus_one_node,
  1338. second_compare);
  1339. }
  1340. case COMPARE_EXPR:
  1341. arg1 = save_expr (arg1); arg2 = save_expr (arg2);
  1342. {
  1343. tree ifexp1 = fold_build2 (LT_EXPR, boolean_type_node, arg1, arg2);
  1344. tree ifexp2 = fold_build2 (GT_EXPR, boolean_type_node, arg1, arg2);
  1345. tree second_compare = fold_build3 (COND_EXPR, int_type_node,
  1346. ifexp2, integer_one_node,
  1347. integer_zero_node);
  1348. return fold_build3 (COND_EXPR, int_type_node,
  1349. ifexp1, integer_minus_one_node, second_compare);
  1350. }
  1351. case TRUNC_DIV_EXPR:
  1352. case TRUNC_MOD_EXPR:
  1353. if (TREE_CODE (type) == REAL_TYPE
  1354. && op == TRUNC_MOD_EXPR)
  1355. {
  1356. tree call;
  1357. if (type != double_type_node)
  1358. {
  1359. arg1 = convert (double_type_node, arg1);
  1360. arg2 = convert (double_type_node, arg2);
  1361. }
  1362. call = build_call_nary (double_type_node,
  1363. build_address_of (soft_fmod_node),
  1364. 2, arg1, arg2);
  1365. if (type != double_type_node)
  1366. call = convert (type, call);
  1367. return call;
  1368. }
  1369. if (TREE_CODE (type) == INTEGER_TYPE
  1370. && flag_use_divide_subroutine
  1371. && ! flag_syntax_only)
  1372. return build_java_soft_divmod (op, type, arg1, arg2);
  1373. break;
  1374. default: ;
  1375. }
  1376. return fold_build2 (op, type, arg1, arg2);
  1377. }
  1378. static void
  1379. expand_java_binop (tree type, enum tree_code op)
  1380. {
  1381. tree larg, rarg;
  1382. tree ltype = type;
  1383. tree rtype = type;
  1384. switch (op)
  1385. {
  1386. case LSHIFT_EXPR:
  1387. case RSHIFT_EXPR:
  1388. case URSHIFT_EXPR:
  1389. rtype = int_type_node;
  1390. rarg = pop_value (rtype);
  1391. break;
  1392. default:
  1393. rarg = pop_value (rtype);
  1394. }
  1395. larg = pop_value (ltype);
  1396. push_value (build_java_binop (op, type, larg, rarg));
  1397. }
  1398. /* Lookup the field named NAME in *TYPEP or its super classes.
  1399. If not found, return NULL_TREE.
  1400. (If the *TYPEP is not found, or if the field reference is
  1401. ambiguous, return error_mark_node.)
  1402. If found, return the FIELD_DECL, and set *TYPEP to the
  1403. class containing the field. */
  1404. tree
  1405. lookup_field (tree *typep, tree name)
  1406. {
  1407. if (CLASS_P (*typep) && !CLASS_LOADED_P (*typep))
  1408. {
  1409. load_class (*typep, 1);
  1410. safe_layout_class (*typep);
  1411. if (!TYPE_SIZE (*typep) || TREE_CODE (TYPE_SIZE (*typep)) == ERROR_MARK)
  1412. return error_mark_node;
  1413. }
  1414. do
  1415. {
  1416. tree field, binfo, base_binfo;
  1417. tree save_field;
  1418. int i;
  1419. for (field = TYPE_FIELDS (*typep); field; field = DECL_CHAIN (field))
  1420. if (DECL_NAME (field) == name)
  1421. return field;
  1422. /* Process implemented interfaces. */
  1423. save_field = NULL_TREE;
  1424. for (binfo = TYPE_BINFO (*typep), i = 0;
  1425. BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
  1426. {
  1427. tree t = BINFO_TYPE (base_binfo);
  1428. if ((field = lookup_field (&t, name)))
  1429. {
  1430. if (save_field == field)
  1431. continue;
  1432. if (save_field == NULL_TREE)
  1433. save_field = field;
  1434. else
  1435. {
  1436. tree i1 = DECL_CONTEXT (save_field);
  1437. tree i2 = DECL_CONTEXT (field);
  1438. error ("reference %qs is ambiguous: appears in interface %qs and interface %qs",
  1439. IDENTIFIER_POINTER (name),
  1440. IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i1))),
  1441. IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (i2))));
  1442. return error_mark_node;
  1443. }
  1444. }
  1445. }
  1446. if (save_field != NULL_TREE)
  1447. return save_field;
  1448. *typep = CLASSTYPE_SUPER (*typep);
  1449. } while (*typep);
  1450. return NULL_TREE;
  1451. }
  1452. /* Look up the field named NAME in object SELF_VALUE,
  1453. which has class SELF_CLASS (a non-handle RECORD_TYPE).
  1454. SELF_VALUE is NULL_TREE if looking for a static field. */
  1455. tree
  1456. build_field_ref (tree self_value, tree self_class, tree name)
  1457. {
  1458. tree base_class = self_class;
  1459. tree field_decl = lookup_field (&base_class, name);
  1460. if (field_decl == NULL_TREE)
  1461. {
  1462. error ("field %qs not found", IDENTIFIER_POINTER (name));
  1463. return error_mark_node;
  1464. }
  1465. if (self_value == NULL_TREE)
  1466. {
  1467. return build_static_field_ref (field_decl);
  1468. }
  1469. else
  1470. {
  1471. tree base_type = promote_type (base_class);
  1472. /* CHECK is true if self_value is not the this pointer. */
  1473. int check = (! (DECL_P (self_value)
  1474. && DECL_NAME (self_value) == this_identifier_node));
  1475. /* Determine whether a field offset from NULL will lie within
  1476. Page 0: this is necessary on those GNU/Linux/BSD systems that
  1477. trap SEGV to generate NullPointerExceptions.
  1478. We assume that Page 0 will be mapped with NOPERM, and that
  1479. memory may be allocated from any other page, so only field
  1480. offsets < pagesize are guaranteed to trap. We also assume
  1481. the smallest page size we'll encounter is 4k bytes. */
  1482. if (! flag_syntax_only && check && ! flag_check_references
  1483. && ! flag_indirect_dispatch)
  1484. {
  1485. tree field_offset = byte_position (field_decl);
  1486. if (! page_size)
  1487. page_size = size_int (4096);
  1488. check = !tree_int_cst_lt (field_offset, page_size);
  1489. }
  1490. if (base_type != TREE_TYPE (self_value))
  1491. self_value = fold_build1 (NOP_EXPR, base_type, self_value);
  1492. if (! flag_syntax_only && flag_indirect_dispatch)
  1493. {
  1494. tree otable_index
  1495. = build_int_cst (NULL_TREE, get_symbol_table_index
  1496. (field_decl, NULL_TREE,
  1497. &TYPE_OTABLE_METHODS (output_class)));
  1498. tree field_offset
  1499. = build4 (ARRAY_REF, integer_type_node,
  1500. TYPE_OTABLE_DECL (output_class), otable_index,
  1501. NULL_TREE, NULL_TREE);
  1502. tree address;
  1503. if (DECL_CONTEXT (field_decl) != output_class)
  1504. field_offset
  1505. = build3 (COND_EXPR, TREE_TYPE (field_offset),
  1506. build2 (EQ_EXPR, boolean_type_node,
  1507. field_offset, integer_zero_node),
  1508. build_call_nary (void_type_node,
  1509. build_address_of (soft_nosuchfield_node),
  1510. 1, otable_index),
  1511. field_offset);
  1512. self_value = java_check_reference (self_value, check);
  1513. address = fold_build_pointer_plus (self_value, field_offset);
  1514. address = fold_convert (build_pointer_type (TREE_TYPE (field_decl)),
  1515. address);
  1516. return fold_build1 (INDIRECT_REF, TREE_TYPE (field_decl), address);
  1517. }
  1518. self_value = build_java_indirect_ref (TREE_TYPE (TREE_TYPE (self_value)),
  1519. self_value, check);
  1520. return fold_build3 (COMPONENT_REF, TREE_TYPE (field_decl),
  1521. self_value, field_decl, NULL_TREE);
  1522. }
  1523. }
  1524. tree
  1525. lookup_label (int pc)
  1526. {
  1527. tree name;
  1528. char buf[32];
  1529. if (pc > highest_label_pc_this_method)
  1530. highest_label_pc_this_method = pc;
  1531. targetm.asm_out.generate_internal_label (buf, "LJpc=",
  1532. start_label_pc_this_method + pc);
  1533. name = get_identifier (buf);
  1534. if (IDENTIFIER_LOCAL_VALUE (name))
  1535. return IDENTIFIER_LOCAL_VALUE (name);
  1536. else
  1537. {
  1538. /* The type of the address of a label is return_address_type_node. */
  1539. tree decl = create_label_decl (name);
  1540. return pushdecl (decl);
  1541. }
  1542. }
  1543. /* Generate a unique name for the purpose of loops and switches
  1544. labels, and try-catch-finally blocks label or temporary variables. */
  1545. tree
  1546. generate_name (void)
  1547. {
  1548. static int l_number = 0;
  1549. char buff [32];
  1550. targetm.asm_out.generate_internal_label (buff, "LJv", l_number);
  1551. l_number++;
  1552. return get_identifier (buff);
  1553. }
  1554. tree
  1555. create_label_decl (tree name)
  1556. {
  1557. tree decl;
  1558. decl = build_decl (input_location, LABEL_DECL, name,
  1559. TREE_TYPE (return_address_type_node));
  1560. DECL_CONTEXT (decl) = current_function_decl;
  1561. DECL_IGNORED_P (decl) = 1;
  1562. return decl;
  1563. }
  1564. /* This maps a bytecode offset (PC) to various flags. */
  1565. char *instruction_bits;
  1566. /* This is a vector of type states for the current method. It is
  1567. indexed by PC. Each element is a tree vector holding the type
  1568. state at that PC. We only note type states at basic block
  1569. boundaries. */
  1570. vec<tree, va_gc> *type_states;
  1571. static void
  1572. note_label (int current_pc ATTRIBUTE_UNUSED, int target_pc)
  1573. {
  1574. lookup_label (target_pc);
  1575. instruction_bits [target_pc] |= BCODE_JUMP_TARGET;
  1576. }
  1577. /* Emit code to jump to TARGET_PC if VALUE1 CONDITION VALUE2,
  1578. where CONDITION is one of one the compare operators. */
  1579. static void
  1580. expand_compare (enum tree_code condition, tree value1, tree value2,
  1581. int target_pc)
  1582. {
  1583. tree target = lookup_label (target_pc);
  1584. tree cond = fold_build2 (condition, boolean_type_node, value1, value2);
  1585. java_add_stmt
  1586. (build3 (COND_EXPR, void_type_node, java_truthvalue_conversion (cond),
  1587. build1 (GOTO_EXPR, void_type_node, target),
  1588. build_java_empty_stmt ()));
  1589. }
  1590. /* Emit code for a TEST-type opcode. */
  1591. static void
  1592. expand_test (enum tree_code condition, tree type, int target_pc)
  1593. {
  1594. tree value1, value2;
  1595. flush_quick_stack ();
  1596. value1 = pop_value (type);
  1597. value2 = (type == ptr_type_node) ? null_pointer_node : integer_zero_node;
  1598. expand_compare (condition, value1, value2, target_pc);
  1599. }
  1600. /* Emit code for a COND-type opcode. */
  1601. static void
  1602. expand_cond (enum tree_code condition, tree type, int target_pc)
  1603. {
  1604. tree value1, value2;
  1605. flush_quick_stack ();
  1606. /* note: pop values in opposite order */
  1607. value2 = pop_value (type);
  1608. value1 = pop_value (type);
  1609. /* Maybe should check value1 and value2 for type compatibility ??? */
  1610. expand_compare (condition, value1, value2, target_pc);
  1611. }
  1612. static void
  1613. expand_java_goto (int target_pc)
  1614. {
  1615. tree target_label = lookup_label (target_pc);
  1616. flush_quick_stack ();
  1617. java_add_stmt (build1 (GOTO_EXPR, void_type_node, target_label));
  1618. }
  1619. static tree
  1620. expand_java_switch (tree selector, int default_pc)
  1621. {
  1622. tree switch_expr, x;
  1623. flush_quick_stack ();
  1624. switch_expr = build3 (SWITCH_EXPR, TREE_TYPE (selector), selector,
  1625. NULL_TREE, NULL_TREE);
  1626. java_add_stmt (switch_expr);
  1627. x = build_case_label (NULL_TREE, NULL_TREE,
  1628. create_artificial_label (input_location));
  1629. append_to_statement_list (x, &SWITCH_BODY (switch_expr));
  1630. x = build1 (GOTO_EXPR, void_type_node, lookup_label (default_pc));
  1631. append_to_statement_list (x, &SWITCH_BODY (switch_expr));
  1632. return switch_expr;
  1633. }
  1634. static void
  1635. expand_java_add_case (tree switch_expr, int match, int target_pc)
  1636. {
  1637. tree value, x;
  1638. value = build_int_cst (TREE_TYPE (switch_expr), match);
  1639. x = build_case_label (value, NULL_TREE,
  1640. create_artificial_label (input_location));
  1641. append_to_statement_list (x, &SWITCH_BODY (switch_expr));
  1642. x = build1 (GOTO_EXPR, void_type_node, lookup_label (target_pc));
  1643. append_to_statement_list (x, &SWITCH_BODY (switch_expr));
  1644. }
  1645. static vec<tree, va_gc> *
  1646. pop_arguments (tree method_type)
  1647. {
  1648. function_args_iterator fnai;
  1649. tree type;
  1650. vec<tree, va_gc> *args = NULL;
  1651. int arity;
  1652. FOREACH_FUNCTION_ARGS (method_type, type, fnai)
  1653. {
  1654. /* XXX: leaky abstraction. */
  1655. if (type == void_type_node)
  1656. break;
  1657. vec_safe_push (args, type);
  1658. }
  1659. arity = vec_safe_length (args);
  1660. while (arity--)
  1661. {
  1662. tree arg = pop_value ((*args)[arity]);
  1663. /* We simply cast each argument to its proper type. This is
  1664. needed since we lose type information coming out of the
  1665. verifier. We also have to do this when we pop an integer
  1666. type that must be promoted for the function call. */
  1667. if (TREE_CODE (type) == POINTER_TYPE)
  1668. arg = build1 (NOP_EXPR, type, arg);
  1669. else if (targetm.calls.promote_prototypes (type)
  1670. && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
  1671. && INTEGRAL_TYPE_P (type))
  1672. arg = convert (integer_type_node, arg);
  1673. (*args)[arity] = arg;
  1674. }
  1675. return args;
  1676. }
  1677. /* Attach to PTR (a block) the declaration found in ENTRY. */
  1678. int
  1679. attach_init_test_initialization_flags (treetreehash_entry **slot, tree block)
  1680. {
  1681. treetreehash_entry *ite = *slot;
  1682. if (block != error_mark_node)
  1683. {
  1684. if (TREE_CODE (block) == BIND_EXPR)
  1685. {
  1686. tree body = BIND_EXPR_BODY (block);
  1687. DECL_CHAIN (ite->value) = BIND_EXPR_VARS (block);
  1688. BIND_EXPR_VARS (block) = ite->value;
  1689. body = build2 (COMPOUND_EXPR, void_type_node,
  1690. build1 (DECL_EXPR, void_type_node, ite->value), body);
  1691. BIND_EXPR_BODY (block) = body;
  1692. }
  1693. else
  1694. {
  1695. tree body = BLOCK_SUBBLOCKS (block);
  1696. TREE_CHAIN (ite->value) = BLOCK_EXPR_DECLS (block);
  1697. BLOCK_EXPR_DECLS (block) = ite->value;
  1698. body = build2 (COMPOUND_EXPR, void_type_node,
  1699. build1 (DECL_EXPR, void_type_node, ite->value), body);
  1700. BLOCK_SUBBLOCKS (block) = body;
  1701. }
  1702. }
  1703. return true;
  1704. }
  1705. /* Build an expression to initialize the class CLAS.
  1706. if EXPR is non-NULL, returns an expression to first call the initializer
  1707. (if it is needed) and then calls EXPR. */
  1708. tree
  1709. build_class_init (tree clas, tree expr)
  1710. {
  1711. tree init;
  1712. /* An optimization: if CLAS is a superclass of the class we're
  1713. compiling, we don't need to initialize it. However, if CLAS is
  1714. an interface, it won't necessarily be initialized, even if we
  1715. implement it. */
  1716. if ((! CLASS_INTERFACE (TYPE_NAME (clas))
  1717. && inherits_from_p (current_class, clas))
  1718. || current_class == clas)
  1719. return expr;
  1720. if (always_initialize_class_p)
  1721. {
  1722. init = build_call_nary (void_type_node,
  1723. build_address_of (soft_initclass_node),
  1724. 1, build_class_ref (clas));
  1725. TREE_SIDE_EFFECTS (init) = 1;
  1726. }
  1727. else
  1728. {
  1729. tree *init_test_decl;
  1730. tree decl;
  1731. init_test_decl = java_treetreehash_new
  1732. (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), clas);
  1733. if (*init_test_decl == NULL)
  1734. {
  1735. /* Build a declaration and mark it as a flag used to track
  1736. static class initializations. */
  1737. decl = build_decl (input_location, VAR_DECL, NULL_TREE,
  1738. boolean_type_node);
  1739. MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
  1740. DECL_CONTEXT (decl) = current_function_decl;
  1741. DECL_INITIAL (decl) = boolean_false_node;
  1742. /* Don't emit any symbolic debugging info for this decl. */
  1743. DECL_IGNORED_P (decl) = 1;
  1744. *init_test_decl = decl;
  1745. }
  1746. init = build_call_nary (void_type_node,
  1747. build_address_of (soft_initclass_node),
  1748. 1, build_class_ref (clas));
  1749. TREE_SIDE_EFFECTS (init) = 1;
  1750. init = build3 (COND_EXPR, void_type_node,
  1751. build2 (EQ_EXPR, boolean_type_node,
  1752. *init_test_decl, boolean_false_node),
  1753. init, integer_zero_node);
  1754. TREE_SIDE_EFFECTS (init) = 1;
  1755. init = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init,
  1756. build2 (MODIFY_EXPR, boolean_type_node,
  1757. *init_test_decl, boolean_true_node));
  1758. TREE_SIDE_EFFECTS (init) = 1;
  1759. }
  1760. if (expr != NULL_TREE)
  1761. {
  1762. expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), init, expr);
  1763. TREE_SIDE_EFFECTS (expr) = 1;
  1764. return expr;
  1765. }
  1766. return init;
  1767. }
  1768. /* Rewrite expensive calls that require stack unwinding at runtime to
  1769. cheaper alternatives. The logic here performs these
  1770. transformations:
  1771. java.lang.Class.forName("foo") -> java.lang.Class.forName("foo", class$)
  1772. java.lang.Class.getClassLoader() -> java.lang.Class.getClassLoader(class$)
  1773. */
  1774. typedef struct
  1775. {
  1776. const char *classname;
  1777. const char *method;
  1778. const char *signature;
  1779. const char *new_classname;
  1780. const char *new_signature;
  1781. int flags;
  1782. void (*rewrite_arglist) (vec<tree, va_gc> **);
  1783. } rewrite_rule;
  1784. /* Add __builtin_return_address(0) to the end of an arglist. */
  1785. static void
  1786. rewrite_arglist_getcaller (vec<tree, va_gc> **arglist)
  1787. {
  1788. tree retaddr
  1789. = build_call_expr (builtin_decl_explicit (BUILT_IN_RETURN_ADDRESS),
  1790. 1, integer_zero_node);
  1791. DECL_UNINLINABLE (current_function_decl) = 1;
  1792. vec_safe_push (*arglist, retaddr);
  1793. }
  1794. /* Add this.class to the end of an arglist. */
  1795. static void
  1796. rewrite_arglist_getclass (vec<tree, va_gc> **arglist)
  1797. {
  1798. vec_safe_push (*arglist, build_class_ref (output_class));
  1799. }
  1800. static rewrite_rule rules[] =
  1801. {{"java.lang.Class", "getClassLoader", "()Ljava/lang/ClassLoader;",
  1802. "java.lang.Class", "(Ljava/lang/Class;)Ljava/lang/ClassLoader;",
  1803. ACC_FINAL|ACC_PRIVATE, rewrite_arglist_getclass},
  1804. {"java.lang.Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;",
  1805. "java.lang.Class", "(Ljava/lang/String;Ljava/lang/Class;)Ljava/lang/Class;",
  1806. ACC_FINAL|ACC_PRIVATE|ACC_STATIC, rewrite_arglist_getclass},
  1807. {"gnu.classpath.VMStackWalker", "getCallingClass", "()Ljava/lang/Class;",
  1808. "gnu.classpath.VMStackWalker", "(Lgnu/gcj/RawData;)Ljava/lang/Class;",
  1809. ACC_FINAL|ACC_PRIVATE|ACC_STATIC, rewrite_arglist_getcaller},
  1810. {"gnu.classpath.VMStackWalker", "getCallingClassLoader",
  1811. "()Ljava/lang/ClassLoader;",
  1812. "gnu.classpath.VMStackWalker", "(Lgnu/gcj/RawData;)Ljava/lang/ClassLoader;",
  1813. ACC_FINAL|ACC_PRIVATE|ACC_STATIC, rewrite_arglist_getcaller},
  1814. {"gnu.java.lang.VMCPStringBuilder", "toString", "([CII)Ljava/lang/String;",
  1815. "java.lang.String", "([CII)Ljava/lang/String;",
  1816. ACC_FINAL|ACC_PRIVATE|ACC_STATIC, NULL},
  1817. {NULL, NULL, NULL, NULL, NULL, 0, NULL}};
  1818. /* True if this method is special, i.e. it's a private method that
  1819. should be exported from a DSO. */
  1820. bool
  1821. special_method_p (tree candidate_method)
  1822. {
  1823. tree context = DECL_NAME (TYPE_NAME (DECL_CONTEXT (candidate_method)));
  1824. tree method = DECL_NAME (candidate_method);
  1825. rewrite_rule *p;
  1826. for (p = rules; p->classname; p++)
  1827. {
  1828. if (get_identifier (p->classname) == context
  1829. && get_identifier (p->method) == method)
  1830. return true;
  1831. }
  1832. return false;
  1833. }
  1834. /* Scan the rules list for replacements for *METHOD_P and replace the
  1835. args accordingly. If the rewrite results in an access to a private
  1836. method, update SPECIAL.*/
  1837. void
  1838. maybe_rewrite_invocation (tree *method_p, vec<tree, va_gc> **arg_list_p,
  1839. tree *method_signature_p, tree *special)
  1840. {
  1841. tree context = DECL_NAME (TYPE_NAME (DECL_CONTEXT (*method_p)));
  1842. rewrite_rule *p;
  1843. *special = NULL_TREE;
  1844. for (p = rules; p->classname; p++)
  1845. {
  1846. if (get_identifier (p->classname) == context)
  1847. {
  1848. tree method = DECL_NAME (*method_p);
  1849. if (get_identifier (p->method) == method
  1850. && get_identifier (p->signature) == *method_signature_p)
  1851. {
  1852. tree maybe_method;
  1853. tree destination_class
  1854. = lookup_class (get_identifier (p->new_classname));
  1855. gcc_assert (destination_class);
  1856. maybe_method
  1857. = lookup_java_method (destination_class,
  1858. method,
  1859. get_identifier (p->new_signature));
  1860. if (! maybe_method && ! flag_verify_invocations)
  1861. {
  1862. maybe_method
  1863. = add_method (destination_class, p->flags,
  1864. method, get_identifier (p->new_signature));
  1865. DECL_EXTERNAL (maybe_method) = 1;
  1866. }
  1867. *method_p = maybe_method;
  1868. gcc_assert (*method_p);
  1869. if (p->rewrite_arglist)
  1870. p->rewrite_arglist (arg_list_p);
  1871. *method_signature_p = get_identifier (p->new_signature);
  1872. *special = integer_one_node;
  1873. break;
  1874. }
  1875. }
  1876. }
  1877. }
  1878. tree
  1879. build_known_method_ref (tree method, tree method_type ATTRIBUTE_UNUSED,
  1880. tree self_type, tree method_signature ATTRIBUTE_UNUSED,
  1881. vec<tree, va_gc> *arg_list ATTRIBUTE_UNUSED, tree special)
  1882. {
  1883. tree func;
  1884. if (is_compiled_class (self_type))
  1885. {
  1886. /* With indirect dispatch we have to use indirect calls for all
  1887. publicly visible methods or gcc will use PLT indirections
  1888. to reach them. We also have to use indirect dispatch for all
  1889. external methods. */
  1890. if (! flag_indirect_dispatch
  1891. || (! DECL_EXTERNAL (method) && ! TREE_PUBLIC (method)))
  1892. {
  1893. func = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (method)),
  1894. method);
  1895. }
  1896. else
  1897. {
  1898. tree table_index
  1899. = build_int_cst (NULL_TREE,
  1900. (get_symbol_table_index
  1901. (method, special,
  1902. &TYPE_ATABLE_METHODS (output_class))));
  1903. func
  1904. = build4 (ARRAY_REF,
  1905. TREE_TYPE (TREE_TYPE (TYPE_ATABLE_DECL (output_class))),
  1906. TYPE_ATABLE_DECL (output_class), table_index,
  1907. NULL_TREE, NULL_TREE);
  1908. }
  1909. func = convert (method_ptr_type_node, func);
  1910. }
  1911. else
  1912. {
  1913. /* We don't know whether the method has been (statically) compiled.
  1914. Compile this code to get a reference to the method's code:
  1915. SELF_TYPE->methods[METHOD_INDEX].ncode
  1916. */
  1917. int method_index = 0;
  1918. tree meth, ref;
  1919. /* The method might actually be declared in some superclass, so
  1920. we have to use its class context, not the caller's notion of
  1921. where the method is. */
  1922. self_type = DECL_CONTEXT (method);
  1923. ref = build_class_ref (self_type);
  1924. ref = build1 (INDIRECT_REF, class_type_node, ref);
  1925. if (ncode_ident == NULL_TREE)
  1926. ncode_ident = get_identifier ("ncode");
  1927. if (methods_ident == NULL_TREE)
  1928. methods_ident = get_identifier ("methods");
  1929. ref = build3 (COMPONENT_REF, method_ptr_type_node, ref,
  1930. lookup_field (&class_type_node, methods_ident),
  1931. NULL_TREE);
  1932. for (meth = TYPE_METHODS (self_type);
  1933. ; meth = DECL_CHAIN (meth))
  1934. {
  1935. if (method == meth)
  1936. break;
  1937. if (meth == NULL_TREE)
  1938. fatal_error (input_location, "method '%s' not found in class",
  1939. IDENTIFIER_POINTER (DECL_NAME (method)));
  1940. method_index++;
  1941. }
  1942. method_index *= int_size_in_bytes (method_type_node);
  1943. ref = fold_build_pointer_plus_hwi (ref, method_index);
  1944. ref = build1 (INDIRECT_REF, method_type_node, ref);
  1945. func = build3 (COMPONENT_REF, nativecode_ptr_type_node,
  1946. ref, lookup_field (&method_type_node, ncode_ident),
  1947. NULL_TREE);
  1948. }
  1949. return func;
  1950. }
  1951. tree
  1952. invoke_build_dtable (int is_invoke_interface, vec<tree, va_gc> *arg_list)
  1953. {
  1954. tree dtable, objectref;
  1955. tree saved = save_expr ((*arg_list)[0]);
  1956. (*arg_list)[0] = saved;
  1957. /* If we're dealing with interfaces and if the objectref
  1958. argument is an array then get the dispatch table of the class
  1959. Object rather than the one from the objectref. */
  1960. objectref = (is_invoke_interface
  1961. && is_array_type_p (TREE_TYPE (saved))
  1962. ? build_class_ref (object_type_node) : saved);
  1963. if (dtable_ident == NULL_TREE)
  1964. dtable_ident = get_identifier ("vtable");
  1965. dtable = build_java_indirect_ref (object_type_node, objectref,
  1966. flag_check_references);
  1967. dtable = build3 (COMPONENT_REF, dtable_ptr_type, dtable,
  1968. lookup_field (&object_type_node, dtable_ident), NULL_TREE);
  1969. return dtable;
  1970. }
  1971. /* Determine the index in SYMBOL_TABLE for a reference to the decl
  1972. T. If this decl has not been seen before, it will be added to the
  1973. [oa]table_methods. If it has, the existing table slot will be
  1974. reused. */
  1975. int
  1976. get_symbol_table_index (tree t, tree special,
  1977. vec<method_entry, va_gc> **symbol_table)
  1978. {
  1979. method_entry *e;
  1980. unsigned i;
  1981. method_entry elem = {t, special};
  1982. FOR_EACH_VEC_SAFE_ELT (*symbol_table, i, e)
  1983. if (t == e->method && special == e->special)
  1984. goto done;
  1985. vec_safe_push (*symbol_table, elem);
  1986. done:
  1987. return i + 1;
  1988. }
  1989. tree
  1990. build_invokevirtual (tree dtable, tree method, tree special)
  1991. {
  1992. tree func;
  1993. tree nativecode_ptr_ptr_type_node
  1994. = build_pointer_type (nativecode_ptr_type_node);
  1995. tree method_index;
  1996. tree otable_index;
  1997. if (flag_indirect_dispatch)
  1998. {
  1999. gcc_assert (! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))));
  2000. otable_index
  2001. = build_int_cst (NULL_TREE, get_symbol_table_index
  2002. (method, special,
  2003. &TYPE_OTABLE_METHODS (output_class)));
  2004. method_index = build4 (ARRAY_REF, integer_type_node,
  2005. TYPE_OTABLE_DECL (output_class),
  2006. otable_index, NULL_TREE, NULL_TREE);
  2007. }
  2008. else
  2009. {
  2010. /* We fetch the DECL_VINDEX field directly here, rather than
  2011. using get_method_index(). DECL_VINDEX is the true offset
  2012. from the vtable base to a method, regrdless of any extra
  2013. words inserted at the start of the vtable. */
  2014. method_index = DECL_VINDEX (method);
  2015. method_index = size_binop (MULT_EXPR, method_index,
  2016. TYPE_SIZE_UNIT (nativecode_ptr_ptr_type_node));
  2017. if (TARGET_VTABLE_USES_DESCRIPTORS)
  2018. method_index = size_binop (MULT_EXPR, method_index,
  2019. size_int (TARGET_VTABLE_USES_DESCRIPTORS));
  2020. }
  2021. func = fold_build_pointer_plus (dtable, method_index);
  2022. if (TARGET_VTABLE_USES_DESCRIPTORS)
  2023. func = build1 (NOP_EXPR, nativecode_ptr_type_node, func);
  2024. else
  2025. {
  2026. func = fold_convert (nativecode_ptr_ptr_type_node, func);
  2027. func = build1 (INDIRECT_REF, nativecode_ptr_type_node, func);
  2028. }
  2029. return func;
  2030. }
  2031. static GTY(()) tree class_ident;
  2032. tree
  2033. build_invokeinterface (tree dtable, tree method)
  2034. {
  2035. tree interface;
  2036. tree idx;
  2037. /* We expand invokeinterface here. */
  2038. if (class_ident == NULL_TREE)
  2039. class_ident = get_identifier ("class");
  2040. dtable = build_java_indirect_ref (dtable_type, dtable,
  2041. flag_check_references);
  2042. dtable = build3 (COMPONENT_REF, class_ptr_type, dtable,
  2043. lookup_field (&dtable_type, class_ident), NULL_TREE);
  2044. interface = DECL_CONTEXT (method);
  2045. gcc_assert (CLASS_INTERFACE (TYPE_NAME (interface)));
  2046. layout_class_methods (interface);
  2047. if (flag_indirect_dispatch)
  2048. {
  2049. int itable_index
  2050. = 2 * (get_symbol_table_index
  2051. (method, NULL_TREE, &TYPE_ITABLE_METHODS (output_class)));
  2052. interface
  2053. = build4 (ARRAY_REF,
  2054. TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
  2055. TYPE_ITABLE_DECL (output_class),
  2056. build_int_cst (NULL_TREE, itable_index-1),
  2057. NULL_TREE, NULL_TREE);
  2058. idx
  2059. = build4 (ARRAY_REF,
  2060. TREE_TYPE (TREE_TYPE (TYPE_ITABLE_DECL (output_class))),
  2061. TYPE_ITABLE_DECL (output_class),
  2062. build_int_cst (NULL_TREE, itable_index),
  2063. NULL_TREE, NULL_TREE);
  2064. interface = convert (class_ptr_type, interface);
  2065. idx = convert (integer_type_node, idx);
  2066. }
  2067. else
  2068. {
  2069. idx = build_int_cst (NULL_TREE,
  2070. get_interface_method_index (method, interface));
  2071. interface = build_class_ref (interface);
  2072. }
  2073. return build_call_nary (ptr_type_node,
  2074. build_address_of (soft_lookupinterfacemethod_node),
  2075. 3, dtable, interface, idx);
  2076. }
  2077. /* Expand one of the invoke_* opcodes.
  2078. OPCODE is the specific opcode.
  2079. METHOD_REF_INDEX is an index into the constant pool.
  2080. NARGS is the number of arguments, or -1 if not specified. */
  2081. static void
  2082. expand_invoke (int opcode, int method_ref_index, int nargs ATTRIBUTE_UNUSED)
  2083. {
  2084. tree method_signature
  2085. = COMPONENT_REF_SIGNATURE(&current_jcf->cpool, method_ref_index);
  2086. tree method_name = COMPONENT_REF_NAME (&current_jcf->cpool,
  2087. method_ref_index);
  2088. tree self_type
  2089. = get_class_constant (current_jcf,
  2090. COMPONENT_REF_CLASS_INDEX(&current_jcf->cpool,
  2091. method_ref_index));
  2092. const char *const self_name
  2093. = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
  2094. tree call, func, method, method_type;
  2095. vec<tree, va_gc> *arg_list;
  2096. tree check = NULL_TREE;
  2097. tree special = NULL_TREE;
  2098. if (! CLASS_LOADED_P (self_type))
  2099. {
  2100. load_class (self_type, 1);
  2101. safe_layout_class (self_type);
  2102. if (TREE_CODE (TYPE_SIZE (self_type)) == ERROR_MARK)
  2103. fatal_error (input_location, "failed to find class '%s'", self_name);
  2104. }
  2105. layout_class_methods (self_type);
  2106. if (ID_INIT_P (method_name))
  2107. method = lookup_java_constructor (self_type, method_signature);
  2108. else
  2109. method = lookup_java_method (self_type, method_name, method_signature);
  2110. /* We've found a method in a class other than the one in which it
  2111. was wanted. This can happen if, for instance, we're trying to
  2112. compile invokespecial super.equals().
  2113. FIXME: This is a kludge. Rather than nullifying the result, we
  2114. should change lookup_java_method() so that it doesn't search the
  2115. superclass chain when we're BC-compiling. */
  2116. if (! flag_verify_invocations
  2117. && method
  2118. && ! TYPE_ARRAY_P (self_type)
  2119. && self_type != DECL_CONTEXT (method))
  2120. method = NULL_TREE;
  2121. /* We've found a method in an interface, but this isn't an interface
  2122. call. */
  2123. if (opcode != OPCODE_invokeinterface
  2124. && method
  2125. && (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method)))))
  2126. method = NULL_TREE;
  2127. /* We've found a non-interface method but we are making an
  2128. interface call. This can happen if the interface overrides a
  2129. method in Object. */
  2130. if (! flag_verify_invocations
  2131. && opcode == OPCODE_invokeinterface
  2132. && method
  2133. && ! CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
  2134. method = NULL_TREE;
  2135. if (method == NULL_TREE)
  2136. {
  2137. if (flag_verify_invocations || ! flag_indirect_dispatch)
  2138. {
  2139. error ("class '%s' has no method named '%s' matching signature '%s'",
  2140. self_name,
  2141. IDENTIFIER_POINTER (method_name),
  2142. IDENTIFIER_POINTER (method_signature));
  2143. }
  2144. else
  2145. {
  2146. int flags = ACC_PUBLIC;
  2147. if (opcode == OPCODE_invokestatic)
  2148. flags |= ACC_STATIC;
  2149. if (opcode == OPCODE_invokeinterface)
  2150. {
  2151. flags |= ACC_INTERFACE | ACC_ABSTRACT;
  2152. CLASS_INTERFACE (TYPE_NAME (self_type)) = 1;
  2153. }
  2154. method = add_method (self_type, flags, method_name,
  2155. method_signature);
  2156. DECL_ARTIFICIAL (method) = 1;
  2157. METHOD_DUMMY (method) = 1;
  2158. layout_class_method (self_type, NULL,
  2159. method, NULL);
  2160. }
  2161. }
  2162. /* Invoke static can't invoke static/abstract method */
  2163. if (method != NULL_TREE)
  2164. {
  2165. if (opcode == OPCODE_invokestatic)
  2166. {
  2167. if (!METHOD_STATIC (method))
  2168. {
  2169. error ("invokestatic on non static method");
  2170. method = NULL_TREE;
  2171. }
  2172. else if (METHOD_ABSTRACT (method))
  2173. {
  2174. error ("invokestatic on abstract method");
  2175. method = NULL_TREE;
  2176. }
  2177. }
  2178. else
  2179. {
  2180. if (METHOD_STATIC (method))
  2181. {
  2182. error ("invoke[non-static] on static method");
  2183. method = NULL_TREE;
  2184. }
  2185. }
  2186. }
  2187. if (method == NULL_TREE)
  2188. {
  2189. /* If we got here, we emitted an error message above. So we
  2190. just pop the arguments, push a properly-typed zero, and
  2191. continue. */
  2192. method_type = get_type_from_signature (method_signature);
  2193. pop_arguments (method_type);
  2194. if (opcode != OPCODE_invokestatic)
  2195. pop_type (self_type);
  2196. method_type = promote_type (TREE_TYPE (method_type));
  2197. push_value (convert (method_type, integer_zero_node));
  2198. return;
  2199. }
  2200. arg_list = pop_arguments (TREE_TYPE (method));
  2201. flush_quick_stack ();
  2202. maybe_rewrite_invocation (&method, &arg_list, &method_signature,
  2203. &special);
  2204. method_type = TREE_TYPE (method);
  2205. func = NULL_TREE;
  2206. if (opcode == OPCODE_invokestatic)
  2207. func = build_known_method_ref (method, method_type, self_type,
  2208. method_signature, arg_list, special);
  2209. else if (opcode == OPCODE_invokespecial
  2210. || (opcode == OPCODE_invokevirtual
  2211. && (METHOD_PRIVATE (method)
  2212. || METHOD_FINAL (method)
  2213. || CLASS_FINAL (TYPE_NAME (self_type)))))
  2214. {
  2215. /* If the object for the method call is null, we throw an
  2216. exception. We don't do this if the object is the current
  2217. method's `this'. In other cases we just rely on an
  2218. optimization pass to eliminate redundant checks. FIXME:
  2219. Unfortunately there doesn't seem to be a way to determine
  2220. what the current method is right now.
  2221. We do omit the check if we're calling <init>. */
  2222. /* We use a SAVE_EXPR here to make sure we only evaluate
  2223. the new `self' expression once. */
  2224. tree save_arg = save_expr ((*arg_list)[0]);
  2225. (*arg_list)[0] = save_arg;
  2226. check = java_check_reference (save_arg, ! DECL_INIT_P (method));
  2227. func = build_known_method_ref (method, method_type, self_type,
  2228. method_signature, arg_list, special);
  2229. }
  2230. else
  2231. {
  2232. tree dtable = invoke_build_dtable (opcode == OPCODE_invokeinterface,
  2233. arg_list);
  2234. if (opcode == OPCODE_invokevirtual)
  2235. func = build_invokevirtual (dtable, method, special);
  2236. else
  2237. func = build_invokeinterface (dtable, method);
  2238. }
  2239. if (TREE_CODE (func) == ADDR_EXPR)
  2240. TREE_TYPE (func) = build_pointer_type (method_type);
  2241. else
  2242. func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
  2243. call = build_call_vec (TREE_TYPE (method_type), func, arg_list);
  2244. TREE_SIDE_EFFECTS (call) = 1;
  2245. call = check_for_builtin (method, call);
  2246. if (check != NULL_TREE)
  2247. {
  2248. call = build2 (COMPOUND_EXPR, TREE_TYPE (call), check, call);
  2249. TREE_SIDE_EFFECTS (call) = 1;
  2250. }
  2251. if (TREE_CODE (TREE_TYPE (method_type)) == VOID_TYPE)
  2252. java_add_stmt (call);
  2253. else
  2254. {
  2255. push_value (call);
  2256. flush_quick_stack ();
  2257. }
  2258. }
  2259. /* Create a stub which will be put into the vtable but which will call
  2260. a JNI function. */
  2261. tree
  2262. build_jni_stub (tree method)
  2263. {
  2264. tree jnifunc, call, body, method_sig, arg_types;
  2265. tree jniarg0, jniarg1, jniarg2, jniarg3;
  2266. tree jni_func_type, tem;
  2267. tree env_var, res_var = NULL_TREE, block;
  2268. tree method_args;
  2269. tree meth_var;
  2270. tree bind;
  2271. vec<tree, va_gc> *args = NULL;
  2272. int args_size = 0;
  2273. tree klass = DECL_CONTEXT (method);
  2274. klass = build_class_ref (klass);
  2275. gcc_assert (METHOD_NATIVE (method) && flag_jni);
  2276. DECL_ARTIFICIAL (method) = 1;
  2277. DECL_EXTERNAL (method) = 0;
  2278. env_var = build_decl (input_location,
  2279. VAR_DECL, get_identifier ("env"), ptr_type_node);
  2280. DECL_CONTEXT (env_var) = method;
  2281. if (TREE_TYPE (TREE_TYPE (method)) != void_type_node)
  2282. {
  2283. res_var = build_decl (input_location, VAR_DECL, get_identifier ("res"),
  2284. TREE_TYPE (TREE_TYPE (method)));
  2285. DECL_CONTEXT (res_var) = method;
  2286. DECL_CHAIN (env_var) = res_var;
  2287. }
  2288. method_args = DECL_ARGUMENTS (method);
  2289. block = build_block (env_var, NULL_TREE, method_args, NULL_TREE);
  2290. TREE_SIDE_EFFECTS (block) = 1;
  2291. /* Compute the local `env' by calling _Jv_GetJNIEnvNewFrame. */
  2292. body = build2 (MODIFY_EXPR, ptr_type_node, env_var,
  2293. build_call_nary (ptr_type_node,
  2294. build_address_of (soft_getjnienvnewframe_node),
  2295. 1, klass));
  2296. /* The JNIEnv structure is the first argument to the JNI function. */
  2297. args_size += int_size_in_bytes (TREE_TYPE (env_var));
  2298. vec_safe_push (args, env_var);
  2299. /* For a static method the second argument is the class. For a
  2300. non-static method the second argument is `this'; that is already
  2301. available in the argument list. */
  2302. if (METHOD_STATIC (method))
  2303. {
  2304. args_size += int_size_in_bytes (TREE_TYPE (klass));
  2305. vec_safe_push (args, klass);
  2306. }
  2307. /* All the arguments to this method become arguments to the
  2308. underlying JNI function. If we had to wrap object arguments in a
  2309. special way, we would do that here. */
  2310. for (tem = method_args; tem != NULL_TREE; tem = DECL_CHAIN (tem))
  2311. {
  2312. int arg_bits = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (tem)));
  2313. #ifdef PARM_BOUNDARY
  2314. arg_bits = (((arg_bits + PARM_BOUNDARY - 1) / PARM_BOUNDARY)
  2315. * PARM_BOUNDARY);
  2316. #endif
  2317. args_size += (arg_bits / BITS_PER_UNIT);
  2318. vec_safe_push (args, tem);
  2319. }
  2320. arg_types = TYPE_ARG_TYPES (TREE_TYPE (method));
  2321. /* Argument types for static methods and the JNIEnv structure.
  2322. FIXME: Write and use build_function_type_vec to avoid this. */
  2323. if (METHOD_STATIC (method))
  2324. arg_types = tree_cons (NULL_TREE, object_ptr_type_node, arg_types);
  2325. arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
  2326. /* We call _Jv_LookupJNIMethod to find the actual underlying
  2327. function pointer. _Jv_LookupJNIMethod will throw the appropriate
  2328. exception if this function is not found at runtime. */
  2329. method_sig = build_java_signature (TREE_TYPE (method));
  2330. jniarg0 = klass;
  2331. jniarg1 = build_utf8_ref (DECL_NAME (method));
  2332. jniarg2 = build_utf8_ref (unmangle_classname
  2333. (IDENTIFIER_POINTER (method_sig),
  2334. IDENTIFIER_LENGTH (method_sig)));
  2335. jniarg3 = build_int_cst (NULL_TREE, args_size);
  2336. tem = build_function_type (TREE_TYPE (TREE_TYPE (method)), arg_types);
  2337. #ifdef MODIFY_JNI_METHOD_CALL
  2338. tem = MODIFY_JNI_METHOD_CALL (tem);
  2339. #endif
  2340. jni_func_type = build_pointer_type (tem);
  2341. /* Use the actual function type, rather than a generic pointer type,
  2342. such that this decl keeps the actual pointer type from being
  2343. garbage-collected. If it is, we end up using canonical types
  2344. with different uids for equivalent function types, and this in
  2345. turn causes utf8 identifiers and output order to vary. */
  2346. meth_var = build_decl (input_location,
  2347. VAR_DECL, get_identifier ("meth"), jni_func_type);
  2348. TREE_STATIC (meth_var) = 1;
  2349. TREE_PUBLIC (meth_var) = 0;
  2350. DECL_EXTERNAL (meth_var) = 0;
  2351. DECL_CONTEXT (meth_var) = method;
  2352. DECL_ARTIFICIAL (meth_var) = 1;
  2353. DECL_INITIAL (meth_var) = null_pointer_node;
  2354. TREE_USED (meth_var) = 1;
  2355. chainon (env_var, meth_var);
  2356. build_result_decl (method);
  2357. jnifunc = build3 (COND_EXPR, jni_func_type,
  2358. build2 (NE_EXPR, boolean_type_node,
  2359. meth_var, build_int_cst (TREE_TYPE (meth_var), 0)),
  2360. meth_var,
  2361. build2 (MODIFY_EXPR, jni_func_type, meth_var,
  2362. build1
  2363. (NOP_EXPR, jni_func_type,
  2364. build_call_nary (ptr_type_node,
  2365. build_address_of
  2366. (soft_lookupjnimethod_node),
  2367. 4,
  2368. jniarg0, jniarg1,
  2369. jniarg2, jniarg3))));
  2370. /* Now we make the actual JNI call via the resulting function
  2371. pointer. */
  2372. call = build_call_vec (TREE_TYPE (TREE_TYPE (method)), jnifunc, args);
  2373. /* If the JNI call returned a result, capture it here. If we had to
  2374. unwrap JNI object results, we would do that here. */
  2375. if (res_var != NULL_TREE)
  2376. {
  2377. /* If the call returns an object, it may return a JNI weak
  2378. reference, in which case we must unwrap it. */
  2379. if (! JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_TYPE (method))))
  2380. call = build_call_nary (TREE_TYPE (TREE_TYPE (method)),
  2381. build_address_of (soft_unwrapjni_node),
  2382. 1, call);
  2383. call = build2 (MODIFY_EXPR, TREE_TYPE (TREE_TYPE (method)),
  2384. res_var, call);
  2385. }
  2386. TREE_SIDE_EFFECTS (call) = 1;
  2387. body = build2 (COMPOUND_EXPR, void_type_node, body, call);
  2388. TREE_SIDE_EFFECTS (body) = 1;
  2389. /* Now free the environment we allocated. */
  2390. call = build_call_nary (ptr_type_node,
  2391. build_address_of (soft_jnipopsystemframe_node),
  2392. 1, env_var);
  2393. TREE_SIDE_EFFECTS (call) = 1;
  2394. body = build2 (COMPOUND_EXPR, void_type_node, body, call);
  2395. TREE_SIDE_EFFECTS (body) = 1;
  2396. /* Finally, do the return. */
  2397. if (res_var != NULL_TREE)
  2398. {
  2399. tree drt;
  2400. gcc_assert (DECL_RESULT (method));
  2401. /* Make sure we copy the result variable to the actual
  2402. result. We use the type of the DECL_RESULT because it
  2403. might be different from the return type of the function:
  2404. it might be promoted. */
  2405. drt = TREE_TYPE (DECL_RESULT (method));
  2406. if (drt != TREE_TYPE (res_var))
  2407. res_var = build1 (CONVERT_EXPR, drt, res_var);
  2408. res_var = build2 (MODIFY_EXPR, drt, DECL_RESULT (method), res_var);
  2409. TREE_SIDE_EFFECTS (res_var) = 1;
  2410. }
  2411. body = build2 (COMPOUND_EXPR, void_type_node, body,
  2412. build1 (RETURN_EXPR, void_type_node, res_var));
  2413. TREE_SIDE_EFFECTS (body) = 1;
  2414. /* Prepend class initialization for static methods reachable from
  2415. other classes. */
  2416. if (METHOD_STATIC (method)
  2417. && (! METHOD_PRIVATE (method)
  2418. || INNER_CLASS_P (DECL_CONTEXT (method))))
  2419. {
  2420. tree init = build_call_expr (soft_initclass_node, 1,
  2421. klass);
  2422. body = build2 (COMPOUND_EXPR, void_type_node, init, body);
  2423. TREE_SIDE_EFFECTS (body) = 1;
  2424. }
  2425. bind = build3 (BIND_EXPR, void_type_node, BLOCK_VARS (block),
  2426. body, block);
  2427. return bind;
  2428. }
  2429. /* Given lvalue EXP, return a volatile expression that references the
  2430. same object. */
  2431. tree
  2432. java_modify_addr_for_volatile (tree exp)
  2433. {
  2434. tree exp_type = TREE_TYPE (exp);
  2435. tree v_type
  2436. = build_qualified_type (exp_type,
  2437. TYPE_QUALS (exp_type) | TYPE_QUAL_VOLATILE);
  2438. tree addr = build_fold_addr_expr (exp);
  2439. v_type = build_pointer_type (v_type);
  2440. addr = fold_convert (v_type, addr);
  2441. exp = build_fold_indirect_ref (addr);
  2442. return exp;
  2443. }
  2444. /* Expand an operation to extract from or store into a field.
  2445. IS_STATIC is 1 iff the field is static.
  2446. IS_PUTTING is 1 for putting into a field; 0 for getting from the field.
  2447. FIELD_REF_INDEX is an index into the constant pool. */
  2448. static void
  2449. expand_java_field_op (int is_static, int is_putting, int field_ref_index)
  2450. {
  2451. tree self_type
  2452. = get_class_constant (current_jcf,
  2453. COMPONENT_REF_CLASS_INDEX (&current_jcf->cpool,
  2454. field_ref_index));
  2455. const char *self_name
  2456. = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (self_type)));
  2457. tree field_name = COMPONENT_REF_NAME (&current_jcf->cpool, field_ref_index);
  2458. tree field_signature = COMPONENT_REF_SIGNATURE (&current_jcf->cpool,
  2459. field_ref_index);
  2460. tree field_type = get_type_from_signature (field_signature);
  2461. tree new_value = is_putting ? pop_value (field_type) : NULL_TREE;
  2462. tree field_ref;
  2463. int is_error = 0;
  2464. tree original_self_type = self_type;
  2465. tree field_decl;
  2466. tree modify_expr;
  2467. if (! CLASS_LOADED_P (self_type))
  2468. load_class (self_type, 1);
  2469. field_decl = lookup_field (&self_type, field_name);
  2470. if (field_decl == error_mark_node)
  2471. {
  2472. is_error = 1;
  2473. }
  2474. else if (field_decl == NULL_TREE)
  2475. {
  2476. if (! flag_verify_invocations)
  2477. {
  2478. int flags = ACC_PUBLIC;
  2479. if (is_static)
  2480. flags |= ACC_STATIC;
  2481. self_type = original_self_type;
  2482. field_decl = add_field (original_self_type, field_name,
  2483. field_type, flags);
  2484. DECL_ARTIFICIAL (field_decl) = 1;
  2485. DECL_IGNORED_P (field_decl) = 1;
  2486. #if 0
  2487. /* FIXME: We should be pessimistic about volatility. We
  2488. don't know one way or another, but this is safe.
  2489. However, doing this has bad effects on code quality. We
  2490. need to look at better ways to do this. */
  2491. TREE_THIS_VOLATILE (field_decl) = 1;
  2492. #endif
  2493. }
  2494. else
  2495. {
  2496. error ("missing field '%s' in '%s'",
  2497. IDENTIFIER_POINTER (field_name), self_name);
  2498. is_error = 1;
  2499. }
  2500. }
  2501. else if (build_java_signature (TREE_TYPE (field_decl)) != field_signature)
  2502. {
  2503. error ("mismatching signature for field '%s' in '%s'",
  2504. IDENTIFIER_POINTER (field_name), self_name);
  2505. is_error = 1;
  2506. }
  2507. field_ref = is_static ? NULL_TREE : pop_value (self_type);
  2508. if (is_error)
  2509. {
  2510. if (! is_putting)
  2511. push_value (convert (field_type, integer_zero_node));
  2512. flush_quick_stack ();
  2513. return;
  2514. }
  2515. field_ref = build_field_ref (field_ref, self_type, field_name);
  2516. if (is_static
  2517. && ! flag_indirect_dispatch)
  2518. {
  2519. tree context = DECL_CONTEXT (field_ref);
  2520. if (context != self_type && CLASS_INTERFACE (TYPE_NAME (context)))
  2521. field_ref = build_class_init (context, field_ref);
  2522. else
  2523. field_ref = build_class_init (self_type, field_ref);
  2524. }
  2525. if (is_putting)
  2526. {
  2527. flush_quick_stack ();
  2528. if (FIELD_FINAL (field_decl))
  2529. {
  2530. if (DECL_CONTEXT (field_decl) != current_class)
  2531. error ("assignment to final field %q+D not in field%'s class",
  2532. field_decl);
  2533. /* We used to check for assignments to final fields not
  2534. occurring in the class initializer or in a constructor
  2535. here. However, this constraint doesn't seem to be
  2536. enforced by the JVM. */
  2537. }
  2538. if (TREE_THIS_VOLATILE (field_decl))
  2539. field_ref = java_modify_addr_for_volatile (field_ref);
  2540. modify_expr = build2 (MODIFY_EXPR, TREE_TYPE (field_ref),
  2541. field_ref, new_value);
  2542. if (TREE_THIS_VOLATILE (field_decl))
  2543. {
  2544. tree sync = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
  2545. java_add_stmt (build_call_expr (sync, 0));
  2546. }
  2547. java_add_stmt (modify_expr);
  2548. }
  2549. else
  2550. {
  2551. tree temp = build_decl (input_location,
  2552. VAR_DECL, NULL_TREE, TREE_TYPE (field_ref));
  2553. java_add_local_var (temp);
  2554. if (TREE_THIS_VOLATILE (field_decl))
  2555. field_ref = java_modify_addr_for_volatile (field_ref);
  2556. modify_expr
  2557. = build2 (MODIFY_EXPR, TREE_TYPE (field_ref), temp, field_ref);
  2558. java_add_stmt (modify_expr);
  2559. if (TREE_THIS_VOLATILE (field_decl))
  2560. {
  2561. tree sync = builtin_decl_explicit (BUILT_IN_SYNC_SYNCHRONIZE);
  2562. java_add_stmt (build_call_expr (sync, 0));
  2563. }
  2564. push_value (temp);
  2565. }
  2566. TREE_THIS_VOLATILE (field_ref) = TREE_THIS_VOLATILE (field_decl);
  2567. }
  2568. static void
  2569. load_type_state (int pc)
  2570. {
  2571. int i;
  2572. tree vec = (*type_states)[pc];
  2573. int cur_length = TREE_VEC_LENGTH (vec);
  2574. stack_pointer = cur_length - DECL_MAX_LOCALS(current_function_decl);
  2575. for (i = 0; i < cur_length; i++)
  2576. type_map [i] = TREE_VEC_ELT (vec, i);
  2577. }
  2578. /* Go over METHOD's bytecode and note instruction starts in
  2579. instruction_bits[]. */
  2580. void
  2581. note_instructions (JCF *jcf, tree method)
  2582. {
  2583. int PC;
  2584. unsigned char* byte_ops;
  2585. long length = DECL_CODE_LENGTH (method);
  2586. int saw_index;
  2587. jint INT_temp;
  2588. #undef RET /* Defined by config/i386/i386.h */
  2589. #undef PTR
  2590. #define BCODE byte_ops
  2591. #define BYTE_type_node byte_type_node
  2592. #define SHORT_type_node short_type_node
  2593. #define INT_type_node int_type_node
  2594. #define LONG_type_node long_type_node
  2595. #define CHAR_type_node char_type_node
  2596. #define PTR_type_node ptr_type_node
  2597. #define FLOAT_type_node float_type_node
  2598. #define DOUBLE_type_node double_type_node
  2599. #define VOID_type_node void_type_node
  2600. #define CONST_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
  2601. #define CONST_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
  2602. #define VAR_INDEX_1 (saw_index = 1, IMMEDIATE_u1)
  2603. #define VAR_INDEX_2 (saw_index = 1, IMMEDIATE_u2)
  2604. #define CHECK_PC_IN_RANGE(PC) ((void)1) /* Already handled by verifier. */
  2605. JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
  2606. byte_ops = jcf->read_ptr;
  2607. instruction_bits = XRESIZEVAR (char, instruction_bits, length + 1);
  2608. memset (instruction_bits, 0, length + 1);
  2609. vec_alloc (type_states, length + 1);
  2610. type_states->quick_grow_cleared (length + 1);
  2611. /* This pass figures out which PC can be the targets of jumps. */
  2612. for (PC = 0; PC < length;)
  2613. {
  2614. int oldpc = PC; /* PC at instruction start. */
  2615. instruction_bits [PC] |= BCODE_INSTRUCTION_START;
  2616. switch (byte_ops[PC++])
  2617. {
  2618. #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
  2619. case OPCODE: \
  2620. PRE_##OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
  2621. break;
  2622. #define NOTE_LABEL(PC) note_label(oldpc, PC)
  2623. #define PRE_PUSHC(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
  2624. #define PRE_LOAD(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
  2625. #define PRE_STORE(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE);
  2626. #define PRE_STACK(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
  2627. #define PRE_UNOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
  2628. #define PRE_BINOP(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
  2629. #define PRE_CONVERT(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
  2630. #define PRE_CONVERT2(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
  2631. #define PRE_SPECIAL(OPERAND_TYPE, INSTRUCTION) \
  2632. PRE_SPECIAL_##INSTRUCTION(OPERAND_TYPE)
  2633. #define PRE_SPECIAL_IINC(OPERAND_TYPE) \
  2634. ((void) IMMEDIATE_u1, (void) IMMEDIATE_s1)
  2635. #define PRE_SPECIAL_ENTER(IGNORE) /* nothing */
  2636. #define PRE_SPECIAL_EXIT(IGNORE) /* nothing */
  2637. #define PRE_SPECIAL_THROW(IGNORE) /* nothing */
  2638. #define PRE_SPECIAL_BREAK(IGNORE) /* nothing */
  2639. /* two forms of wide instructions */
  2640. #define PRE_SPECIAL_WIDE(IGNORE) \
  2641. { \
  2642. int modified_opcode = IMMEDIATE_u1; \
  2643. if (modified_opcode == OPCODE_iinc) \
  2644. { \
  2645. (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
  2646. (void) IMMEDIATE_s2; /* constbyte1 and constbyte2 */ \
  2647. } \
  2648. else \
  2649. { \
  2650. (void) IMMEDIATE_u2; /* indexbyte1 and indexbyte2 */ \
  2651. } \
  2652. }
  2653. #define PRE_IMPL(IGNORE1, IGNORE2) /* nothing */
  2654. #define PRE_MONITOR(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
  2655. #define PRE_RETURN(OPERAND_TYPE, OPERAND_VALUE) /* nothing */
  2656. #define PRE_ARRAY(OPERAND_TYPE, SUBOP) \
  2657. PRE_ARRAY_##SUBOP(OPERAND_TYPE)
  2658. #define PRE_ARRAY_LOAD(TYPE) /* nothing */
  2659. #define PRE_ARRAY_STORE(TYPE) /* nothing */
  2660. #define PRE_ARRAY_LENGTH(TYPE) /* nothing */
  2661. #define PRE_ARRAY_NEW(TYPE) PRE_ARRAY_NEW_##TYPE
  2662. #define PRE_ARRAY_NEW_NUM ((void) IMMEDIATE_u1)
  2663. #define PRE_ARRAY_NEW_PTR ((void) IMMEDIATE_u2)
  2664. #define PRE_ARRAY_NEW_MULTI ((void) IMMEDIATE_u2, (void) IMMEDIATE_u1)
  2665. #define PRE_TEST(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
  2666. #define PRE_COND(OPERAND_TYPE, OPERAND_VALUE) NOTE_LABEL (oldpc+IMMEDIATE_s2)
  2667. #define PRE_BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
  2668. saw_index = 0; INT_temp = (OPERAND_VALUE); \
  2669. if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
  2670. #define PRE_JSR(OPERAND_TYPE, OPERAND_VALUE) \
  2671. saw_index = 0; INT_temp = (OPERAND_VALUE); \
  2672. NOTE_LABEL (PC); \
  2673. if (!saw_index) NOTE_LABEL(oldpc + INT_temp);
  2674. #define PRE_RET(OPERAND_TYPE, OPERAND_VALUE) (void)(OPERAND_VALUE)
  2675. #define PRE_SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
  2676. PC = (PC + 3) / 4 * 4; PRE_##TABLE_OR_LOOKUP##_SWITCH
  2677. #define PRE_LOOKUP_SWITCH \
  2678. { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
  2679. NOTE_LABEL (default_offset+oldpc); \
  2680. if (npairs >= 0) \
  2681. while (--npairs >= 0) { \
  2682. jint match ATTRIBUTE_UNUSED = IMMEDIATE_s4; \
  2683. jint offset = IMMEDIATE_s4; \
  2684. NOTE_LABEL (offset+oldpc); } \
  2685. }
  2686. #define PRE_TABLE_SWITCH \
  2687. { jint default_offset = IMMEDIATE_s4; \
  2688. jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
  2689. NOTE_LABEL (default_offset+oldpc); \
  2690. if (low <= high) \
  2691. while (low++ <= high) { \
  2692. jint offset = IMMEDIATE_s4; \
  2693. NOTE_LABEL (offset+oldpc); } \
  2694. }
  2695. #define PRE_FIELD(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
  2696. #define PRE_OBJECT(MAYBE_STATIC, PUT_OR_GET) (void)(IMMEDIATE_u2);
  2697. #define PRE_INVOKE(MAYBE_STATIC, IS_INTERFACE) \
  2698. (void)(IMMEDIATE_u2); \
  2699. PC += 2 * IS_INTERFACE /* for invokeinterface */;
  2700. #include "javaop.def"
  2701. #undef JAVAOP
  2702. }
  2703. } /* for */
  2704. }
  2705. void
  2706. expand_byte_code (JCF *jcf, tree method)
  2707. {
  2708. int PC;
  2709. int i;
  2710. const unsigned char *linenumber_pointer;
  2711. int dead_code_index = -1;
  2712. unsigned char* byte_ops;
  2713. long length = DECL_CODE_LENGTH (method);
  2714. location_t max_location = input_location;
  2715. stack_pointer = 0;
  2716. JCF_SEEK (jcf, DECL_CODE_OFFSET (method));
  2717. byte_ops = jcf->read_ptr;
  2718. /* We make an initial pass of the line number table, to note
  2719. which instructions have associated line number entries. */
  2720. linenumber_pointer = linenumber_table;
  2721. for (i = 0; i < linenumber_count; i++)
  2722. {
  2723. int pc = GET_u2 (linenumber_pointer);
  2724. linenumber_pointer += 4;
  2725. if (pc >= length)
  2726. warning (0, "invalid PC in line number table");
  2727. else
  2728. {
  2729. if ((instruction_bits[pc] & BCODE_HAS_LINENUMBER) != 0)
  2730. instruction_bits[pc] |= BCODE_HAS_MULTI_LINENUMBERS;
  2731. instruction_bits[pc] |= BCODE_HAS_LINENUMBER;
  2732. }
  2733. }
  2734. if (! verify_jvm_instructions_new (jcf, byte_ops, length))
  2735. return;
  2736. promote_arguments ();
  2737. cache_this_class_ref (method);
  2738. cache_cpool_data_ref ();
  2739. /* Translate bytecodes. */
  2740. linenumber_pointer = linenumber_table;
  2741. for (PC = 0; PC < length;)
  2742. {
  2743. if ((instruction_bits [PC] & BCODE_TARGET) != 0 || PC == 0)
  2744. {
  2745. tree label = lookup_label (PC);
  2746. flush_quick_stack ();
  2747. if ((instruction_bits [PC] & BCODE_TARGET) != 0)
  2748. java_add_stmt (build1 (LABEL_EXPR, void_type_node, label));
  2749. if ((instruction_bits[PC] & BCODE_VERIFIED) != 0)
  2750. load_type_state (PC);
  2751. }
  2752. if (! (instruction_bits [PC] & BCODE_VERIFIED))
  2753. {
  2754. if (dead_code_index == -1)
  2755. {
  2756. /* This is the start of a region of unreachable bytecodes.
  2757. They still need to be processed in order for EH ranges
  2758. to get handled correctly. However, we can simply
  2759. replace these bytecodes with nops. */
  2760. dead_code_index = PC;
  2761. }
  2762. /* Turn this bytecode into a nop. */
  2763. byte_ops[PC] = 0x0;
  2764. }
  2765. else
  2766. {
  2767. if (dead_code_index != -1)
  2768. {
  2769. /* We've just reached the end of a region of dead code. */
  2770. if (extra_warnings)
  2771. warning (0, "unreachable bytecode from %d to before %d",
  2772. dead_code_index, PC);
  2773. dead_code_index = -1;
  2774. }
  2775. }
  2776. /* Handle possible line number entry for this PC.
  2777. This code handles out-of-order and multiple linenumbers per PC,
  2778. but is optimized for the case of line numbers increasing
  2779. monotonically with PC. */
  2780. if ((instruction_bits[PC] & BCODE_HAS_LINENUMBER) != 0)
  2781. {
  2782. if ((instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS) != 0
  2783. || GET_u2 (linenumber_pointer) != PC)
  2784. linenumber_pointer = linenumber_table;
  2785. while (linenumber_pointer < linenumber_table + linenumber_count * 4)
  2786. {
  2787. int pc = GET_u2 (linenumber_pointer);
  2788. linenumber_pointer += 4;
  2789. if (pc == PC)
  2790. {
  2791. int line = GET_u2 (linenumber_pointer - 2);
  2792. input_location = linemap_line_start (line_table, line, 1);
  2793. if (input_location > max_location)
  2794. max_location = input_location;
  2795. if (!(instruction_bits[PC] & BCODE_HAS_MULTI_LINENUMBERS))
  2796. break;
  2797. }
  2798. }
  2799. }
  2800. maybe_pushlevels (PC);
  2801. PC = process_jvm_instruction (PC, byte_ops, length);
  2802. maybe_poplevels (PC);
  2803. } /* for */
  2804. uncache_this_class_ref (method);
  2805. if (dead_code_index != -1)
  2806. {
  2807. /* We've just reached the end of a region of dead code. */
  2808. if (extra_warnings)
  2809. warning (0, "unreachable bytecode from %d to the end of the method",
  2810. dead_code_index);
  2811. }
  2812. DECL_FUNCTION_LAST_LINE (method) = max_location;
  2813. }
  2814. static void
  2815. java_push_constant_from_pool (JCF *jcf, int index)
  2816. {
  2817. tree c;
  2818. if (JPOOL_TAG (jcf, index) == CONSTANT_String)
  2819. {
  2820. tree name;
  2821. name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
  2822. index = alloc_name_constant (CONSTANT_String, name);
  2823. c = build_ref_from_constant_pool (index);
  2824. c = convert (promote_type (string_type_node), c);
  2825. }
  2826. else if (JPOOL_TAG (jcf, index) == CONSTANT_Class
  2827. || JPOOL_TAG (jcf, index) == CONSTANT_ResolvedClass)
  2828. {
  2829. tree record = get_class_constant (jcf, index);
  2830. c = build_class_ref (record);
  2831. }
  2832. else
  2833. c = get_constant (jcf, index);
  2834. push_value (c);
  2835. }
  2836. int
  2837. process_jvm_instruction (int PC, const unsigned char* byte_ops,
  2838. long length ATTRIBUTE_UNUSED)
  2839. {
  2840. const char *opname; /* Temporary ??? */
  2841. int oldpc = PC; /* PC at instruction start. */
  2842. /* If the instruction is at the beginning of an exception handler,
  2843. replace the top of the stack with the thrown object reference. */
  2844. if (instruction_bits [PC] & BCODE_EXCEPTION_TARGET)
  2845. {
  2846. /* Note that the verifier will not emit a type map at all for
  2847. dead exception handlers. In this case we just ignore the
  2848. situation. */
  2849. if ((instruction_bits[PC] & BCODE_VERIFIED) != 0)
  2850. {
  2851. tree type = pop_type (promote_type (throwable_type_node));
  2852. push_value (build_exception_object_ref (type));
  2853. }
  2854. }
  2855. switch (byte_ops[PC++])
  2856. {
  2857. #define JAVAOP(OPNAME, OPCODE, OPKIND, OPERAND_TYPE, OPERAND_VALUE) \
  2858. case OPCODE: \
  2859. opname = #OPNAME; \
  2860. OPKIND(OPERAND_TYPE, OPERAND_VALUE); \
  2861. break;
  2862. #define RET(OPERAND_TYPE, OPERAND_VALUE) \
  2863. { \
  2864. int saw_index = 0; \
  2865. int index = OPERAND_VALUE; \
  2866. (void) saw_index; /* Avoid set but not used warning. */ \
  2867. build_java_ret \
  2868. (find_local_variable (index, return_address_type_node, oldpc)); \
  2869. }
  2870. #define JSR(OPERAND_TYPE, OPERAND_VALUE) \
  2871. { \
  2872. /* OPERAND_VALUE may have side-effects on PC */ \
  2873. int opvalue = OPERAND_VALUE; \
  2874. build_java_jsr (oldpc + opvalue, PC); \
  2875. }
  2876. /* Push a constant onto the stack. */
  2877. #define PUSHC(OPERAND_TYPE, OPERAND_VALUE) \
  2878. { int saw_index = 0; int ival = (OPERAND_VALUE); \
  2879. if (saw_index) java_push_constant_from_pool (current_jcf, ival); \
  2880. else expand_java_pushc (ival, OPERAND_TYPE##_type_node); }
  2881. /* internal macro added for use by the WIDE case */
  2882. #define LOAD_INTERNAL(OPTYPE, OPVALUE) \
  2883. expand_load_internal (OPVALUE, type_map[OPVALUE], oldpc);
  2884. /* Push local variable onto the opcode stack. */
  2885. #define LOAD(OPERAND_TYPE, OPERAND_VALUE) \
  2886. { \
  2887. /* have to do this since OPERAND_VALUE may have side-effects */ \
  2888. int opvalue = OPERAND_VALUE; \
  2889. LOAD_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
  2890. }
  2891. #define RETURN(OPERAND_TYPE, OPERAND_VALUE) \
  2892. expand_java_return (OPERAND_TYPE##_type_node)
  2893. #define REM_EXPR TRUNC_MOD_EXPR
  2894. #define BINOP(OPERAND_TYPE, OPERAND_VALUE) \
  2895. expand_java_binop (OPERAND_TYPE##_type_node, OPERAND_VALUE##_EXPR)
  2896. #define FIELD(IS_STATIC, IS_PUT) \
  2897. expand_java_field_op (IS_STATIC, IS_PUT, IMMEDIATE_u2)
  2898. #define TEST(OPERAND_TYPE, CONDITION) \
  2899. expand_test (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
  2900. #define COND(OPERAND_TYPE, CONDITION) \
  2901. expand_cond (CONDITION##_EXPR, OPERAND_TYPE##_type_node, oldpc+IMMEDIATE_s2)
  2902. #define BRANCH(OPERAND_TYPE, OPERAND_VALUE) \
  2903. BRANCH_##OPERAND_TYPE (OPERAND_VALUE)
  2904. #define BRANCH_GOTO(OPERAND_VALUE) \
  2905. expand_java_goto (oldpc + OPERAND_VALUE)
  2906. #define BRANCH_CALL(OPERAND_VALUE) \
  2907. expand_java_call (oldpc + OPERAND_VALUE, oldpc)
  2908. #if 0
  2909. #define BRANCH_RETURN(OPERAND_VALUE) \
  2910. { \
  2911. tree type = OPERAND_TYPE##_type_node; \
  2912. tree value = find_local_variable (OPERAND_VALUE, type, oldpc); \
  2913. expand_java_ret (value); \
  2914. }
  2915. #endif
  2916. #define NOT_IMPL(OPERAND_TYPE, OPERAND_VALUE) \
  2917. fprintf (stderr, "%3d: %s ", oldpc, opname); \
  2918. fprintf (stderr, "(not implemented)\n")
  2919. #define NOT_IMPL1(OPERAND_VALUE) \
  2920. fprintf (stderr, "%3d: %s ", oldpc, opname); \
  2921. fprintf (stderr, "(not implemented)\n")
  2922. #define BRANCH_RETURN(OPERAND_VALUE) NOT_IMPL1(OPERAND_VALUE)
  2923. #define STACK(SUBOP, COUNT) STACK_##SUBOP (COUNT)
  2924. #define STACK_POP(COUNT) java_stack_pop (COUNT)
  2925. #define STACK_SWAP(COUNT) java_stack_swap()
  2926. #define STACK_DUP(COUNT) java_stack_dup (COUNT, 0)
  2927. #define STACK_DUPx1(COUNT) java_stack_dup (COUNT, 1)
  2928. #define STACK_DUPx2(COUNT) java_stack_dup (COUNT, 2)
  2929. #define SWITCH(OPERAND_TYPE, TABLE_OR_LOOKUP) \
  2930. PC = (PC + 3) / 4 * 4; TABLE_OR_LOOKUP##_SWITCH
  2931. #define LOOKUP_SWITCH \
  2932. { jint default_offset = IMMEDIATE_s4; jint npairs = IMMEDIATE_s4; \
  2933. tree selector = pop_value (INT_type_node); \
  2934. tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
  2935. while (--npairs >= 0) \
  2936. { \
  2937. jint match = IMMEDIATE_s4; jint offset = IMMEDIATE_s4; \
  2938. expand_java_add_case (switch_expr, match, oldpc + offset); \
  2939. } \
  2940. }
  2941. #define TABLE_SWITCH \
  2942. { jint default_offset = IMMEDIATE_s4; \
  2943. jint low = IMMEDIATE_s4; jint high = IMMEDIATE_s4; \
  2944. tree selector = pop_value (INT_type_node); \
  2945. tree switch_expr = expand_java_switch (selector, oldpc + default_offset); \
  2946. for (; low <= high; low++) \
  2947. { \
  2948. jint offset = IMMEDIATE_s4; \
  2949. expand_java_add_case (switch_expr, low, oldpc + offset); \
  2950. } \
  2951. }
  2952. #define INVOKE(MAYBE_STATIC, IS_INTERFACE) \
  2953. { int opcode = byte_ops[PC-1]; \
  2954. int method_ref_index = IMMEDIATE_u2; \
  2955. int nargs; \
  2956. if (IS_INTERFACE) { nargs = IMMEDIATE_u1; (void) IMMEDIATE_u1; } \
  2957. else nargs = -1; \
  2958. expand_invoke (opcode, method_ref_index, nargs); \
  2959. }
  2960. /* Handle new, checkcast, instanceof */
  2961. #define OBJECT(TYPE, OP) \
  2962. expand_java_##OP (get_class_constant (current_jcf, IMMEDIATE_u2))
  2963. #define ARRAY(OPERAND_TYPE, SUBOP) ARRAY_##SUBOP(OPERAND_TYPE)
  2964. #define ARRAY_LOAD(OPERAND_TYPE) \
  2965. { \
  2966. expand_java_arrayload( OPERAND_TYPE##_type_node ); \
  2967. }
  2968. #define ARRAY_STORE(OPERAND_TYPE) \
  2969. { \
  2970. expand_java_arraystore( OPERAND_TYPE##_type_node ); \
  2971. }
  2972. #define ARRAY_LENGTH(OPERAND_TYPE) expand_java_array_length();
  2973. #define ARRAY_NEW(OPERAND_TYPE) ARRAY_NEW_##OPERAND_TYPE()
  2974. #define ARRAY_NEW_PTR() \
  2975. push_value (build_anewarray (get_class_constant (current_jcf, \
  2976. IMMEDIATE_u2), \
  2977. pop_value (int_type_node)));
  2978. #define ARRAY_NEW_NUM() \
  2979. { \
  2980. int atype = IMMEDIATE_u1; \
  2981. push_value (build_newarray (atype, pop_value (int_type_node)));\
  2982. }
  2983. #define ARRAY_NEW_MULTI() \
  2984. { \
  2985. tree klass = get_class_constant (current_jcf, IMMEDIATE_u2 ); \
  2986. int ndims = IMMEDIATE_u1; \
  2987. expand_java_multianewarray( klass, ndims ); \
  2988. }
  2989. #define UNOP(OPERAND_TYPE, OPERAND_VALUE) \
  2990. push_value (fold_build1 (NEGATE_EXPR, OPERAND_TYPE##_type_node, \
  2991. pop_value (OPERAND_TYPE##_type_node)));
  2992. #define CONVERT2(FROM_TYPE, TO_TYPE) \
  2993. { \
  2994. push_value (build1 (NOP_EXPR, int_type_node, \
  2995. (convert (TO_TYPE##_type_node, \
  2996. pop_value (FROM_TYPE##_type_node))))); \
  2997. }
  2998. #define CONVERT(FROM_TYPE, TO_TYPE) \
  2999. { \
  3000. push_value (convert (TO_TYPE##_type_node, \
  3001. pop_value (FROM_TYPE##_type_node))); \
  3002. }
  3003. /* internal macro added for use by the WIDE case
  3004. Added TREE_TYPE (decl) assignment, apbianco */
  3005. #define STORE_INTERNAL(OPTYPE, OPVALUE) \
  3006. { \
  3007. tree decl, value; \
  3008. int index = OPVALUE; \
  3009. tree type = OPTYPE; \
  3010. value = pop_value (type); \
  3011. type = TREE_TYPE (value); \
  3012. decl = find_local_variable (index, type, oldpc); \
  3013. set_local_type (index, type); \
  3014. java_add_stmt (build2 (MODIFY_EXPR, type, decl, value)); \
  3015. }
  3016. #define STORE(OPERAND_TYPE, OPERAND_VALUE) \
  3017. { \
  3018. /* have to do this since OPERAND_VALUE may have side-effects */ \
  3019. int opvalue = OPERAND_VALUE; \
  3020. STORE_INTERNAL(OPERAND_TYPE##_type_node, opvalue); \
  3021. }
  3022. #define SPECIAL(OPERAND_TYPE, INSTRUCTION) \
  3023. SPECIAL_##INSTRUCTION(OPERAND_TYPE)
  3024. #define SPECIAL_ENTER(IGNORED) MONITOR_OPERATION (soft_monitorenter_node)
  3025. #define SPECIAL_EXIT(IGNORED) MONITOR_OPERATION (soft_monitorexit_node)
  3026. #define MONITOR_OPERATION(call) \
  3027. { \
  3028. tree o = pop_value (ptr_type_node); \
  3029. tree c; \
  3030. flush_quick_stack (); \
  3031. c = build_java_monitor (call, o); \
  3032. TREE_SIDE_EFFECTS (c) = 1; \
  3033. java_add_stmt (c); \
  3034. }
  3035. #define SPECIAL_IINC(IGNORED) \
  3036. { \
  3037. unsigned int local_var_index = IMMEDIATE_u1; \
  3038. int ival = IMMEDIATE_s1; \
  3039. expand_iinc(local_var_index, ival, oldpc); \
  3040. }
  3041. #define SPECIAL_WIDE(IGNORED) \
  3042. { \
  3043. int modified_opcode = IMMEDIATE_u1; \
  3044. unsigned int local_var_index = IMMEDIATE_u2; \
  3045. switch (modified_opcode) \
  3046. { \
  3047. case OPCODE_iinc: \
  3048. { \
  3049. int ival = IMMEDIATE_s2; \
  3050. expand_iinc (local_var_index, ival, oldpc); \
  3051. break; \
  3052. } \
  3053. case OPCODE_iload: \
  3054. case OPCODE_lload: \
  3055. case OPCODE_fload: \
  3056. case OPCODE_dload: \
  3057. case OPCODE_aload: \
  3058. { \
  3059. /* duplicate code from LOAD macro */ \
  3060. LOAD_INTERNAL(operand_type[modified_opcode], local_var_index); \
  3061. break; \
  3062. } \
  3063. case OPCODE_istore: \
  3064. case OPCODE_lstore: \
  3065. case OPCODE_fstore: \
  3066. case OPCODE_dstore: \
  3067. case OPCODE_astore: \
  3068. { \
  3069. STORE_INTERNAL(operand_type[modified_opcode], local_var_index); \
  3070. break; \
  3071. } \
  3072. default: \
  3073. error ("unrecognized wide sub-instruction"); \
  3074. } \
  3075. }
  3076. #define SPECIAL_THROW(IGNORED) \
  3077. build_java_athrow (pop_value (throwable_type_node))
  3078. #define SPECIAL_BREAK NOT_IMPL1
  3079. #define IMPL NOT_IMPL
  3080. #include "javaop.def"
  3081. #undef JAVAOP
  3082. default:
  3083. fprintf (stderr, "%3d: unknown(%3d)\n", oldpc, byte_ops[PC]);
  3084. }
  3085. return PC;
  3086. }
  3087. /* Return the opcode at PC in the code section pointed to by
  3088. CODE_OFFSET. */
  3089. static unsigned char
  3090. peek_opcode_at_pc (JCF *jcf, int code_offset, int pc)
  3091. {
  3092. unsigned char opcode;
  3093. long absolute_offset = (long)JCF_TELL (jcf);
  3094. JCF_SEEK (jcf, code_offset);
  3095. opcode = jcf->read_ptr [pc];
  3096. JCF_SEEK (jcf, absolute_offset);
  3097. return opcode;
  3098. }
  3099. /* Some bytecode compilers are emitting accurate LocalVariableTable
  3100. attributes. Here's an example:
  3101. PC <t>store_<n>
  3102. PC+1 ...
  3103. Attribute "LocalVariableTable"
  3104. slot #<n>: ... (PC: PC+1 length: L)
  3105. This is accurate because the local in slot <n> really exists after
  3106. the opcode at PC is executed, hence from PC+1 to PC+1+L.
  3107. This procedure recognizes this situation and extends the live range
  3108. of the local in SLOT to START_PC-1 or START_PC-2 (depending on the
  3109. length of the store instruction.)
  3110. This function is used by `give_name_to_locals' so that a local's
  3111. DECL features a DECL_LOCAL_START_PC such that the first related
  3112. store operation will use DECL as a destination, not an unrelated
  3113. temporary created for the occasion.
  3114. This function uses a global (instruction_bits) `note_instructions' should
  3115. have allocated and filled properly. */
  3116. int
  3117. maybe_adjust_start_pc (struct JCF *jcf, int code_offset,
  3118. int start_pc, int slot)
  3119. {
  3120. int first, index, opcode;
  3121. int pc, insn_pc;
  3122. int wide_found = 0;
  3123. if (!start_pc)
  3124. return start_pc;
  3125. first = index = -1;
  3126. /* Find last previous instruction and remember it */
  3127. for (pc = start_pc-1; pc; pc--)
  3128. if (instruction_bits [pc] & BCODE_INSTRUCTION_START)
  3129. break;
  3130. insn_pc = pc;
  3131. /* Retrieve the instruction, handle `wide'. */
  3132. opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
  3133. if (opcode == OPCODE_wide)
  3134. {
  3135. wide_found = 1;
  3136. opcode = (int) peek_opcode_at_pc (jcf, code_offset, pc++);
  3137. }
  3138. switch (opcode)
  3139. {
  3140. case OPCODE_astore_0:
  3141. case OPCODE_astore_1:
  3142. case OPCODE_astore_2:
  3143. case OPCODE_astore_3:
  3144. first = OPCODE_astore_0;
  3145. break;
  3146. case OPCODE_istore_0:
  3147. case OPCODE_istore_1:
  3148. case OPCODE_istore_2:
  3149. case OPCODE_istore_3:
  3150. first = OPCODE_istore_0;
  3151. break;
  3152. case OPCODE_lstore_0:
  3153. case OPCODE_lstore_1:
  3154. case OPCODE_lstore_2:
  3155. case OPCODE_lstore_3:
  3156. first = OPCODE_lstore_0;
  3157. break;
  3158. case OPCODE_fstore_0:
  3159. case OPCODE_fstore_1:
  3160. case OPCODE_fstore_2:
  3161. case OPCODE_fstore_3:
  3162. first = OPCODE_fstore_0;
  3163. break;
  3164. case OPCODE_dstore_0:
  3165. case OPCODE_dstore_1:
  3166. case OPCODE_dstore_2:
  3167. case OPCODE_dstore_3:
  3168. first = OPCODE_dstore_0;
  3169. break;
  3170. case OPCODE_astore:
  3171. case OPCODE_istore:
  3172. case OPCODE_lstore:
  3173. case OPCODE_fstore:
  3174. case OPCODE_dstore:
  3175. index = peek_opcode_at_pc (jcf, code_offset, pc);
  3176. if (wide_found)
  3177. {
  3178. int other = peek_opcode_at_pc (jcf, code_offset, ++pc);
  3179. index = (other << 8) + index;
  3180. }
  3181. break;
  3182. }
  3183. /* Now we decide: first >0 means we have a <t>store_<n>, index >0
  3184. means we have a <t>store. */
  3185. if ((first > 0 && opcode - first == slot) || (index > 0 && index == slot))
  3186. start_pc = insn_pc;
  3187. return start_pc;
  3188. }
  3189. /* Build a node to represent empty statements and blocks. */
  3190. tree
  3191. build_java_empty_stmt (void)
  3192. {
  3193. tree t = build_empty_stmt (input_location);
  3194. return t;
  3195. }
  3196. /* Promote all args of integral type before generating any code. */
  3197. static void
  3198. promote_arguments (void)
  3199. {
  3200. int i;
  3201. tree arg;
  3202. for (arg = DECL_ARGUMENTS (current_function_decl), i = 0;
  3203. arg != NULL_TREE; arg = DECL_CHAIN (arg), i++)
  3204. {
  3205. tree arg_type = TREE_TYPE (arg);
  3206. if (INTEGRAL_TYPE_P (arg_type)
  3207. && TYPE_PRECISION (arg_type) < 32)
  3208. {
  3209. tree copy = find_local_variable (i, integer_type_node, -1);
  3210. java_add_stmt (build2 (MODIFY_EXPR, integer_type_node,
  3211. copy,
  3212. fold_convert (integer_type_node, arg)));
  3213. }
  3214. if (TYPE_IS_WIDE (arg_type))
  3215. i++;
  3216. }
  3217. }
  3218. /* Create a local variable that points to the constant pool. */
  3219. static void
  3220. cache_cpool_data_ref (void)
  3221. {
  3222. if (optimize)
  3223. {
  3224. tree cpool;
  3225. tree d = build_constant_data_ref (flag_indirect_classes);
  3226. tree cpool_ptr = build_decl (input_location, VAR_DECL, NULL_TREE,
  3227. build_pointer_type (TREE_TYPE (d)));
  3228. java_add_local_var (cpool_ptr);
  3229. TREE_CONSTANT (cpool_ptr) = 1;
  3230. java_add_stmt (build2 (MODIFY_EXPR, TREE_TYPE (cpool_ptr),
  3231. cpool_ptr, build_address_of (d)));
  3232. cpool = build1 (INDIRECT_REF, TREE_TYPE (d), cpool_ptr);
  3233. TREE_THIS_NOTRAP (cpool) = 1;
  3234. TYPE_CPOOL_DATA_REF (output_class) = cpool;
  3235. }
  3236. }
  3237. #include "gt-java-expr.h"