hlcgcpu.pas 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580
  1. {
  2. Copyright (c) 1998-2010 by Florian Klaempfl and Jonas Maebe
  3. Member of the Free Pascal development team
  4. This unit implements the jvm high level code generator
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit hlcgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,
  23. aasmbase,aasmdata,
  24. symbase,symconst,symtype,symdef,symsym,
  25. node,
  26. cpubase, hlcgobj, cgbase, cgutils, parabase;
  27. type
  28. { thlcgjvm }
  29. thlcgjvm = class(thlcgobj)
  30. private
  31. fevalstackheight,
  32. fmaxevalstackheight: longint;
  33. public
  34. constructor create;
  35. procedure incstack(list : TAsmList;slots: longint);
  36. procedure decstack(list : TAsmList;slots: longint);
  37. class function def2regtyp(def: tdef): tregistertype; override;
  38. procedure a_load_const_cgpara(list : TAsmList;tosize : tdef;a : tcgint;const cgpara : TCGPara);override;
  39. function a_call_name(list : TAsmList;pd : tprocdef;const s : TSymStr; const paras: array of pcgpara; forceresdef: tdef; weak: boolean): tcgpara;override;
  40. function a_call_name_inherited(list : TAsmList;pd : tprocdef;const s : TSymStr; const paras: array of pcgpara): tcgpara;override;
  41. function a_call_reg(list: TAsmList; pd: tabstractprocdef; reg: tregister; const paras: array of pcgpara): tcgpara; override;
  42. procedure a_load_const_reg(list : TAsmList;tosize : tdef;a : tcgint;register : tregister);override;
  43. procedure a_load_const_ref(list : TAsmList;tosize : tdef;a : tcgint;const ref : treference);override;
  44. procedure a_load_reg_ref(list : TAsmList;fromsize, tosize : tdef;register : tregister;const ref : treference);override;
  45. procedure a_load_reg_reg(list : TAsmList;fromsize, tosize : tdef;reg1,reg2 : tregister);override;
  46. procedure a_load_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;register : tregister);override;
  47. procedure a_load_ref_ref(list : TAsmList;fromsize, tosize : tdef;const sref : treference;const dref : treference);override;
  48. procedure a_loadaddr_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;r : tregister);override;
  49. procedure a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; reg: TRegister); override;
  50. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister); override;
  51. procedure a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; const ref: TReference); override;
  52. procedure a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister); override;
  53. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister); override;
  54. procedure a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister); override;
  55. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation); override;
  56. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation); override;
  57. procedure a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference; l: tasmlabel); override;
  58. procedure a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel); override;
  59. procedure a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel); override;
  60. procedure a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel); override;
  61. procedure a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel); override;
  62. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  63. procedure g_concatcopy(list : TAsmList;size: tdef; const source,dest : treference);override;
  64. procedure g_copyshortstring(list : TAsmList;const source,dest : treference;strdef:tstringdef);override;
  65. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference); override;
  66. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister); override;
  67. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference); override;
  68. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister); override;
  69. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean); override;
  70. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean); override;
  71. procedure gen_load_return_value(list:TAsmList);override;
  72. procedure record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList); override;
  73. procedure g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);override;
  74. procedure g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation; const name: string); override;
  75. procedure g_initialize(list : TAsmList;t : tdef;const ref : treference);override;
  76. procedure g_finalize(list : TAsmList;t : tdef;const ref : treference);override;
  77. procedure g_overflowcheck(list: TAsmList; const Loc: tlocation; def: tdef); override;
  78. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;var ovloc : tlocation); override;
  79. procedure location_get_data_ref(list:TAsmList;def: tdef; const l:tlocation;var ref:treference;loadref:boolean; alignment: longint);override;
  80. procedure maybe_change_load_node_reg(list: TAsmList; var n: tnode; reload: boolean); override;
  81. procedure g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister); override;
  82. procedure g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation); override;
  83. procedure gen_initialize_code(list: TAsmList); override;
  84. procedure gen_entry_code(list: TAsmList); override;
  85. procedure gen_exit_code(list: TAsmList); override;
  86. { unimplemented/unnecessary routines }
  87. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tdef; src, dst: tregister); override;
  88. procedure a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle); override;
  89. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister; shuffle: pmmshuffle); override;
  90. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle); override;
  91. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle); override;
  92. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; src, dst: tregister; shuffle: pmmshuffle); override;
  93. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle); override;
  94. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle); override;
  95. procedure g_stackpointer_alloc(list: TAsmList; size: longint); override;
  96. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint); override;
  97. procedure g_adjust_self_value(list: TAsmList; procdef: tprocdef; ioffset: aint); override;
  98. procedure g_local_unwind(list: TAsmList; l: TAsmLabel); override;
  99. { JVM-specific routines }
  100. procedure a_load_stack_reg(list : TAsmList;size: tdef;reg: tregister);
  101. { extra_slots are the slots that are used by the reference, and that
  102. will be removed by the store operation }
  103. procedure a_load_stack_ref(list : TAsmList;size: tdef;const ref: treference;extra_slots: longint);
  104. procedure a_load_reg_stack(list : TAsmList;size: tdef;reg: tregister);
  105. { extra_slots are the slots that are used by the reference, and that
  106. will be removed by the load operation }
  107. procedure a_load_ref_stack(list : TAsmList;size: tdef;const ref: treference;extra_slots: longint);
  108. procedure a_load_const_stack(list : TAsmList;size: tdef;a :tcgint; typ: TRegisterType);
  109. procedure a_load_stack_loc(list : TAsmList;size: tdef;const loc: tlocation);
  110. procedure a_load_loc_stack(list : TAsmList;size: tdef;const loc: tlocation);
  111. procedure a_loadfpu_const_stack(list : TAsmList;size: tdef;a :double);
  112. procedure a_op_stack(list : TAsmList;op: topcg; size: tdef; trunc32: boolean);
  113. procedure a_op_const_stack(list : TAsmList;op: topcg; size: tdef;a : tcgint);
  114. procedure a_op_reg_stack(list : TAsmList;op: topcg; size: tdef;reg: tregister);
  115. procedure a_op_ref_stack(list : TAsmList;op: topcg; size: tdef;const ref: treference);
  116. procedure a_op_loc_stack(list : TAsmList;op: topcg; size: tdef;const loc: tlocation);
  117. procedure g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation); override;
  118. { assumes that initdim dimensions have already been pushed on the
  119. evaluation stack, and creates a new array of type arrdef with these
  120. dimensions }
  121. procedure g_newarray(list : TAsmList; arrdef: tdef; initdim: longint);
  122. { gets the length of the array whose reference is stored in arrloc,
  123. and puts it on the evaluation stack }
  124. procedure g_getarraylen(list : TAsmList; const arrloc: tlocation);
  125. { this routine expects that all values are already massaged into the
  126. required form (sign bits xor'ed for gt/lt comparisons for OS_32/OS_64,
  127. see http://stackoverflow.com/questions/4068973/c-performing-signed-comparison-in-unsigned-variables-without-casting ) }
  128. procedure a_cmp_stack_label(list : TAsmlist; size: tdef; cmp_op: topcmp; lab: tasmlabel);
  129. { these 2 routines perform the massaging expected by the previous one }
  130. procedure maybe_adjust_cmp_stackval(list : TAsmlist; size: tdef; cmp_op: topcmp);
  131. function maybe_adjust_cmp_constval(size: tdef; cmp_op: topcmp; a: tcgint): tcgint;
  132. { truncate/sign extend after performing operations on values < 32 bit
  133. that may have overflowed outside the range }
  134. procedure maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  135. { performs sign/zero extension as required }
  136. procedure resize_stack_int_val(list: TAsmList;fromsize,tosize: tdef; formemstore: boolean);
  137. { 8/16 bit unsigned parameters and return values must be sign-extended on
  138. the producer side, because the JVM does not support unsigned variants;
  139. then they have to be zero-extended again on the consumer side }
  140. procedure maybe_resize_stack_para_val(list: TAsmList; retdef: tdef; callside: boolean);
  141. { adjust the stack height after a call based on the specified number of
  142. slots used for parameters and the provided resultdef }
  143. procedure g_adjust_stack_after_call(list: TAsmList; pd: tabstractprocdef; paraheight: longint; forceresdef: tdef);
  144. property maxevalstackheight: longint read fmaxevalstackheight;
  145. procedure gen_initialize_fields_code(list:TAsmList);
  146. procedure gen_typecheck(list: TAsmList; checkop: tasmop; checkdef: tdef);
  147. protected
  148. procedure a_load_const_stack_intern(list : TAsmList;size : tdef;a : tcgint; typ: TRegisterType; legalize_const: boolean);
  149. function get_enum_init_val_ref(def: tdef; out ref: treference): boolean;
  150. procedure allocate_implicit_structs_for_st_with_base_ref(list: TAsmList; st: tsymtable; const ref: treference; allocvartyp: tsymtyp);
  151. procedure allocate_enum_with_base_ref(list: TAsmList; vs: tabstractvarsym; const initref: treference; destbaseref: treference);
  152. procedure allocate_implicit_struct_with_base_ref(list: TAsmList; vs: tabstractvarsym; ref: treference);
  153. procedure gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara); override;
  154. procedure g_copyvalueparas(p: TObject; arg: pointer); override;
  155. procedure inittempvariables(list:TAsmList);override;
  156. function g_call_system_proc_intern(list: TAsmList; pd: tprocdef; const paras: array of pcgpara; forceresdef: tdef): tcgpara; override;
  157. { in case of an array, the array base address and index have to be
  158. put on the evaluation stack before the stored value; similarly, for
  159. fields the self pointer has to be loaded first. Also checks whether
  160. the reference is valid. If dup is true, the necessary values are stored
  161. twice. Returns how many stack slots have been consumed, disregarding
  162. the "dup". }
  163. function prepare_stack_for_ref(list: TAsmList; const ref: treference; dup: boolean): longint;
  164. { return the load/store opcode to load/store from/to ref; if the result
  165. has to be and'ed after a load to get the final value, that constant
  166. is returned in finishandval (otherwise that value is set to -1) }
  167. function loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: tcgint): tasmop;
  168. { return the load/store opcode to load/store from/to reg; if the result
  169. has to be and'ed after a load to get the final value, that constant
  170. is returned in finishandval (otherwise that value is set to -1) }
  171. function loadstoreopc(def: tdef; isload, isarray: boolean; out finishandval: tcgint): tasmop;
  172. procedure resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  173. { in case of an OS_32 OP_DIV, we have to use an OS_S64 OP_IDIV because the
  174. JVM does not support unsigned divisions }
  175. procedure maybepreparedivu32(list: TAsmList; var op: topcg; size: tdef; out isdivu32: boolean);
  176. { common implementation of a_call_* }
  177. function a_call_name_intern(list : TAsmList;pd : tprocdef;const s : TSymStr; forceresdef: tdef; inheritedcall: boolean): tcgpara;
  178. { concatcopy helpers }
  179. procedure concatcopy_normal_array(list: TAsmList; size: tdef; const source, dest: treference);
  180. procedure concatcopy_record(list: TAsmList; size: tdef; const source, dest: treference);
  181. procedure concatcopy_set(list: TAsmList; size: tdef; const source, dest: treference);
  182. procedure concatcopy_shortstring(list: TAsmList; size: tdef; const source, dest: treference);
  183. end;
  184. procedure create_hlcodegen;
  185. const
  186. opcmp2if: array[topcmp] of tasmop = (A_None,
  187. a_ifeq,a_ifgt,a_iflt,a_ifge,a_ifle,
  188. a_ifne,a_ifle,a_iflt,a_ifge,a_ifgt);
  189. implementation
  190. uses
  191. verbose,cutils,globals,fmodule,constexp,
  192. defutil,
  193. aasmtai,aasmcpu,
  194. symtable,symcpu,jvmdef,
  195. procinfo,cpuinfo,cgcpu,tgobj;
  196. const
  197. TOpCG2IAsmOp : array[topcg] of TAsmOp=( { not = xor -1 }
  198. A_None,A_None,a_iadd,a_iand,A_none,a_idiv,a_imul,a_imul,a_ineg,A_None,a_ior,a_ishr,a_ishl,a_iushr,a_isub,a_ixor,A_None,A_None
  199. );
  200. TOpCG2LAsmOp : array[topcg] of TAsmOp=( { not = xor -1 }
  201. A_None,A_None,a_ladd,a_land,A_none,a_ldiv,a_lmul,a_lmul,a_lneg,A_None,a_lor,a_lshr,a_lshl,a_lushr,a_lsub,a_lxor,A_None,A_None
  202. );
  203. constructor thlcgjvm.create;
  204. begin
  205. fevalstackheight:=0;
  206. fmaxevalstackheight:=0;
  207. end;
  208. procedure thlcgjvm.incstack(list: TasmList;slots: longint);
  209. begin
  210. if slots=0 then
  211. exit;
  212. inc(fevalstackheight,slots);
  213. if (fevalstackheight>fmaxevalstackheight) then
  214. fmaxevalstackheight:=fevalstackheight;
  215. if cs_asm_regalloc in current_settings.globalswitches then
  216. list.concat(tai_comment.Create(strpnew(' allocated '+tostr(slots)+', stack height = '+tostr(fevalstackheight))));
  217. end;
  218. procedure thlcgjvm.decstack(list: TAsmList;slots: longint);
  219. begin
  220. if slots=0 then
  221. exit;
  222. dec(fevalstackheight,slots);
  223. if (fevalstackheight<0) and
  224. not(cs_no_regalloc in current_settings.globalswitches) then
  225. internalerror(2010120501);
  226. if cs_asm_regalloc in current_settings.globalswitches then
  227. list.concat(tai_comment.Create(strpnew(' freed '+tostr(slots)+', stack height = '+tostr(fevalstackheight))));
  228. end;
  229. class function thlcgjvm.def2regtyp(def: tdef): tregistertype;
  230. begin
  231. case def.typ of
  232. { records (including files) and enums are implemented via classes }
  233. recorddef,
  234. filedef,
  235. enumdef,
  236. setdef:
  237. result:=R_ADDRESSREGISTER;
  238. { shortstrings are implemented via classes }
  239. else if is_shortstring(def) or
  240. { voiddef can only be typecasted into (implicit) pointers }
  241. is_void(def) then
  242. result:=R_ADDRESSREGISTER
  243. else
  244. result:=inherited;
  245. end;
  246. end;
  247. procedure thlcgjvm.a_load_const_cgpara(list: TAsmList; tosize: tdef; a: tcgint; const cgpara: TCGPara);
  248. begin
  249. tosize:=get_para_push_size(tosize);
  250. if tosize=s8inttype then
  251. a:=shortint(a)
  252. else if tosize=s16inttype then
  253. a:=smallint(a);
  254. inherited a_load_const_cgpara(list, tosize, a, cgpara);
  255. end;
  256. function thlcgjvm.a_call_name(list: TAsmList; pd: tprocdef; const s: TSymStr; const paras: array of pcgpara; forceresdef: tdef; weak: boolean): tcgpara;
  257. begin
  258. result:=a_call_name_intern(list,pd,s,forceresdef,false);
  259. end;
  260. function thlcgjvm.a_call_name_inherited(list: TAsmList; pd: tprocdef; const s: TSymStr; const paras: array of pcgpara): tcgpara;
  261. begin
  262. result:=a_call_name_intern(list,pd,s,nil,true);
  263. end;
  264. function thlcgjvm.a_call_reg(list: TAsmList; pd: tabstractprocdef; reg: tregister; const paras: array of pcgpara): tcgpara;
  265. begin
  266. internalerror(2012042824);
  267. result.init;
  268. end;
  269. procedure thlcgjvm.a_load_const_stack_intern(list : TAsmList;size : tdef;a : tcgint; typ: TRegisterType; legalize_const: boolean);
  270. begin
  271. if legalize_const and
  272. (typ=R_INTREGISTER) and
  273. (size.typ=orddef) then
  274. begin
  275. { uses specific byte/short array store instructions, and the Dalvik
  276. VM does not like it if we store values outside the range }
  277. case torddef(size).ordtype of
  278. u8bit:
  279. a:=shortint(a);
  280. u16bit:
  281. a:=smallint(a);
  282. else
  283. ;
  284. end;
  285. end;
  286. a_load_const_stack(list,size,a,typ);
  287. end;
  288. procedure thlcgjvm.a_load_const_stack(list : TAsmList;size : tdef;a : tcgint; typ: TRegisterType);
  289. const
  290. int2opc: array[-1..5] of tasmop = (a_iconst_m1,a_iconst_0,a_iconst_1,
  291. a_iconst_2,a_iconst_3,a_iconst_4,a_iconst_5);
  292. begin
  293. case typ of
  294. R_INTREGISTER:
  295. begin
  296. case def_cgsize(size) of
  297. OS_8,OS_16,OS_32,
  298. OS_S8,OS_S16,OS_S32:
  299. begin
  300. { convert cardinals to longints }
  301. a:=longint(a);
  302. if (a>=-1) and
  303. (a<=5) then
  304. list.concat(taicpu.op_none(int2opc[a]))
  305. else if (a>=low(shortint)) and
  306. (a<=high(shortint)) then
  307. list.concat(taicpu.op_const(a_bipush,a))
  308. else if (a>=low(smallint)) and
  309. (a<=high(smallint)) then
  310. list.concat(taicpu.op_const(a_sipush,a))
  311. else
  312. list.concat(taicpu.op_const(a_ldc,a));
  313. { for android verifier }
  314. if (size.typ=orddef) and
  315. (torddef(size).ordtype=uwidechar) then
  316. list.concat(taicpu.op_none(a_i2c));
  317. end;
  318. OS_64,OS_S64:
  319. begin
  320. case a of
  321. 0:
  322. list.concat(taicpu.op_none(a_lconst_0));
  323. 1:
  324. list.concat(taicpu.op_none(a_lconst_1));
  325. else
  326. list.concat(taicpu.op_const(a_ldc2_w,a));
  327. end;
  328. incstack(list,1);
  329. end;
  330. else
  331. internalerror(2010110702);
  332. end;
  333. end;
  334. R_ADDRESSREGISTER:
  335. begin
  336. if a<>0 then
  337. internalerror(2010110701);
  338. list.concat(taicpu.op_none(a_aconst_null));
  339. end;
  340. else
  341. internalerror(2010110703);
  342. end;
  343. incstack(list,1);
  344. end;
  345. procedure thlcgjvm.a_load_stack_loc(list: TAsmList; size: tdef; const loc: tlocation);
  346. begin
  347. case loc.loc of
  348. LOC_REGISTER,LOC_CREGISTER,
  349. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  350. a_load_stack_reg(list,size,loc.register);
  351. LOC_REFERENCE:
  352. a_load_stack_ref(list,size,loc.reference,prepare_stack_for_ref(list,loc.reference,false));
  353. else
  354. internalerror(2011020501);
  355. end;
  356. end;
  357. procedure thlcgjvm.a_load_loc_stack(list: TAsmList;size: tdef;const loc: tlocation);
  358. begin
  359. case loc.loc of
  360. LOC_REGISTER,LOC_CREGISTER,
  361. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  362. a_load_reg_stack(list,size,loc.register);
  363. LOC_REFERENCE,LOC_CREFERENCE:
  364. a_load_ref_stack(list,size,loc.reference,prepare_stack_for_ref(list,loc.reference,false));
  365. LOC_CONSTANT:
  366. a_load_const_stack(list,size,loc.value,def2regtyp(size));
  367. else
  368. internalerror(2011010401);
  369. end;
  370. end;
  371. procedure thlcgjvm.a_loadfpu_const_stack(list: TAsmList; size: tdef; a: double);
  372. begin
  373. case tfloatdef(size).floattype of
  374. s32real:
  375. begin
  376. if a=0.0 then
  377. list.concat(taicpu.op_none(a_fconst_0))
  378. else if a=1.0 then
  379. list.concat(taicpu.op_none(a_fconst_1))
  380. else if a=2.0 then
  381. list.concat(taicpu.op_none(a_fconst_2))
  382. else
  383. list.concat(taicpu.op_single(a_ldc,a));
  384. incstack(list,1);
  385. end;
  386. s64real:
  387. begin
  388. if a=0.0 then
  389. list.concat(taicpu.op_none(a_dconst_0))
  390. else if a=1.0 then
  391. list.concat(taicpu.op_none(a_dconst_1))
  392. else
  393. list.concat(taicpu.op_double(a_ldc2_w,a));
  394. incstack(list,2);
  395. end
  396. else
  397. internalerror(2011010501);
  398. end;
  399. end;
  400. procedure thlcgjvm.a_op_stack(list: TAsmList; op: topcg; size: tdef; trunc32: boolean);
  401. var
  402. cgsize: tcgsize;
  403. begin
  404. if not trunc32 then
  405. cgsize:=def_cgsize(size)
  406. else
  407. begin
  408. resize_stack_int_val(list,u32inttype,s64inttype,false);
  409. cgsize:=OS_S64;
  410. end;
  411. case cgsize of
  412. OS_8,OS_S8,
  413. OS_16,OS_S16,
  414. OS_32,OS_S32:
  415. begin
  416. { not = xor 1 for boolean, xor -1 for the rest}
  417. if op=OP_NOT then
  418. begin
  419. if not is_pasbool(size) then
  420. a_load_const_stack(list,s32inttype,high(cardinal),R_INTREGISTER)
  421. else
  422. a_load_const_stack(list,size,1,R_INTREGISTER);
  423. op:=OP_XOR;
  424. end;
  425. if TOpCG2IAsmOp[op]=A_None then
  426. internalerror(2010120532);
  427. list.concat(taicpu.op_none(TOpCG2IAsmOp[op]));
  428. maybe_adjust_op_result(list,op,size);
  429. if op<>OP_NEG then
  430. decstack(list,1);
  431. end;
  432. OS_64,OS_S64:
  433. begin
  434. { unsigned 64 bit division must be done via a helper }
  435. if op=OP_DIV then
  436. internalerror(2010120530);
  437. { not = xor 1 for boolean, xor -1 for the rest}
  438. if op=OP_NOT then
  439. begin
  440. if not is_pasbool(size) then
  441. a_load_const_stack(list,s64inttype,-1,R_INTREGISTER)
  442. else
  443. a_load_const_stack(list,s64inttype,1,R_INTREGISTER);
  444. op:=OP_XOR;
  445. end;
  446. if TOpCG2LAsmOp[op]=A_None then
  447. internalerror(2010120533);
  448. list.concat(taicpu.op_none(TOpCG2LAsmOp[op]));
  449. case op of
  450. OP_NOT,
  451. OP_NEG:
  452. ;
  453. { the second argument here is an int rather than a long }
  454. OP_SHL,OP_SHR,OP_SAR:
  455. decstack(list,1);
  456. else
  457. decstack(list,2);
  458. end;
  459. end;
  460. else
  461. internalerror(2010120531);
  462. end;
  463. if trunc32 then
  464. begin
  465. list.concat(taicpu.op_none(a_l2i));
  466. decstack(list,1);
  467. end;
  468. end;
  469. procedure thlcgjvm.a_op_const_stack(list: TAsmList;op: topcg;size: tdef;a: tcgint);
  470. var
  471. trunc32: boolean;
  472. begin
  473. maybepreparedivu32(list,op,size,trunc32);
  474. case op of
  475. OP_NEG,OP_NOT:
  476. internalerror(2011010801);
  477. OP_SHL,OP_SHR,OP_SAR:
  478. { the second argument here is an int rather than a long }
  479. a_load_const_stack(list,s32inttype,a,R_INTREGISTER);
  480. else
  481. a_load_const_stack(list,size,a,R_INTREGISTER);
  482. end;
  483. a_op_stack(list,op,size,trunc32);
  484. end;
  485. procedure thlcgjvm.a_op_reg_stack(list: TAsmList; op: topcg; size: tdef; reg: tregister);
  486. var
  487. trunc32: boolean;
  488. begin
  489. maybepreparedivu32(list,op,size,trunc32);
  490. case op of
  491. OP_SHL,OP_SHR,OP_SAR:
  492. if not is_64bitint(size) then
  493. a_load_reg_stack(list,size,reg)
  494. else
  495. begin
  496. { the second argument here is an int rather than a long }
  497. if getsubreg(reg)=R_SUBQ then
  498. internalerror(2011010802);
  499. a_load_reg_stack(list,s32inttype,reg)
  500. end
  501. else
  502. a_load_reg_stack(list,size,reg);
  503. end;
  504. a_op_stack(list,op,size,trunc32);
  505. end;
  506. procedure thlcgjvm.a_op_ref_stack(list: TAsmList; op: topcg; size: tdef; const ref: treference);
  507. var
  508. trunc32: boolean;
  509. begin
  510. { ref must not be the stack top, because that may indicate an error
  511. (it means that we will perform an operation of the stack top onto
  512. itself, so that means the two values have been loaded manually prior
  513. to calling this routine, instead of letting this routine load one of
  514. them; if something like that is needed, call a_op_stack() directly) }
  515. if ref.base=NR_EVAL_STACK_BASE then
  516. internalerror(2010121102);
  517. maybepreparedivu32(list,op,size,trunc32);
  518. case op of
  519. OP_SHL,OP_SHR,OP_SAR:
  520. begin
  521. if not is_64bitint(size) then
  522. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false))
  523. else
  524. a_load_ref_stack(list,s32inttype,ref,prepare_stack_for_ref(list,ref,false));
  525. end;
  526. else
  527. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  528. end;
  529. a_op_stack(list,op,size,trunc32);
  530. end;
  531. procedure thlcgjvm.a_op_loc_stack(list: TAsmList; op: topcg; size: tdef; const loc: tlocation);
  532. begin
  533. case loc.loc of
  534. LOC_REGISTER,LOC_CREGISTER:
  535. a_op_reg_stack(list,op,size,loc.register);
  536. LOC_REFERENCE,LOC_CREFERENCE:
  537. a_op_ref_stack(list,op,size,loc.reference);
  538. LOC_CONSTANT:
  539. a_op_const_stack(list,op,size,loc.value);
  540. else
  541. internalerror(2011011415)
  542. end;
  543. end;
  544. procedure thlcgjvm.g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation);
  545. begin
  546. case fromloc.loc of
  547. LOC_CREFERENCE,
  548. LOC_REFERENCE:
  549. begin
  550. toloc:=fromloc;
  551. if (fromloc.reference.base<>NR_NO) and
  552. (fromloc.reference.base<>current_procinfo.framepointer) and
  553. (fromloc.reference.base<>NR_STACK_POINTER_REG) then
  554. g_allocload_reg_reg(list,voidpointertype,fromloc.reference.base,toloc.reference.base,R_ADDRESSREGISTER);
  555. case fromloc.reference.arrayreftype of
  556. art_indexreg:
  557. begin
  558. { all array indices in Java are 32 bit ints }
  559. g_allocload_reg_reg(list,s32inttype,fromloc.reference.index,toloc.reference.index,R_INTREGISTER);
  560. end;
  561. art_indexref:
  562. begin
  563. { base register of the address of the index -> pointer }
  564. if (fromloc.reference.indexbase<>NR_NO) and
  565. (fromloc.reference.indexbase<>NR_STACK_POINTER_REG) then
  566. g_allocload_reg_reg(list,voidpointertype,fromloc.reference.indexbase,toloc.reference.indexbase,R_ADDRESSREGISTER);
  567. end;
  568. else
  569. ;
  570. end;
  571. end;
  572. else
  573. inherited;
  574. end;
  575. end;
  576. procedure thlcgjvm.g_newarray(list: TAsmList; arrdef: tdef; initdim: longint);
  577. var
  578. recref,
  579. enuminitref: treference;
  580. elemdef: tdef;
  581. i: longint;
  582. mangledname: string;
  583. opc: tasmop;
  584. primitivetype: boolean;
  585. begin
  586. elemdef:=arrdef;
  587. if initdim>1 then
  588. begin
  589. { multianewarray typedesc ndim }
  590. list.concat(taicpu.op_sym_const(a_multianewarray,
  591. current_asmdata.RefAsmSymbol(jvmarrtype(elemdef,primitivetype),AT_METADATA),initdim));
  592. { has to be a multi-dimensional array type }
  593. if primitivetype then
  594. internalerror(2011012207);
  595. end
  596. else
  597. begin
  598. { for primitive types:
  599. newarray typedesc
  600. for reference types:
  601. anewarray typedesc
  602. }
  603. { get the type of the elements of the array we are creating }
  604. elemdef:=tarraydef(arrdef).elementdef;
  605. mangledname:=jvmarrtype(elemdef,primitivetype);
  606. if primitivetype then
  607. opc:=a_newarray
  608. else
  609. opc:=a_anewarray;
  610. list.concat(taicpu.op_sym(opc,current_asmdata.RefAsmSymbol(mangledname,AT_METADATA)));
  611. end;
  612. { all dimensions are removed from the stack, an array reference is
  613. added }
  614. decstack(list,initdim-1);
  615. { in case of an array of records, sets or shortstrings, initialise }
  616. elemdef:=tarraydef(arrdef).elementdef;
  617. for i:=1 to pred(initdim) do
  618. elemdef:=tarraydef(elemdef).elementdef;
  619. if (elemdef.typ in [recorddef,setdef]) or
  620. ((elemdef.typ=enumdef) and
  621. get_enum_init_val_ref(elemdef,enuminitref)) or
  622. is_shortstring(elemdef) or
  623. ((elemdef.typ=procvardef) and
  624. not tprocvardef(elemdef).is_addressonly) or
  625. is_ansistring(elemdef) or
  626. is_wide_or_unicode_string(elemdef) or
  627. is_dynamic_array(elemdef) then
  628. begin
  629. { duplicate array instance }
  630. list.concat(taicpu.op_none(a_dup));
  631. incstack(list,1);
  632. a_load_const_stack(list,s32inttype,initdim-1,R_INTREGISTER);
  633. case elemdef.typ of
  634. arraydef:
  635. g_call_system_proc(list,'fpc_initialize_array_dynarr',[],nil);
  636. recorddef,setdef,procvardef:
  637. begin
  638. tg.gethltemp(list,elemdef,elemdef.size,tt_persistent,recref);
  639. a_load_ref_stack(list,elemdef,recref,prepare_stack_for_ref(list,recref,false));
  640. case elemdef.typ of
  641. recorddef:
  642. g_call_system_proc(list,'fpc_initialize_array_record',[],nil);
  643. setdef:
  644. begin
  645. if tsetdef(elemdef).elementdef.typ=enumdef then
  646. g_call_system_proc(list,'fpc_initialize_array_enumset',[],nil)
  647. else
  648. g_call_system_proc(list,'fpc_initialize_array_bitset',[],nil)
  649. end;
  650. procvardef:
  651. g_call_system_proc(list,'fpc_initialize_array_procvar',[],nil);
  652. else
  653. internalerror(2019051025);
  654. end;
  655. tg.ungettemp(list,recref);
  656. end;
  657. enumdef:
  658. begin
  659. a_load_ref_stack(list,java_jlobject,enuminitref,prepare_stack_for_ref(list,enuminitref,false));
  660. g_call_system_proc(list,'fpc_initialize_array_object',[],nil);
  661. end;
  662. stringdef:
  663. begin
  664. case tstringdef(elemdef).stringtype of
  665. st_shortstring:
  666. begin
  667. a_load_const_stack_intern(list,u8inttype,tstringdef(elemdef).len,R_INTREGISTER,true);
  668. g_call_system_proc(list,'fpc_initialize_array_shortstring',[],nil);
  669. end;
  670. st_ansistring:
  671. g_call_system_proc(list,'fpc_initialize_array_ansistring',[],nil);
  672. st_unicodestring,
  673. st_widestring:
  674. g_call_system_proc(list,'fpc_initialize_array_unicodestring',[],nil);
  675. else
  676. internalerror(2011081801);
  677. end;
  678. end;
  679. else
  680. internalerror(2011081801);
  681. end;
  682. end;
  683. end;
  684. procedure thlcgjvm.g_getarraylen(list: TAsmList; const arrloc: tlocation);
  685. var
  686. nillab,endlab: tasmlabel;
  687. begin
  688. { inline because we have to use the arraylength opcode, which
  689. cannot be represented directly in Pascal. Even though the JVM
  690. supports allocated arrays with length=0, we still also have to
  691. check for nil pointers because even if FPC always generates
  692. allocated empty arrays under all circumstances, external Java
  693. code could pass in nil pointers.
  694. Note that this means that assigned(arr) can be different from
  695. length(arr)<>0 for dynamic arrays when targeting the JVM.
  696. }
  697. current_asmdata.getjumplabel(nillab);
  698. current_asmdata.getjumplabel(endlab);
  699. { if assigned(arr) ... }
  700. a_load_loc_stack(list,java_jlobject,arrloc);
  701. list.concat(taicpu.op_none(a_dup));
  702. incstack(list,1);
  703. list.concat(taicpu.op_sym(a_ifnull,nillab));
  704. decstack(list,1);
  705. { ... then result:=arraylength(arr) ... }
  706. list.concat(taicpu.op_none(a_arraylength));
  707. a_jmp_always(list,endlab);
  708. { ... else result:=0 }
  709. a_label(list,nillab);
  710. list.concat(taicpu.op_none(a_pop));
  711. decstack(list,1);
  712. list.concat(taicpu.op_none(a_iconst_0));
  713. incstack(list,1);
  714. a_label(list,endlab);
  715. end;
  716. procedure thlcgjvm.a_cmp_stack_label(list: TAsmlist; size: tdef; cmp_op: topcmp; lab: tasmlabel);
  717. const
  718. opcmp2icmp: array[topcmp] of tasmop = (A_None,
  719. a_if_icmpeq,a_if_icmpgt,a_if_icmplt,a_if_icmpge,a_if_icmple,
  720. a_if_icmpne,a_if_icmple,a_if_icmplt,a_if_icmpge,a_if_icmpgt);
  721. var
  722. cgsize: tcgsize;
  723. begin
  724. case def2regtyp(size) of
  725. R_INTREGISTER:
  726. begin
  727. cgsize:=def_cgsize(size);
  728. case cgsize of
  729. OS_S8,OS_8,
  730. OS_16,OS_S16,
  731. OS_S32,OS_32:
  732. begin
  733. list.concat(taicpu.op_sym(opcmp2icmp[cmp_op],lab));
  734. decstack(list,2);
  735. end;
  736. OS_64,OS_S64:
  737. begin
  738. list.concat(taicpu.op_none(a_lcmp));
  739. decstack(list,3);
  740. list.concat(taicpu.op_sym(opcmp2if[cmp_op],lab));
  741. decstack(list,1);
  742. end;
  743. else
  744. internalerror(2010120538);
  745. end;
  746. end;
  747. R_ADDRESSREGISTER:
  748. begin
  749. case cmp_op of
  750. OC_EQ:
  751. list.concat(taicpu.op_sym(a_if_acmpeq,lab));
  752. OC_NE:
  753. list.concat(taicpu.op_sym(a_if_acmpne,lab));
  754. else
  755. internalerror(2010120537);
  756. end;
  757. decstack(list,2);
  758. end;
  759. else
  760. internalerror(2010120538);
  761. end;
  762. end;
  763. procedure thlcgjvm.maybe_adjust_cmp_stackval(list: TAsmlist; size: tdef; cmp_op: topcmp);
  764. begin
  765. { use cmp_op because eventually that's what indicates the
  766. signed/unsigned character of the operation, not the size... }
  767. if (cmp_op in [OC_EQ,OC_NE,OC_LT,OC_LTE,OC_GT,OC_GTE]) or
  768. (def2regtyp(size)<>R_INTREGISTER) then
  769. exit;
  770. { http://stackoverflow.com/questions/4068973/c-performing-signed-comparison-in-unsigned-variables-without-casting }
  771. case def_cgsize(size) of
  772. OS_32,OS_S32:
  773. a_op_const_stack(list,OP_XOR,size,cardinal($80000000));
  774. OS_64,OS_S64:
  775. a_op_const_stack(list,OP_XOR,size,tcgint($8000000000000000));
  776. else
  777. ;
  778. end;
  779. end;
  780. function thlcgjvm.maybe_adjust_cmp_constval(size: tdef; cmp_op: topcmp; a: tcgint): tcgint;
  781. begin
  782. result:=a;
  783. { use cmp_op because eventually that's what indicates the
  784. signed/unsigned character of the operation, not the size... }
  785. if (cmp_op in [OC_EQ,OC_NE,OC_LT,OC_LTE,OC_GT,OC_GTE]) or
  786. (def2regtyp(size)<>R_INTREGISTER) then
  787. exit;
  788. case def_cgsize(size) of
  789. OS_32,OS_S32:
  790. result:=a xor cardinal($80000000);
  791. OS_64,OS_S64:
  792. {$push}{$r-}
  793. result:=a xor tcgint($8000000000000000);
  794. {$pop}
  795. else
  796. ;
  797. end;
  798. end;
  799. procedure thlcgjvm.maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  800. const
  801. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  802. begin
  803. if ((op in overflowops) or
  804. (current_settings.cputype=cpu_dalvik)) and
  805. (def_cgsize(size) in [OS_8,OS_S8,OS_16,OS_S16]) then
  806. resize_stack_int_val(list,s32inttype,size,false);
  807. end;
  808. procedure thlcgjvm.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  809. begin
  810. { constructors don't return anything in Java }
  811. if pd.proctypeoption=potype_constructor then
  812. exit;
  813. { must return a value of the correct type on the evaluation stack }
  814. case def2regtyp(resdef) of
  815. R_INTREGISTER,
  816. R_ADDRESSREGISTER:
  817. a_load_const_cgpara(list,resdef,0,resloc);
  818. R_FPUREGISTER:
  819. case tfloatdef(resdef).floattype of
  820. s32real:
  821. begin
  822. list.concat(taicpu.op_none(a_fconst_0));
  823. incstack(list,1);
  824. end;
  825. s64real:
  826. begin
  827. list.concat(taicpu.op_none(a_dconst_0));
  828. incstack(list,2);
  829. end;
  830. else
  831. internalerror(2011010302);
  832. end
  833. else
  834. internalerror(2011010301);
  835. end;
  836. end;
  837. procedure thlcgjvm.g_copyvalueparas(p: TObject; arg: pointer);
  838. var
  839. list: tasmlist;
  840. tmpref: treference;
  841. begin
  842. { zero-extend < 32 bit primitive types (FPC can zero-extend when calling,
  843. but that doesn't help when we're called from Java code or indirectly
  844. as a procvar -- exceptions: widechar (Java-specific type) and ordinal
  845. types whose upper bound does not set the sign bit }
  846. if (tsym(p).typ=paravarsym) and
  847. (tparavarsym(p).varspez in [vs_value,vs_const]) and
  848. (tparavarsym(p).vardef.typ=orddef) and
  849. not is_pasbool(tparavarsym(p).vardef) and
  850. not is_widechar(tparavarsym(p).vardef) and
  851. (tparavarsym(p).vardef.size<4) and
  852. not is_signed(tparavarsym(p).vardef) and
  853. (torddef(tparavarsym(p).vardef).high>=(1 shl (tparavarsym(p).vardef.size*8-1))) then
  854. begin
  855. list:=TAsmList(arg);
  856. { store value in new location to keep Android verifier happy }
  857. tg.gethltemp(list,tparavarsym(p).vardef,tparavarsym(p).vardef.size,tt_persistent,tmpref);
  858. a_load_loc_stack(list,tparavarsym(p).vardef,tparavarsym(p).initialloc);
  859. a_op_const_stack(list,OP_AND,tparavarsym(p).vardef,(1 shl (tparavarsym(p).vardef.size*8))-1);
  860. a_load_stack_ref(list,tparavarsym(p).vardef,tmpref,prepare_stack_for_ref(list,tmpref,false));
  861. location_reset_ref(tparavarsym(p).localloc,LOC_REFERENCE,def_cgsize(tparavarsym(p).vardef),4,tmpref.volatility);
  862. tparavarsym(p).localloc.reference:=tmpref;
  863. end;
  864. inherited g_copyvalueparas(p, arg);
  865. end;
  866. procedure thlcgjvm.inittempvariables(list: TAsmList);
  867. begin
  868. { these are automatically initialised when allocated if necessary }
  869. end;
  870. function thlcgjvm.g_call_system_proc_intern(list: TAsmList; pd: tprocdef; const paras: array of pcgpara; forceresdef: tdef): tcgpara;
  871. begin
  872. result:=inherited;
  873. pd.init_paraloc_info(callerside);
  874. g_adjust_stack_after_call(list,pd,pd.callerargareasize,forceresdef);
  875. end;
  876. function thlcgjvm.prepare_stack_for_ref(list: TAsmList; const ref: treference; dup: boolean): longint;
  877. var
  878. href: treference;
  879. begin
  880. result:=0;
  881. { fake location that indicates the value is already on the stack? }
  882. if (ref.base=NR_EVAL_STACK_BASE) then
  883. exit;
  884. if ref.arrayreftype=art_none then
  885. begin
  886. { non-array accesses cannot have an index reg }
  887. if ref.index<>NR_NO then
  888. internalerror(2010120509);
  889. if (ref.base<>NR_NO) then
  890. begin
  891. if (ref.base<>NR_STACK_POINTER_REG) then
  892. begin
  893. { regular field -> load self on the stack }
  894. a_load_reg_stack(list,voidpointertype,ref.base);
  895. if dup then
  896. begin
  897. list.concat(taicpu.op_none(a_dup));
  898. incstack(list,1);
  899. end;
  900. { field name/type encoded in symbol, no index/offset }
  901. if not assigned(ref.symbol) or
  902. (ref.offset<>0) then
  903. internalerror(2010120524);
  904. result:=1;
  905. end
  906. else
  907. begin
  908. { local variable -> offset encoded in opcode and nothing to
  909. do here, except for checking that it's a valid reference }
  910. if assigned(ref.symbol) then
  911. internalerror(2010120523);
  912. end;
  913. end
  914. else
  915. begin
  916. { static field -> nothing to do here, except for validity check }
  917. if not assigned(ref.symbol) or
  918. (ref.offset<>0) then
  919. internalerror(2010120525);
  920. end;
  921. end
  922. else
  923. begin
  924. { arrays have implicit dereference -> pointer to array must have been
  925. loaded into base reg }
  926. if (ref.base=NR_NO) or
  927. (ref.base=NR_STACK_POINTER_REG) then
  928. internalerror(2010120511);
  929. if assigned(ref.symbol) then
  930. internalerror(2010120512);
  931. { stack: ... -> ..., arrayref, index }
  932. { load array base address }
  933. a_load_reg_stack(list,voidpointertype,ref.base);
  934. { index can either be in a register, or located in a simple memory
  935. location (since we have to load it anyway) }
  936. case ref.arrayreftype of
  937. art_indexreg:
  938. begin
  939. if ref.index=NR_NO then
  940. internalerror(2010120513);
  941. { all array indices in Java are 32 bit ints }
  942. a_load_reg_stack(list,s32inttype,ref.index);
  943. end;
  944. art_indexref:
  945. begin
  946. cgutils.reference_reset_base(href,ref.indexbase,ref.indexoffset,ref.temppos,4,ref.volatility);
  947. href.symbol:=ref.indexsymbol;
  948. a_load_ref_stack(list,s32inttype,href,prepare_stack_for_ref(list,href,false));
  949. end;
  950. art_indexconst:
  951. begin
  952. a_load_const_stack(list,s32inttype,ref.indexoffset,R_INTREGISTER);
  953. end;
  954. else
  955. internalerror(2011012001);
  956. end;
  957. { adjustment of the index }
  958. if ref.offset<>0 then
  959. a_op_const_stack(list,OP_ADD,s32inttype,ref.offset);
  960. if dup then
  961. begin
  962. list.concat(taicpu.op_none(a_dup2));
  963. incstack(list,2);
  964. end;
  965. result:=2;
  966. end;
  967. end;
  968. procedure thlcgjvm.a_load_const_reg(list: TAsmList; tosize: tdef; a: tcgint; register: tregister);
  969. begin
  970. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  971. a_load_stack_reg(list,tosize,register);
  972. end;
  973. procedure thlcgjvm.a_load_const_ref(list: TAsmList; tosize: tdef; a: tcgint; const ref: treference);
  974. var
  975. extra_slots: longint;
  976. begin
  977. extra_slots:=prepare_stack_for_ref(list,ref,false);
  978. a_load_const_stack_intern(list,tosize,a,def2regtyp(tosize),(ref.arrayreftype<>art_none) or assigned(ref.symbol));
  979. a_load_stack_ref(list,tosize,ref,extra_slots);
  980. end;
  981. procedure thlcgjvm.a_load_reg_ref(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  982. var
  983. extra_slots: longint;
  984. begin
  985. extra_slots:=prepare_stack_for_ref(list,ref,false);
  986. a_load_reg_stack(list,fromsize,register);
  987. if def2regtyp(fromsize)=R_INTREGISTER then
  988. resize_stack_int_val(list,fromsize,tosize,(ref.arrayreftype<>art_none) or assigned(ref.symbol));
  989. a_load_stack_ref(list,tosize,ref,extra_slots);
  990. end;
  991. procedure thlcgjvm.a_load_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  992. begin
  993. a_load_reg_stack(list,fromsize,reg1);
  994. if def2regtyp(fromsize)=R_INTREGISTER then
  995. resize_stack_int_val(list,fromsize,tosize,false);
  996. a_load_stack_reg(list,tosize,reg2);
  997. end;
  998. procedure thlcgjvm.a_load_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  999. var
  1000. extra_slots: longint;
  1001. begin
  1002. extra_slots:=prepare_stack_for_ref(list,ref,false);
  1003. a_load_ref_stack(list,fromsize,ref,extra_slots);
  1004. if def2regtyp(fromsize)=R_INTREGISTER then
  1005. resize_stack_int_val(list,fromsize,tosize,false);
  1006. a_load_stack_reg(list,tosize,register);
  1007. end;
  1008. procedure thlcgjvm.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  1009. var
  1010. extra_sslots,
  1011. extra_dslots: longint;
  1012. begin
  1013. { make sure the destination reference is on top, since in the end the
  1014. order has to be "destref, value" -> first create "destref, sourceref" }
  1015. extra_dslots:=prepare_stack_for_ref(list,dref,false);
  1016. extra_sslots:=prepare_stack_for_ref(list,sref,false);
  1017. a_load_ref_stack(list,fromsize,sref,extra_sslots);
  1018. if def2regtyp(fromsize)=R_INTREGISTER then
  1019. resize_stack_int_val(list,fromsize,tosize,(dref.arrayreftype<>art_none) or assigned(dref.symbol));
  1020. a_load_stack_ref(list,tosize,dref,extra_dslots);
  1021. end;
  1022. procedure thlcgjvm.a_loadaddr_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; r: tregister);
  1023. begin
  1024. { only allowed for types that are not implicit pointers in Pascal (in
  1025. that case, ref contains a pointer to the actual data and we simply
  1026. return that pointer) }
  1027. if not jvmimplicitpointertype(fromsize) then
  1028. internalerror(2010120534);
  1029. a_load_ref_reg(list,java_jlobject,java_jlobject,ref,r);
  1030. end;
  1031. procedure thlcgjvm.a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; reg: TRegister);
  1032. begin
  1033. a_op_const_reg_reg(list,op,size,a,reg,reg);
  1034. end;
  1035. procedure thlcgjvm.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister);
  1036. begin
  1037. a_load_reg_stack(list,size,src);
  1038. a_op_const_stack(list,op,size,a);
  1039. a_load_stack_reg(list,size,dst);
  1040. end;
  1041. procedure thlcgjvm.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: tcgint; const ref: TReference);
  1042. var
  1043. extra_slots: longint;
  1044. begin
  1045. extra_slots:=prepare_stack_for_ref(list,ref,true);
  1046. { TODO, here or in peepholeopt: use iinc when possible }
  1047. a_load_ref_stack(list,size,ref,extra_slots);
  1048. a_op_const_stack(list,op,size,a);
  1049. { for android verifier }
  1050. if (def2regtyp(size)=R_INTREGISTER) and
  1051. ((ref.arrayreftype<>art_none) or
  1052. assigned(ref.symbol)) then
  1053. resize_stack_int_val(list,size,size,true);
  1054. a_load_stack_ref(list,size,ref,extra_slots);
  1055. end;
  1056. procedure thlcgjvm.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  1057. begin
  1058. if not(op in [OP_NOT,OP_NEG]) then
  1059. a_load_reg_stack(list,size,reg);
  1060. a_op_ref_stack(list,op,size,ref);
  1061. a_load_stack_reg(list,size,reg);
  1062. end;
  1063. procedure thlcgjvm.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  1064. begin
  1065. if not(op in [OP_NOT,OP_NEG]) then
  1066. a_load_reg_stack(list,size,src2);
  1067. a_op_reg_stack(list,op,size,src1);
  1068. a_load_stack_reg(list,size,dst);
  1069. end;
  1070. procedure thlcgjvm.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister);
  1071. begin
  1072. a_op_reg_reg_reg(list,op,size,reg1,reg2,reg2);
  1073. end;
  1074. procedure thlcgjvm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; a: tcgint; src, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1075. var
  1076. tmpreg: tregister;
  1077. begin
  1078. if not setflags then
  1079. begin
  1080. inherited;
  1081. exit;
  1082. end;
  1083. tmpreg:=getintregister(list,size);
  1084. a_load_const_reg(list,size,a,tmpreg);
  1085. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,true,ovloc);
  1086. end;
  1087. procedure thlcgjvm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister; setflags: boolean; var ovloc: tlocation);
  1088. var
  1089. orgsrc1, orgsrc2: tregister;
  1090. docheck: boolean;
  1091. lab: tasmlabel;
  1092. begin
  1093. if not setflags then
  1094. begin
  1095. inherited;
  1096. exit;
  1097. end;
  1098. { anything else cannot overflow }
  1099. docheck:=size.size in [4,8];
  1100. if docheck then
  1101. begin
  1102. orgsrc1:=src1;
  1103. orgsrc2:=src2;
  1104. if src1=dst then
  1105. begin
  1106. orgsrc1:=getintregister(list,size);
  1107. a_load_reg_reg(list,size,size,src1,orgsrc1);
  1108. end;
  1109. if src2=dst then
  1110. begin
  1111. orgsrc2:=getintregister(list,size);
  1112. a_load_reg_reg(list,size,size,src2,orgsrc2);
  1113. end;
  1114. end;
  1115. a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1116. if docheck then
  1117. begin
  1118. { * signed overflow for addition iff
  1119. - src1 and src2 are negative and result is positive (excep in case of
  1120. subtraction, then sign of src1 has to be inverted)
  1121. - src1 and src2 are positive and result is negative
  1122. -> Simplified boolean equivalent (in terms of sign bits):
  1123. not(src1 xor src2) and (src1 xor dst)
  1124. for subtraction, multiplication: invert src1 sign bit
  1125. for division: handle separately (div by zero, low(inttype) div -1),
  1126. not supported by this code
  1127. * unsigned overflow iff carry out, aka dst < src1 or dst < src2
  1128. }
  1129. location_reset(ovloc,LOC_REGISTER,OS_S32);
  1130. { not pasbool8, because then we'd still have to convert the integer to
  1131. a boolean via branches for Dalvik}
  1132. ovloc.register:=getintregister(list,s32inttype);
  1133. if not ((size.typ=pointerdef) or
  1134. ((size.typ=orddef) and
  1135. (torddef(size).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1136. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64]))) then
  1137. begin
  1138. a_load_reg_stack(list,size,src1);
  1139. if op in [OP_SUB,OP_IMUL] then
  1140. a_op_stack(list,OP_NOT,size,false);
  1141. a_op_reg_stack(list,OP_XOR,size,src2);
  1142. a_op_stack(list,OP_NOT,size,false);
  1143. a_load_reg_stack(list,size,src1);
  1144. a_op_reg_stack(list,OP_XOR,size,dst);
  1145. a_op_stack(list,OP_AND,size,false);
  1146. a_op_const_stack(list,OP_SHR,size,(size.size*8)-1);
  1147. if size.size=8 then
  1148. begin
  1149. list.concat(taicpu.op_none(a_l2i));
  1150. decstack(list,1);
  1151. end;
  1152. end
  1153. else
  1154. begin
  1155. a_load_const_stack(list,s32inttype,0,R_INTREGISTER);
  1156. current_asmdata.getjumplabel(lab);
  1157. { can be optimized by removing duplicate xor'ing to convert dst from
  1158. signed to unsigned quadrant }
  1159. a_cmp_reg_reg_label(list,size,OC_B,dst,src1,lab);
  1160. a_cmp_reg_reg_label(list,size,OC_B,dst,src2,lab);
  1161. a_op_const_stack(list,OP_XOR,s32inttype,1);
  1162. a_label(list,lab);
  1163. end;
  1164. a_load_stack_reg(list,s32inttype,ovloc.register);
  1165. end
  1166. else
  1167. ovloc.loc:=LOC_VOID;
  1168. end;
  1169. procedure thlcgjvm.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; const ref: treference; l: tasmlabel);
  1170. begin
  1171. if ref.base<>NR_EVAL_STACK_BASE then
  1172. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  1173. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1174. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  1175. a_cmp_stack_label(list,size,cmp_op,l);
  1176. end;
  1177. procedure thlcgjvm.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: tcgint; reg: tregister; l: tasmlabel);
  1178. begin
  1179. a_load_reg_stack(list,size,reg);
  1180. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1181. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  1182. a_cmp_stack_label(list,size,cmp_op,l);
  1183. end;
  1184. procedure thlcgjvm.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  1185. begin
  1186. a_load_reg_stack(list,size,reg);
  1187. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1188. if ref.base<>NR_EVAL_STACK_BASE then
  1189. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false))
  1190. else
  1191. list.concat(taicpu.op_none(a_swap));
  1192. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1193. a_cmp_stack_label(list,size,cmp_op,l);
  1194. end;
  1195. procedure thlcgjvm.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  1196. begin
  1197. if ref.base<>NR_EVAL_STACK_BASE then
  1198. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  1199. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1200. a_load_reg_stack(list,size,reg);
  1201. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1202. a_cmp_stack_label(list,size,cmp_op,l);
  1203. end;
  1204. procedure thlcgjvm.a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  1205. begin
  1206. a_load_reg_stack(list,size,reg2);
  1207. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1208. a_load_reg_stack(list,size,reg1);
  1209. maybe_adjust_cmp_stackval(list,size,cmp_op);
  1210. a_cmp_stack_label(list,size,cmp_op,l);
  1211. end;
  1212. procedure thlcgjvm.a_jmp_always(list: TAsmList; l: tasmlabel);
  1213. begin
  1214. list.concat(taicpu.op_sym(a_goto,current_asmdata.RefAsmSymbol(l.name,AT_METADATA)));
  1215. end;
  1216. procedure thlcgjvm.concatcopy_normal_array(list: TAsmList; size: tdef; const source, dest: treference);
  1217. var
  1218. procname: string;
  1219. eledef: tdef;
  1220. ndim: longint;
  1221. adddefaultlenparas: boolean;
  1222. begin
  1223. { load copy helper parameters on the stack }
  1224. a_load_ref_stack(list,java_jlobject,source,prepare_stack_for_ref(list,source,false));
  1225. a_load_ref_stack(list,java_jlobject,dest,prepare_stack_for_ref(list,dest,false));
  1226. { call copy helper }
  1227. eledef:=tarraydef(size).elementdef;
  1228. ndim:=1;
  1229. adddefaultlenparas:=true;
  1230. case eledef.typ of
  1231. orddef:
  1232. begin
  1233. case torddef(eledef).ordtype of
  1234. pasbool1,pasbool8,s8bit,u8bit,bool8bit,uchar,
  1235. s16bit,u16bit,bool16bit,pasbool16,
  1236. uwidechar,
  1237. s32bit,u32bit,bool32bit,pasbool32,
  1238. s64bit,u64bit,bool64bit,pasbool64,scurrency:
  1239. procname:='FPC_COPY_SHALLOW_ARRAY'
  1240. else
  1241. internalerror(2011020504);
  1242. end;
  1243. end;
  1244. arraydef:
  1245. begin
  1246. { call fpc_setlength_dynarr_multidim with deepcopy=true, and extra
  1247. parameters }
  1248. while (eledef.typ=arraydef) and
  1249. not is_dynamic_array(eledef) do
  1250. begin
  1251. eledef:=tarraydef(eledef).elementdef;
  1252. inc(ndim)
  1253. end;
  1254. if (ndim=1) then
  1255. procname:='FPC_COPY_SHALLOW_ARRAY'
  1256. else
  1257. begin
  1258. { deepcopy=true }
  1259. a_load_const_stack(list,pasbool1type,1,R_INTREGISTER);
  1260. { ndim }
  1261. a_load_const_stack(list,s32inttype,ndim,R_INTREGISTER);
  1262. { eletype }
  1263. a_load_const_stack(list,cwidechartype,ord(jvmarrtype_setlength(eledef)),R_INTREGISTER);
  1264. adddefaultlenparas:=false;
  1265. procname:='FPC_SETLENGTH_DYNARR_MULTIDIM';
  1266. end;
  1267. end;
  1268. recorddef:
  1269. procname:='FPC_COPY_JRECORD_ARRAY';
  1270. procvardef:
  1271. if tprocvardef(eledef).is_addressonly then
  1272. procname:='FPC_COPY_SHALLOW_ARRAY'
  1273. else
  1274. procname:='FPC_COPY_JPROCVAR_ARRAY';
  1275. setdef:
  1276. if tsetdef(eledef).elementdef.typ=enumdef then
  1277. procname:='FPC_COPY_JENUMSET_ARRAY'
  1278. else
  1279. procname:='FPC_COPY_JBITSET_ARRAY';
  1280. floatdef:
  1281. procname:='FPC_COPY_SHALLOW_ARRAY';
  1282. stringdef:
  1283. if is_shortstring(eledef) then
  1284. procname:='FPC_COPY_JSHORTSTRING_ARRAY'
  1285. else
  1286. procname:='FPC_COPY_SHALLOW_ARRAY';
  1287. variantdef:
  1288. begin
  1289. {$ifndef nounsupported}
  1290. procname:='FPC_COPY_SHALLOW_ARRAY';
  1291. {$else}
  1292. { todo: make a deep copy via clone... }
  1293. internalerror(2011020505);
  1294. {$endif}
  1295. end;
  1296. else
  1297. procname:='FPC_COPY_SHALLOW_ARRAY';
  1298. end;
  1299. if adddefaultlenparas then
  1300. begin
  1301. { -1, -1 means "copy entire array" }
  1302. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  1303. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  1304. end;
  1305. g_call_system_proc(list,procname,[],nil);
  1306. if ndim<>1 then
  1307. begin
  1308. { pop return value, must be the same as dest }
  1309. list.concat(taicpu.op_none(a_pop));
  1310. decstack(list,1);
  1311. end;
  1312. end;
  1313. procedure thlcgjvm.concatcopy_record(list: TAsmList; size: tdef; const source, dest: treference);
  1314. var
  1315. srsym: tsym;
  1316. pd: tprocdef;
  1317. begin
  1318. { self }
  1319. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1320. { result }
  1321. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1322. { call fpcDeepCopy helper }
  1323. srsym:=search_struct_member(tabstractrecorddef(size),'FPCDEEPCOPY');
  1324. if not assigned(srsym) or
  1325. (srsym.typ<>procsym) then
  1326. Message1(cg_f_unknown_compilerproc,size.typename+'.fpcDeepCopy');
  1327. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1328. a_call_name(list,pd,pd.mangledname,[],nil,false);
  1329. { both parameters are removed, no function result }
  1330. decstack(list,2);
  1331. end;
  1332. procedure thlcgjvm.concatcopy_set(list: TAsmList; size: tdef; const source, dest: treference);
  1333. begin
  1334. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1335. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1336. { call set copy helper }
  1337. if tsetdef(size).elementdef.typ=enumdef then
  1338. g_call_system_proc(list,'fpc_enumset_copy',[],nil)
  1339. else
  1340. g_call_system_proc(list,'fpc_bitset_copy',[],nil);
  1341. end;
  1342. procedure thlcgjvm.concatcopy_shortstring(list: TAsmList; size: tdef; const source, dest: treference);
  1343. var
  1344. srsym: tsym;
  1345. pd: tprocdef;
  1346. begin
  1347. { self }
  1348. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1349. { result }
  1350. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1351. { call fpcDeepCopy helper }
  1352. srsym:=search_struct_member(java_shortstring,'FPCDEEPCOPY');
  1353. if not assigned(srsym) or
  1354. (srsym.typ<>procsym) then
  1355. Message1(cg_f_unknown_compilerproc,'ShortstringClass.FpcDeepCopy');
  1356. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1357. a_call_name(list,pd,pd.mangledname,[],nil,false);
  1358. { both parameters are removed, no function result }
  1359. decstack(list,2);
  1360. end;
  1361. procedure thlcgjvm.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  1362. var
  1363. handled: boolean;
  1364. begin
  1365. handled:=false;
  1366. case size.typ of
  1367. arraydef:
  1368. begin
  1369. if not is_dynamic_array(size) then
  1370. begin
  1371. concatcopy_normal_array(list,size,source,dest);
  1372. handled:=true;
  1373. end;
  1374. end;
  1375. recorddef:
  1376. begin
  1377. concatcopy_record(list,size,source,dest);
  1378. handled:=true;
  1379. end;
  1380. setdef:
  1381. begin
  1382. concatcopy_set(list,size,source,dest);
  1383. handled:=true;
  1384. end;
  1385. stringdef:
  1386. begin
  1387. if is_shortstring(size) then
  1388. begin
  1389. concatcopy_shortstring(list,size,source,dest);
  1390. handled:=true;
  1391. end;
  1392. end;
  1393. procvardef:
  1394. begin
  1395. if not tprocvardef(size).is_addressonly then
  1396. begin
  1397. concatcopy_record(list,tcpuprocvardef(size).classdef,source,dest);
  1398. handled:=true;
  1399. end;
  1400. end;
  1401. else
  1402. ;
  1403. end;
  1404. if not handled then
  1405. inherited;
  1406. end;
  1407. procedure thlcgjvm.g_copyshortstring(list: TAsmList; const source, dest: treference; strdef: tstringdef);
  1408. begin
  1409. concatcopy_shortstring(list,strdef,source,dest);
  1410. end;
  1411. procedure thlcgjvm.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  1412. var
  1413. dstack_slots: longint;
  1414. begin
  1415. dstack_slots:=prepare_stack_for_ref(list,ref2,false);
  1416. a_load_ref_stack(list,fromsize,ref1,prepare_stack_for_ref(list,ref1,false));
  1417. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1418. a_load_stack_ref(list,tosize,ref2,dstack_slots);
  1419. end;
  1420. procedure thlcgjvm.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister);
  1421. begin
  1422. a_load_ref_stack(list,fromsize,ref,prepare_stack_for_ref(list,ref,false));
  1423. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1424. a_load_stack_reg(list,tosize,reg);
  1425. end;
  1426. procedure thlcgjvm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference);
  1427. var
  1428. dstack_slots: longint;
  1429. begin
  1430. dstack_slots:=prepare_stack_for_ref(list,ref,false);
  1431. a_load_reg_stack(list,fromsize,reg);
  1432. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1433. a_load_stack_ref(list,tosize,ref,dstack_slots);
  1434. end;
  1435. procedure thlcgjvm.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  1436. begin
  1437. a_load_reg_stack(list,fromsize,reg1);
  1438. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1439. a_load_stack_reg(list,tosize,reg2);
  1440. end;
  1441. procedure thlcgjvm.g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);
  1442. begin
  1443. { the localsize is based on tg.lasttemp -> already in terms of stack
  1444. slots rather than bytes }
  1445. list.concat(tai_directive.Create(asd_jlimit,'locals '+tostr(localsize)));
  1446. { we insert the unit initialisation code afterwards in the proginit code,
  1447. and it uses one stack slot }
  1448. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1449. fmaxevalstackheight:=max(1,fmaxevalstackheight);
  1450. list.concat(tai_directive.Create(asd_jlimit,'stack '+tostr(fmaxevalstackheight)));
  1451. end;
  1452. procedure thlcgjvm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  1453. var
  1454. retdef: tdef;
  1455. opc: tasmop;
  1456. begin
  1457. if current_procinfo.procdef.proctypeoption in [potype_constructor,potype_class_constructor] then
  1458. retdef:=voidtype
  1459. else
  1460. retdef:=current_procinfo.procdef.returndef;
  1461. case retdef.typ of
  1462. orddef:
  1463. case torddef(retdef).ordtype of
  1464. uvoid:
  1465. opc:=a_return;
  1466. s64bit,
  1467. u64bit,
  1468. scurrency:
  1469. opc:=a_lreturn;
  1470. else
  1471. opc:=a_ireturn;
  1472. end;
  1473. setdef:
  1474. opc:=a_areturn;
  1475. floatdef:
  1476. case tfloatdef(retdef).floattype of
  1477. s32real:
  1478. opc:=a_freturn;
  1479. s64real:
  1480. opc:=a_dreturn;
  1481. else
  1482. internalerror(2011010213);
  1483. end;
  1484. else
  1485. opc:=a_areturn;
  1486. end;
  1487. list.concat(taicpu.op_none(opc));
  1488. end;
  1489. procedure thlcgjvm.gen_load_return_value(list: TAsmList);
  1490. begin
  1491. { constructors don't return anything in the jvm }
  1492. if current_procinfo.procdef.proctypeoption in [potype_constructor,potype_class_constructor] then
  1493. exit;
  1494. inherited gen_load_return_value(list);
  1495. end;
  1496. procedure thlcgjvm.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  1497. begin
  1498. { add something to the al_procedures list as well, because if all al_*
  1499. lists are empty, the assembler writer isn't called }
  1500. if not code.empty and
  1501. current_asmdata.asmlists[al_procedures].empty then
  1502. current_asmdata.asmlists[al_procedures].concat(tai_align.Create(4));
  1503. tcpuprocdef(pd).exprasmlist:=TAsmList.create;
  1504. tcpuprocdef(pd).exprasmlist.concatlist(code);
  1505. if assigned(data) and
  1506. not data.empty then
  1507. internalerror(2010122801);
  1508. end;
  1509. procedure thlcgjvm.g_incrrefcount(list: TAsmList; t: tdef; const ref: treference);
  1510. begin
  1511. // do nothing
  1512. end;
  1513. procedure thlcgjvm.g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation; const name: string);
  1514. var
  1515. normaldim: longint;
  1516. eleref: treference;
  1517. begin
  1518. { only in case of initialisation, we have to set all elements to "empty" }
  1519. if name<>'fpc_initialize_array' then
  1520. exit;
  1521. { put array on the stack }
  1522. a_load_ref_stack(list,java_jlobject,ref,prepare_stack_for_ref(list,ref,false));
  1523. { in case it's an open array whose elements are regular arrays, put the
  1524. dimension of the regular arrays on the stack (otherwise pass 0) }
  1525. normaldim:=0;
  1526. while (t.typ=arraydef) and
  1527. not is_dynamic_array(t) do
  1528. begin
  1529. inc(normaldim);
  1530. t:=tarraydef(t).elementdef;
  1531. end;
  1532. a_load_const_stack(list,s32inttype,normaldim,R_INTREGISTER);
  1533. { highloc is invalid, the length is part of the array in Java }
  1534. if is_wide_or_unicode_string(t) then
  1535. g_call_system_proc(list,'fpc_initialize_array_unicodestring',[],nil)
  1536. else if is_ansistring(t) then
  1537. g_call_system_proc(list,'fpc_initialize_array_ansistring',[],nil)
  1538. else if is_dynamic_array(t) then
  1539. g_call_system_proc(list,'fpc_initialize_array_dynarr',[],nil)
  1540. else if is_record(t) or
  1541. (t.typ=setdef) then
  1542. begin
  1543. tg.gethltemp(list,t,t.size,tt_persistent,eleref);
  1544. a_load_ref_stack(list,t,eleref,prepare_stack_for_ref(list,eleref,false));
  1545. if is_record(t) then
  1546. g_call_system_proc(list,'fpc_initialize_array_record',[],nil)
  1547. else if tsetdef(t).elementdef.typ=enumdef then
  1548. g_call_system_proc(list,'fpc_initialize_array_enumset',[],nil)
  1549. else
  1550. g_call_system_proc(list,'fpc_initialize_array_bitset',[],nil);
  1551. tg.ungettemp(list,eleref);
  1552. end
  1553. else if (t.typ=enumdef) then
  1554. begin
  1555. if get_enum_init_val_ref(t,eleref) then
  1556. begin
  1557. a_load_ref_stack(list,java_jlobject,eleref,prepare_stack_for_ref(list,eleref,false));
  1558. g_call_system_proc(list,'fpc_initialize_array_object',[],nil);
  1559. end;
  1560. end
  1561. else
  1562. internalerror(2011031901);
  1563. end;
  1564. procedure thlcgjvm.g_initialize(list: TAsmList; t: tdef; const ref: treference);
  1565. var
  1566. dummyloc: tlocation;
  1567. sym: tsym;
  1568. pd: tprocdef;
  1569. begin
  1570. if (t.typ=arraydef) and
  1571. not is_dynamic_array(t) then
  1572. begin
  1573. dummyloc.loc:=LOC_INVALID;
  1574. g_array_rtti_helper(list,tarraydef(t).elementdef,ref,dummyloc,'fpc_initialize_array')
  1575. end
  1576. else if is_record(t) then
  1577. begin
  1578. { call the fpcInitializeRec method }
  1579. sym:=tsym(trecorddef(t).symtable.find('FPCINITIALIZEREC'));
  1580. if assigned(sym) and
  1581. (sym.typ=procsym) then
  1582. begin
  1583. if tprocsym(sym).procdeflist.Count<>1 then
  1584. internalerror(2011071713);
  1585. pd:=tprocdef(tprocsym(sym).procdeflist[0]);
  1586. end
  1587. else
  1588. internalerror(2013113008);
  1589. a_load_ref_stack(list,java_jlobject,ref,prepare_stack_for_ref(list,ref,false));
  1590. a_call_name(list,pd,pd.mangledname,[],nil,false);
  1591. { parameter removed, no result }
  1592. decstack(list,1);
  1593. end
  1594. else
  1595. a_load_const_ref(list,t,0,ref);
  1596. end;
  1597. procedure thlcgjvm.g_finalize(list: TAsmList; t: tdef; const ref: treference);
  1598. begin
  1599. // do nothing
  1600. end;
  1601. procedure thlcgjvm.g_overflowcheck(list: TAsmList; const Loc: tlocation; def: tdef);
  1602. begin
  1603. { not possible, need the original operands }
  1604. internalerror(2012102101);
  1605. end;
  1606. procedure thlcgjvm.g_overflowCheck_loc(List: TAsmList; const Loc: TLocation; def: TDef; var ovloc: tlocation);
  1607. var
  1608. hl : tasmlabel;
  1609. begin
  1610. if not(cs_check_overflow in current_settings.localswitches) then
  1611. exit;
  1612. current_asmdata.getjumplabel(hl);
  1613. a_cmp_const_loc_label(list,s32inttype,OC_EQ,0,ovloc,hl);
  1614. g_call_system_proc(list,'fpc_overflow',[],nil);
  1615. a_label(list,hl);
  1616. end;
  1617. procedure thlcgjvm.location_get_data_ref(list: TAsmList; def: tdef; const l: tlocation; var ref: treference; loadref: boolean; alignment: longint);
  1618. var
  1619. tmploc: tlocation;
  1620. begin
  1621. { This routine is a combination of a generalised a_loadaddr_ref_reg()
  1622. that also works for addresses in registers (in case loadref is false)
  1623. and of a_load_ref_reg (in case loadref is true). It is used for
  1624. a) getting the address of managed var/out parameters
  1625. b) getting to the actual data of value types that are passed by
  1626. reference by the compiler (and then get a local copy at the caller
  1627. side). Normally, depending on whether this reference is passed in a
  1628. register or reference, we either need a reference with that register
  1629. as base or load the address in that reference and use that as a new
  1630. base.
  1631. Since the JVM cannot take the address of anything, all
  1632. "pass-by-reference" value parameters (which are always aggregate types)
  1633. are already simply the implicit pointer to the data (since arrays,
  1634. records, etc are already internally implicit pointers). This means
  1635. that if "loadref" is true, we must simply return this implicit pointer.
  1636. If it is false, we are supposed the take the address of this implicit
  1637. pointer, which is not possible.
  1638. However, managed types are also implicit pointers in Pascal, so in that
  1639. case "taking the address" again consists of simply returning the
  1640. implicit pointer/current value (in case of a var/out parameter, this
  1641. value is stored inside an array).
  1642. }
  1643. if not loadref then
  1644. begin
  1645. if not is_managed_type(def) then
  1646. internalerror(2011020601);
  1647. tmploc:=l;
  1648. end
  1649. else
  1650. begin
  1651. if not jvmimplicitpointertype(def) then
  1652. begin
  1653. { passed by reference in array of single element; l contains the
  1654. base address of the array }
  1655. location_reset_ref(tmploc,LOC_REFERENCE,OS_ADDR,4,ref.volatility);
  1656. cgutils.reference_reset_base(tmploc.reference,getaddressregister(list,java_jlobject),0,tmploc.reference.temppos,4,ref.volatility);
  1657. tmploc.reference.arrayreftype:=art_indexconst;
  1658. tmploc.reference.indexoffset:=0;
  1659. a_load_loc_reg(list,java_jlobject,java_jlobject,l,tmploc.reference.base);
  1660. end
  1661. else
  1662. tmploc:=l;
  1663. end;
  1664. case tmploc.loc of
  1665. LOC_REGISTER,
  1666. LOC_CREGISTER :
  1667. begin
  1668. { the implicit pointer is in a register and has to be in a
  1669. reference -> create a reference and put it there }
  1670. location_force_mem(list,tmploc,java_jlobject);
  1671. ref:=tmploc.reference;
  1672. end;
  1673. LOC_REFERENCE,
  1674. LOC_CREFERENCE :
  1675. begin
  1676. ref:=tmploc.reference;
  1677. end;
  1678. else
  1679. internalerror(2011020603);
  1680. end;
  1681. end;
  1682. procedure thlcgjvm.maybe_change_load_node_reg(list: TAsmList; var n: tnode; reload: boolean);
  1683. begin
  1684. { don't do anything, all registers become stack locations anyway }
  1685. end;
  1686. procedure thlcgjvm.g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister);
  1687. var
  1688. localref: treference;
  1689. arrloc: tlocation;
  1690. stackslots: longint;
  1691. begin
  1692. { temporary reference for passing to concatcopy }
  1693. tg.gethltemp(list,java_jlobject,java_jlobject.size,tt_persistent,localref);
  1694. stackslots:=prepare_stack_for_ref(list,localref,false);
  1695. { create the local copy of the array (lenloc is invalid, get length
  1696. directly from the array) }
  1697. location_reset_ref(arrloc,LOC_REFERENCE,OS_ADDR,sizeof(pint),ref.volatility);
  1698. arrloc.reference:=ref;
  1699. g_getarraylen(list,arrloc);
  1700. g_newarray(list,arrdef,1);
  1701. a_load_stack_ref(list,java_jlobject,localref,stackslots);
  1702. { copy the source array to the destination }
  1703. g_concatcopy(list,arrdef,ref,localref);
  1704. { and put the array pointer in the register as expected by the caller }
  1705. a_load_ref_reg(list,java_jlobject,java_jlobject,localref,destreg);
  1706. end;
  1707. procedure thlcgjvm.g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation);
  1708. begin
  1709. // do nothing, long live garbage collection!
  1710. end;
  1711. procedure thlcgjvm.gen_initialize_code(list: TAsmList);
  1712. var
  1713. ref: treference;
  1714. begin
  1715. { create globals with wrapped types such as arrays/records }
  1716. case current_procinfo.procdef.proctypeoption of
  1717. potype_unitinit:
  1718. begin
  1719. cgutils.reference_reset_base(ref,NR_NO,0,ctempposinvalid,1,[]);
  1720. if assigned(current_module.globalsymtable) then
  1721. allocate_implicit_structs_for_st_with_base_ref(list,current_module.globalsymtable,ref,staticvarsym);
  1722. allocate_implicit_structs_for_st_with_base_ref(list,current_module.localsymtable,ref,staticvarsym);
  1723. end;
  1724. potype_class_constructor:
  1725. begin
  1726. { also initialise local variables, if any }
  1727. inherited;
  1728. { initialise class fields }
  1729. cgutils.reference_reset_base(ref,NR_NO,0,ctempposinvalid,1,[]);
  1730. allocate_implicit_structs_for_st_with_base_ref(list,tabstractrecorddef(current_procinfo.procdef.owner.defowner).symtable,ref,staticvarsym);
  1731. end
  1732. else
  1733. inherited
  1734. end;
  1735. end;
  1736. procedure thlcgjvm.gen_entry_code(list: TAsmList);
  1737. begin
  1738. list.concat(Tai_force_line.Create);
  1739. end;
  1740. procedure thlcgjvm.gen_exit_code(list: TAsmList);
  1741. begin
  1742. { nothing }
  1743. end;
  1744. procedure thlcgjvm.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: tdef; src, dst: tregister);
  1745. begin
  1746. internalerror(2012090201);
  1747. end;
  1748. procedure thlcgjvm.a_loadmm_loc_reg(list: TAsmList; fromsize, tosize: tdef; const loc: tlocation; const reg: tregister; shuffle: pmmshuffle);
  1749. begin
  1750. internalerror(2012090202);
  1751. end;
  1752. procedure thlcgjvm.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister; shuffle: pmmshuffle);
  1753. begin
  1754. internalerror(2012060130);
  1755. end;
  1756. procedure thlcgjvm.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  1757. begin
  1758. internalerror(2012060131);
  1759. end;
  1760. procedure thlcgjvm.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  1761. begin
  1762. internalerror(2012060132);
  1763. end;
  1764. procedure thlcgjvm.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; src, dst: tregister; shuffle: pmmshuffle);
  1765. begin
  1766. internalerror(2012060133);
  1767. end;
  1768. procedure thlcgjvm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tdef; intreg, mmreg: tregister; shuffle: pmmshuffle);
  1769. begin
  1770. internalerror(2012060134);
  1771. end;
  1772. procedure thlcgjvm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tdef; mmreg, intreg: tregister; shuffle: pmmshuffle);
  1773. begin
  1774. internalerror(2012060135);
  1775. end;
  1776. procedure thlcgjvm.g_stackpointer_alloc(list: TAsmList; size: longint);
  1777. begin
  1778. internalerror(2012090203);
  1779. end;
  1780. procedure thlcgjvm.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  1781. begin
  1782. internalerror(2012090204);
  1783. end;
  1784. procedure thlcgjvm.g_adjust_self_value(list: TAsmList; procdef: tprocdef; ioffset: aint);
  1785. begin
  1786. internalerror(2012090205);
  1787. end;
  1788. procedure thlcgjvm.g_local_unwind(list: TAsmList; l: TAsmLabel);
  1789. begin
  1790. internalerror(2012090206);
  1791. end;
  1792. procedure thlcgjvm.a_load_stack_reg(list: TAsmList; size: tdef; reg: tregister);
  1793. var
  1794. opc: tasmop;
  1795. finishandval: tcgint;
  1796. begin
  1797. opc:=loadstoreopc(size,false,false,finishandval);
  1798. list.concat(taicpu.op_reg(opc,reg));
  1799. { avoid problems with getting the size of an open array etc }
  1800. if jvmimplicitpointertype(size) then
  1801. size:=java_jlobject;
  1802. decstack(list,1+ord(size.size>4));
  1803. end;
  1804. procedure thlcgjvm.a_load_stack_ref(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1805. var
  1806. opc: tasmop;
  1807. finishandval: tcgint;
  1808. begin
  1809. { fake location that indicates the value has to remain on the stack }
  1810. if ref.base=NR_EVAL_STACK_BASE then
  1811. exit;
  1812. opc:=loadstoreopcref(size,false,ref,finishandval);
  1813. if ref.arrayreftype=art_none then
  1814. list.concat(taicpu.op_ref(opc,ref))
  1815. else
  1816. list.concat(taicpu.op_none(opc));
  1817. { avoid problems with getting the size of an open array etc }
  1818. if jvmimplicitpointertype(size) then
  1819. size:=java_jlobject;
  1820. decstack(list,1+ord(size.size>4)+extra_slots);
  1821. end;
  1822. procedure thlcgjvm.a_load_reg_stack(list: TAsmList; size: tdef; reg: tregister);
  1823. var
  1824. opc: tasmop;
  1825. finishandval: tcgint;
  1826. begin
  1827. opc:=loadstoreopc(size,true,false,finishandval);
  1828. list.concat(taicpu.op_reg(opc,reg));
  1829. { avoid problems with getting the size of an open array etc }
  1830. if jvmimplicitpointertype(size) then
  1831. size:=java_jlobject;
  1832. incstack(list,1+ord(size.size>4));
  1833. if finishandval<>-1 then
  1834. a_op_const_stack(list,OP_AND,size,finishandval);
  1835. end;
  1836. procedure thlcgjvm.a_load_ref_stack(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1837. var
  1838. opc: tasmop;
  1839. finishandval: tcgint;
  1840. begin
  1841. { fake location that indicates the value is already on the stack? }
  1842. if (ref.base=NR_EVAL_STACK_BASE) then
  1843. exit;
  1844. opc:=loadstoreopcref(size,true,ref,finishandval);
  1845. if ref.arrayreftype=art_none then
  1846. list.concat(taicpu.op_ref(opc,ref))
  1847. else
  1848. list.concat(taicpu.op_none(opc));
  1849. { avoid problems with getting the size of an open array etc }
  1850. if jvmimplicitpointertype(size) then
  1851. size:=java_jlobject;
  1852. incstack(list,1+ord(size.size>4)-extra_slots);
  1853. if finishandval<>-1 then
  1854. a_op_const_stack(list,OP_AND,size,finishandval);
  1855. if ref.checkcast then
  1856. gen_typecheck(list,a_checkcast,size);
  1857. end;
  1858. function thlcgjvm.loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: tcgint): tasmop;
  1859. const
  1860. { isload static }
  1861. getputopc: array[boolean,boolean] of tasmop =
  1862. ((a_putfield,a_putstatic),
  1863. (a_getfield,a_getstatic));
  1864. begin
  1865. if assigned(ref.symbol) then
  1866. begin
  1867. { -> either a global (static) field, or a regular field. If a regular
  1868. field, then ref.base contains the self pointer, otherwise
  1869. ref.base=NR_NO. In both cases, the symbol contains all other
  1870. information (combined field name and type descriptor) }
  1871. result:=getputopc[isload,ref.base=NR_NO];
  1872. finishandval:=-1;
  1873. { erase sign extension for byte/smallint loads }
  1874. if (def2regtyp(def)=R_INTREGISTER) and
  1875. not is_signed(def) and
  1876. (def.typ=orddef) and
  1877. not is_widechar(def) then
  1878. case def.size of
  1879. 1: if (torddef(def).high>127) then
  1880. finishandval:=255;
  1881. 2: if (torddef(def).high>32767) then
  1882. finishandval:=65535;
  1883. end;
  1884. end
  1885. else
  1886. result:=loadstoreopc(def,isload,ref.arrayreftype<>art_none,finishandval);
  1887. end;
  1888. function thlcgjvm.loadstoreopc(def: tdef; isload, isarray: boolean; out finishandval: tcgint): tasmop;
  1889. var
  1890. size: longint;
  1891. begin
  1892. finishandval:=-1;
  1893. case def2regtyp(def) of
  1894. R_INTREGISTER:
  1895. begin
  1896. size:=def.size;
  1897. if not isarray then
  1898. begin
  1899. case size of
  1900. 1,2,3,4:
  1901. if isload then
  1902. result:=a_iload
  1903. else
  1904. result:=a_istore;
  1905. 8:
  1906. if isload then
  1907. result:=a_lload
  1908. else
  1909. result:=a_lstore;
  1910. else
  1911. internalerror(2011032814);
  1912. end;
  1913. end
  1914. { array }
  1915. else if isload then
  1916. begin
  1917. case size of
  1918. 1:
  1919. begin
  1920. result:=a_baload;
  1921. if not is_signed(def) and
  1922. (def.typ=orddef) and
  1923. (torddef(def).high>127) then
  1924. finishandval:=255;
  1925. end;
  1926. 2:
  1927. begin
  1928. if is_widechar(def) then
  1929. result:=a_caload
  1930. else
  1931. begin
  1932. result:=a_saload;
  1933. { if we'd treat arrays of word as "array of widechar" we
  1934. could use a_caload, but that would make for even more
  1935. awkward interfacing with external Java code }
  1936. if not is_signed(def) and
  1937. (def.typ=orddef) and
  1938. (torddef(def).high>32767) then
  1939. finishandval:=65535;
  1940. end;
  1941. end;
  1942. 4: result:=a_iaload;
  1943. 8: result:=a_laload;
  1944. else
  1945. internalerror(2010120503);
  1946. end
  1947. end
  1948. else
  1949. begin
  1950. case size of
  1951. 1: result:=a_bastore;
  1952. 2: if not is_widechar(def) then
  1953. result:=a_sastore
  1954. else
  1955. result:=a_castore;
  1956. 4: result:=a_iastore;
  1957. 8: result:=a_lastore;
  1958. else
  1959. internalerror(2010120508);
  1960. end
  1961. end
  1962. end;
  1963. R_ADDRESSREGISTER:
  1964. if not isarray then
  1965. if isload then
  1966. result:=a_aload
  1967. else
  1968. result:=a_astore
  1969. else if isload then
  1970. result:=a_aaload
  1971. else
  1972. result:=a_aastore;
  1973. R_FPUREGISTER:
  1974. begin
  1975. case tfloatdef(def).floattype of
  1976. s32real:
  1977. if not isarray then
  1978. if isload then
  1979. result:=a_fload
  1980. else
  1981. result:=a_fstore
  1982. else if isload then
  1983. result:=a_faload
  1984. else
  1985. result:=a_fastore;
  1986. s64real:
  1987. if not isarray then
  1988. if isload then
  1989. result:=a_dload
  1990. else
  1991. result:=a_dstore
  1992. else if isload then
  1993. result:=a_daload
  1994. else
  1995. result:=a_dastore;
  1996. else
  1997. internalerror(2010120504);
  1998. end
  1999. end
  2000. else
  2001. internalerror(2010120502);
  2002. end;
  2003. end;
  2004. procedure thlcgjvm.resize_stack_int_val(list: TAsmList; fromsize, tosize: tdef; formemstore: boolean);
  2005. var
  2006. fromcgsize, tocgsize: tcgsize;
  2007. begin
  2008. { When storing to an array, field or global variable, make sure the
  2009. static type verification can determine that the stored value fits
  2010. within the boundaries of the declared type (to appease the Dalvik VM).
  2011. Local variables either get their type upgraded in the debug info,
  2012. or have no type information at all }
  2013. if formemstore and
  2014. (tosize.typ=orddef) then
  2015. if (torddef(tosize).ordtype in [u8bit,uchar]) then
  2016. tosize:=s8inttype
  2017. else if torddef(tosize).ordtype=u16bit then
  2018. tosize:=s16inttype;
  2019. fromcgsize:=def_cgsize(fromsize);
  2020. tocgsize:=def_cgsize(tosize);
  2021. if fromcgsize in [OS_S64,OS_64] then
  2022. begin
  2023. if not(tocgsize in [OS_S64,OS_64]) then
  2024. begin
  2025. { truncate }
  2026. list.concat(taicpu.op_none(a_l2i));
  2027. decstack(list,1);
  2028. end;
  2029. end
  2030. else if tocgsize in [OS_S64,OS_64] then
  2031. begin
  2032. { extend }
  2033. list.concat(taicpu.op_none(a_i2l));
  2034. incstack(list,1);
  2035. { if it was an unsigned 32 bit value, remove sign extension }
  2036. if fromcgsize=OS_32 then
  2037. a_op_const_stack(list,OP_AND,s64inttype,cardinal($ffffffff));
  2038. end;
  2039. { Conversions between 32 and 64 bit types have been completely handled
  2040. above. We still may have to truncate or sign extend in case the
  2041. destination type is smaller that the source type, or has a different
  2042. sign. In case the destination is a widechar and the source is not, we
  2043. also have to insert a conversion to widechar.
  2044. In case of Dalvik, we also have to insert conversions for e.g. byte
  2045. -> smallint, because truncating a byte happens via "and 255", and the
  2046. result is a longint in Dalvik's type verification model (so we have
  2047. to "truncate" it back to smallint) }
  2048. if (not(fromcgsize in [OS_S64,OS_64,OS_32,OS_S32]) or
  2049. not(tocgsize in [OS_S64,OS_64,OS_32,OS_S32])) and
  2050. (((current_settings.cputype=cpu_dalvik) and
  2051. not(tocgsize in [OS_32,OS_S32]) and
  2052. not is_signed(fromsize) and
  2053. is_signed(tosize)) or
  2054. (tcgsize2size[fromcgsize]>tcgsize2size[tocgsize]) or
  2055. ((tcgsize2size[fromcgsize]=tcgsize2size[tocgsize]) and
  2056. (fromcgsize<>tocgsize)) or
  2057. { needs to mask out the sign in the top 16 bits }
  2058. ((fromcgsize=OS_S8) and
  2059. (tocgsize=OS_16)) or
  2060. ((tosize=cwidechartype) and
  2061. (fromsize<>cwidechartype))) then
  2062. case tocgsize of
  2063. OS_8:
  2064. a_op_const_stack(list,OP_AND,s32inttype,255);
  2065. OS_S8:
  2066. list.concat(taicpu.op_none(a_i2b));
  2067. OS_16:
  2068. if (tosize.typ=orddef) and
  2069. (torddef(tosize).ordtype=uwidechar) then
  2070. list.concat(taicpu.op_none(a_i2c))
  2071. else
  2072. a_op_const_stack(list,OP_AND,s32inttype,65535);
  2073. OS_S16:
  2074. list.concat(taicpu.op_none(a_i2s));
  2075. else
  2076. ;
  2077. end;
  2078. end;
  2079. procedure thlcgjvm.maybe_resize_stack_para_val(list: TAsmList; retdef: tdef; callside: boolean);
  2080. var
  2081. convsize: tdef;
  2082. begin
  2083. if (retdef.typ=orddef) then
  2084. begin
  2085. if (torddef(retdef).ordtype in [u8bit,u16bit,uchar]) and
  2086. (torddef(retdef).high>=(1 shl (retdef.size*8-1))) then
  2087. begin
  2088. convsize:=nil;
  2089. if callside then
  2090. if torddef(retdef).ordtype in [u8bit,uchar] then
  2091. convsize:=s8inttype
  2092. else
  2093. convsize:=s16inttype
  2094. else if torddef(retdef).ordtype in [u8bit,uchar] then
  2095. convsize:=u8inttype
  2096. else
  2097. convsize:=u16inttype;
  2098. if assigned(convsize) then
  2099. resize_stack_int_val(list,s32inttype,convsize,false);
  2100. end;
  2101. end;
  2102. end;
  2103. procedure thlcgjvm.g_adjust_stack_after_call(list: TAsmList; pd: tabstractprocdef; paraheight: longint; forceresdef: tdef);
  2104. var
  2105. totalremovesize: longint;
  2106. realresdef: tdef;
  2107. begin
  2108. if not assigned(forceresdef) then
  2109. realresdef:=pd.returndef
  2110. else
  2111. realresdef:=forceresdef;
  2112. { a constructor doesn't actually return a value in the jvm }
  2113. if (tabstractprocdef(pd).proctypeoption=potype_constructor) then
  2114. totalremovesize:=paraheight
  2115. else
  2116. { even a byte takes up a full stackslot -> align size to multiple of 4 }
  2117. totalremovesize:=paraheight-(align(realresdef.size,4) shr 2);
  2118. { remove parameters from internal evaluation stack counter (in case of
  2119. e.g. no parameters and a result, it can also increase) }
  2120. if totalremovesize>0 then
  2121. decstack(list,totalremovesize)
  2122. else if totalremovesize<0 then
  2123. incstack(list,-totalremovesize);
  2124. end;
  2125. procedure thlcgjvm.allocate_implicit_struct_with_base_ref(list: TAsmList; vs: tabstractvarsym; ref: treference);
  2126. var
  2127. tmpref: treference;
  2128. begin
  2129. ref.symbol:=current_asmdata.RefAsmSymbol(vs.mangledname,AT_DATA);
  2130. tg.gethltemp(list,vs.vardef,vs.vardef.size,tt_persistent,tmpref);
  2131. { only copy the reference, not the actual data }
  2132. a_load_ref_ref(list,java_jlobject,java_jlobject,tmpref,ref);
  2133. { remains live since there's still a reference to the created
  2134. entity }
  2135. tg.ungettemp(list,tmpref);
  2136. end;
  2137. procedure thlcgjvm.allocate_enum_with_base_ref(list: TAsmList; vs: tabstractvarsym; const initref: treference; destbaseref: treference);
  2138. begin
  2139. destbaseref.symbol:=current_asmdata.RefAsmSymbol(vs.mangledname,AT_DATA);
  2140. { only copy the reference, not the actual data }
  2141. a_load_ref_ref(list,java_jlobject,java_jlobject,initref,destbaseref);
  2142. end;
  2143. function thlcgjvm.get_enum_init_val_ref(def: tdef; out ref: treference): boolean;
  2144. var
  2145. sym: tstaticvarsym;
  2146. begin
  2147. result:=false;
  2148. sym:=tstaticvarsym(tcpuenumdef(tenumdef(def).getbasedef).classdef.symtable.Find('__FPC_ZERO_INITIALIZER'));
  2149. { no enum with ordinal value 0 -> exit }
  2150. if not assigned(sym) then
  2151. exit;
  2152. reference_reset_symbol(ref,current_asmdata.RefAsmSymbol(sym.mangledname,AT_DATA),0,4,[]);
  2153. result:=true;
  2154. end;
  2155. procedure thlcgjvm.allocate_implicit_structs_for_st_with_base_ref(list: TAsmList; st: tsymtable; const ref: treference; allocvartyp: tsymtyp);
  2156. var
  2157. vs: tabstractvarsym;
  2158. def: tdef;
  2159. i: longint;
  2160. initref: treference;
  2161. begin
  2162. for i:=0 to st.symlist.count-1 do
  2163. begin
  2164. if (tsym(st.symlist[i]).typ<>allocvartyp) then
  2165. continue;
  2166. vs:=tabstractvarsym(st.symlist[i]);
  2167. if sp_static in vs.symoptions then
  2168. continue;
  2169. { vo_is_external and vo_has_local_copy means a staticvarsym that is
  2170. alias for a constsym, whose sole purpose is for allocating and
  2171. intialising the constant }
  2172. if [vo_is_external,vo_has_local_copy]*vs.varoptions=[vo_is_external] then
  2173. continue;
  2174. { threadvar innitializations are handled at the node tree level }
  2175. if vo_is_thread_var in vs.varoptions then
  2176. begin
  2177. { nothing }
  2178. end
  2179. else if jvmimplicitpointertype(vs.vardef) then
  2180. allocate_implicit_struct_with_base_ref(list,vs,ref)
  2181. { enums are class instances in Java, while they are ordinals in
  2182. Pascal. When they are initialized with enum(0), such as in
  2183. constructors or global variables, initialize them with the
  2184. enum instance for 0 if it exists (if not, it remains nil since
  2185. there is no valid enum value in it) }
  2186. else if (vs.vardef.typ=enumdef) and
  2187. ((vs.typ<>fieldvarsym) or
  2188. (tdef(vs.owner.defowner).typ<>objectdef) or
  2189. (ts_jvm_enum_field_init in current_settings.targetswitches)) and
  2190. get_enum_init_val_ref(vs.vardef,initref) then
  2191. allocate_enum_with_base_ref(list,vs,initref,ref);
  2192. end;
  2193. { process symtables of routines part of this symtable (for local typed
  2194. constants) }
  2195. if allocvartyp=staticvarsym then
  2196. begin
  2197. for i:=0 to st.deflist.count-1 do
  2198. begin
  2199. def:=tdef(st.deflist[i]);
  2200. { the unit symtable also contains the methods of classes defined
  2201. in that unit -> skip them when processing the unit itself.
  2202. Localst is not assigned for the main program code.
  2203. Localst can be the same as st in case of unit init code. }
  2204. if (def.typ<>procdef) or
  2205. (def.owner<>st) or
  2206. not assigned(tprocdef(def).localst) or
  2207. (tprocdef(def).localst=st) then
  2208. continue;
  2209. allocate_implicit_structs_for_st_with_base_ref(list,tprocdef(def).localst,ref,allocvartyp);
  2210. end;
  2211. end;
  2212. end;
  2213. procedure thlcgjvm.gen_initialize_fields_code(list: TAsmList);
  2214. var
  2215. sym: tsym;
  2216. selfpara: tparavarsym;
  2217. selfreg: tregister;
  2218. ref: treference;
  2219. obj: tabstractrecorddef;
  2220. i: longint;
  2221. needinit: boolean;
  2222. begin
  2223. obj:=tabstractrecorddef(current_procinfo.procdef.owner.defowner);
  2224. { check whether there are any fields that need initialisation }
  2225. needinit:=false;
  2226. for i:=0 to obj.symtable.symlist.count-1 do
  2227. begin
  2228. sym:=tsym(obj.symtable.symlist[i]);
  2229. if (sym.typ=fieldvarsym) and
  2230. not(sp_static in sym.symoptions) and
  2231. (jvmimplicitpointertype(tfieldvarsym(sym).vardef) or
  2232. ((tfieldvarsym(sym).vardef.typ=enumdef) and
  2233. get_enum_init_val_ref(tfieldvarsym(sym).vardef,ref))) then
  2234. begin
  2235. needinit:=true;
  2236. break;
  2237. end;
  2238. end;
  2239. if not needinit then
  2240. exit;
  2241. selfpara:=tparavarsym(current_procinfo.procdef.parast.find('self'));
  2242. if not assigned(selfpara) then
  2243. internalerror(2011033001);
  2244. selfreg:=getaddressregister(list,selfpara.vardef);
  2245. a_load_loc_reg(list,obj,obj,selfpara.localloc,selfreg);
  2246. cgutils.reference_reset_base(ref,selfreg,0,ctempposinvalid,1,[]);
  2247. allocate_implicit_structs_for_st_with_base_ref(list,obj.symtable,ref,fieldvarsym);
  2248. end;
  2249. procedure thlcgjvm.gen_typecheck(list: TAsmList; checkop: tasmop; checkdef: tdef);
  2250. begin
  2251. { replace special types with their equivalent class type }
  2252. if (checkdef.typ=pointerdef) and
  2253. jvmimplicitpointertype(tpointerdef(checkdef).pointeddef) then
  2254. checkdef:=tpointerdef(checkdef).pointeddef;
  2255. if (checkdef=voidpointertype) or
  2256. (checkdef.typ=formaldef) then
  2257. checkdef:=java_jlobject
  2258. else if checkdef.typ=enumdef then
  2259. checkdef:=tcpuenumdef(checkdef).classdef
  2260. else if checkdef.typ=setdef then
  2261. begin
  2262. if tsetdef(checkdef).elementdef.typ=enumdef then
  2263. checkdef:=java_juenumset
  2264. else
  2265. checkdef:=java_jubitset;
  2266. end
  2267. else if checkdef.typ=procvardef then
  2268. checkdef:=tcpuprocvardef(checkdef).classdef
  2269. else if is_wide_or_unicode_string(checkdef) then
  2270. checkdef:=java_jlstring
  2271. else if is_ansistring(checkdef) then
  2272. checkdef:=java_ansistring
  2273. else if is_shortstring(checkdef) then
  2274. checkdef:=java_shortstring;
  2275. if checkdef.typ in [objectdef,recorddef] then
  2276. list.concat(taicpu.op_sym(checkop,current_asmdata.RefAsmSymbol(tabstractrecorddef(checkdef).jvm_full_typename(true),AT_METADATA)))
  2277. else if checkdef.typ=classrefdef then
  2278. list.concat(taicpu.op_sym(checkop,current_asmdata.RefAsmSymbol('java/lang/Class',AT_METADATA)))
  2279. else
  2280. list.concat(taicpu.op_sym(checkop,current_asmdata.RefAsmSymbol(jvmencodetype(checkdef,false),AT_METADATA)));
  2281. end;
  2282. procedure thlcgjvm.resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  2283. begin
  2284. if (fromsize=OS_F32) and
  2285. (tosize=OS_F64) then
  2286. begin
  2287. list.concat(taicpu.op_none(a_f2d));
  2288. incstack(list,1);
  2289. end
  2290. else if (fromsize=OS_F64) and
  2291. (tosize=OS_F32) then
  2292. begin
  2293. list.concat(taicpu.op_none(a_d2f));
  2294. decstack(list,1);
  2295. end;
  2296. end;
  2297. procedure thlcgjvm.maybepreparedivu32(list: TAsmList; var op: topcg; size: tdef; out isdivu32: boolean);
  2298. begin
  2299. if (op=OP_DIV) and
  2300. (def_cgsize(size)=OS_32) then
  2301. begin
  2302. { needs zero-extension to 64 bit, because the JVM only supports
  2303. signed divisions }
  2304. resize_stack_int_val(list,u32inttype,s64inttype,false);
  2305. op:=OP_IDIV;
  2306. isdivu32:=true;
  2307. end
  2308. else
  2309. isdivu32:=false;
  2310. end;
  2311. function thlcgjvm.a_call_name_intern(list: TAsmList; pd: tprocdef; const s: TSymStr; forceresdef: tdef; inheritedcall: boolean): tcgpara;
  2312. var
  2313. opc: tasmop;
  2314. begin
  2315. {
  2316. invoke types:
  2317. * invokeinterface: call method from an interface (must also specify
  2318. number of parameters in terms of stack slot count!)
  2319. * invokespecial: invoke a constructor, method in a superclass,
  2320. or private instance method
  2321. * invokestatic: invoke a class method (private or not)
  2322. * invokevirtual: invoke a regular method
  2323. }
  2324. case pd.owner.symtabletype of
  2325. globalsymtable,
  2326. staticsymtable,
  2327. localsymtable:
  2328. { regular and nested procedures are turned into static methods }
  2329. opc:=a_invokestatic;
  2330. objectsymtable:
  2331. begin
  2332. case tobjectdef(pd.owner.defowner).objecttype of
  2333. odt_javaclass:
  2334. begin
  2335. if (po_classmethod in pd.procoptions) or
  2336. (pd.proctypeoption=potype_operator) then
  2337. opc:=a_invokestatic
  2338. else if (pd.visibility=vis_strictprivate) or
  2339. (pd.proctypeoption=potype_constructor) or
  2340. inheritedcall then
  2341. opc:=a_invokespecial
  2342. else
  2343. opc:=a_invokevirtual;
  2344. end;
  2345. odt_interfacejava:
  2346. { static interface methods are not allowed }
  2347. opc:=a_invokeinterface;
  2348. else
  2349. internalerror(2010122601);
  2350. end;
  2351. end;
  2352. recordsymtable:
  2353. begin
  2354. if (po_staticmethod in pd.procoptions) or
  2355. (pd.proctypeoption=potype_operator) then
  2356. opc:=a_invokestatic
  2357. else if (pd.visibility=vis_strictprivate) or
  2358. (pd.proctypeoption=potype_constructor) or
  2359. inheritedcall then
  2360. opc:=a_invokespecial
  2361. else
  2362. opc:=a_invokevirtual;
  2363. end
  2364. else
  2365. internalerror(2010122602);
  2366. end;
  2367. if (opc<>a_invokeinterface) then
  2368. list.concat(taicpu.op_sym(opc,current_asmdata.RefAsmSymbol(s,AT_FUNCTION)))
  2369. else
  2370. begin
  2371. pd.init_paraloc_info(calleeside);
  2372. list.concat(taicpu.op_sym_const(opc,current_asmdata.RefAsmSymbol(s,AT_FUNCTION),pd.calleeargareasize));
  2373. end;
  2374. result:=get_call_result_cgpara(pd,forceresdef);
  2375. end;
  2376. procedure create_hlcodegen;
  2377. begin
  2378. hlcg:=thlcgjvm.create;
  2379. create_codegen;
  2380. end;
  2381. begin
  2382. chlcgobj:=thlcgjvm;
  2383. end.