hlcgcpu.pas 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003
  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. cpubase, hlcgobj, cgbase, cgutils, parabase;
  26. type
  27. { thlcgjvm }
  28. thlcgjvm = class(thlcgobj)
  29. private
  30. fevalstackheight,
  31. fmaxevalstackheight: longint;
  32. public
  33. constructor create;
  34. procedure incstack(list : TAsmList;slots: longint);
  35. procedure decstack(list : TAsmList;slots: longint);
  36. function def2regtyp(def: tdef): tregistertype; override;
  37. procedure a_call_name(list : TAsmList;pd : tprocdef;const s : TSymStr; weak: boolean);override;
  38. procedure a_call_name_inherited(list : TAsmList;pd : tprocdef;const s : TSymStr);override;
  39. procedure a_load_const_reg(list : TAsmList;tosize : tdef;a : aint;register : tregister);override;
  40. procedure a_load_const_ref(list : TAsmList;tosize : tdef;a : aint;const ref : treference);override;
  41. procedure a_load_reg_ref(list : TAsmList;fromsize, tosize : tdef;register : tregister;const ref : treference);override;
  42. procedure a_load_reg_reg(list : TAsmList;fromsize, tosize : tdef;reg1,reg2 : tregister);override;
  43. procedure a_load_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;register : tregister);override;
  44. procedure a_load_ref_ref(list : TAsmList;fromsize, tosize : tdef;const sref : treference;const dref : treference);override;
  45. procedure a_loadaddr_ref_reg(list : TAsmList;fromsize, tosize : tdef;const ref : treference;r : tregister);override;
  46. procedure a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; reg: TRegister); override;
  47. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister); override;
  48. procedure a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference); override;
  49. procedure a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister); override;
  50. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister); override;
  51. procedure a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister); override;
  52. procedure a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const ref: treference; l: tasmlabel); override;
  53. procedure a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel); override;
  54. procedure a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel); override;
  55. procedure a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel); override;
  56. procedure a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel); override;
  57. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  58. procedure g_concatcopy(list : TAsmList;size: tdef; const source,dest : treference);override;
  59. procedure g_copyshortstring(list : TAsmList;const source,dest : treference;strdef:tstringdef);override;
  60. procedure a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference); override;
  61. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister); override;
  62. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference); override;
  63. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister); override;
  64. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean); override;
  65. procedure g_proc_exit(list : TAsmList;parasize:longint;nostackframe:boolean); override;
  66. procedure gen_load_return_value(list:TAsmList);override;
  67. procedure record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList); override;
  68. procedure g_incrrefcount(list : TAsmList;t: tdef; const ref: treference);override;
  69. procedure g_decrrefcount(list : TAsmList;t: tdef; const ref: treference);override;
  70. procedure g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation; const name: string); override;
  71. procedure g_initialize(list : TAsmList;t : tdef;const ref : treference);override;
  72. procedure g_finalize(list : TAsmList;t : tdef;const ref : treference);override;
  73. procedure location_get_data_ref(list:TAsmList;def: tdef; const l:tlocation;var ref:treference;loadref:boolean; alignment: longint);override;
  74. procedure g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister); override;
  75. procedure g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation); override;
  76. procedure gen_initialize_code(list: TAsmList); override;
  77. procedure gen_entry_code(list: TAsmList); override;
  78. procedure gen_exit_code(list: TAsmList); override;
  79. { JVM-specific routines }
  80. procedure a_load_stack_reg(list : TAsmList;size: tdef;reg: tregister);
  81. { extra_slots are the slots that are used by the reference, and that
  82. will be removed by the store operation }
  83. procedure a_load_stack_ref(list : TAsmList;size: tdef;const ref: treference;extra_slots: longint);
  84. procedure a_load_reg_stack(list : TAsmList;size: tdef;reg: tregister);
  85. { extra_slots are the slots that are used by the reference, and that
  86. will be removed by the load operation }
  87. procedure a_load_ref_stack(list : TAsmList;size: tdef;const ref: treference;extra_slots: longint);
  88. procedure a_load_const_stack(list : TAsmList;size: tdef;a :aint; typ: TRegisterType);
  89. procedure a_load_stack_loc(list : TAsmList;size: tdef;const loc: tlocation);
  90. procedure a_load_loc_stack(list : TAsmList;size: tdef;const loc: tlocation);
  91. procedure a_loadfpu_const_stack(list : TAsmList;size: tdef;a :double);
  92. procedure a_op_stack(list : TAsmList;op: topcg; size: tdef; trunc32: boolean);
  93. procedure a_op_const_stack(list : TAsmList;op: topcg; size: tdef;a : aint);
  94. procedure a_op_reg_stack(list : TAsmList;op: topcg; size: tdef;reg: tregister);
  95. procedure a_op_ref_stack(list : TAsmList;op: topcg; size: tdef;const ref: treference);
  96. procedure a_op_loc_stack(list : TAsmList;op: topcg; size: tdef;const loc: tlocation);
  97. procedure g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation); override;
  98. { assumes that initdim dimensions have already been pushed on the
  99. evaluation stack, and creates a new array of type arrdef with these
  100. dimensions }
  101. procedure g_newarray(list : TAsmList; arrdef: tdef; initdim: longint);
  102. { gets the length of the array whose reference is stored in arrloc,
  103. and puts it on the evaluation stack }
  104. procedure g_getarraylen(list : TAsmList; const arrloc: tlocation);
  105. { this routine expects that all values are already massaged into the
  106. required form (sign bits xor'ed for gt/lt comparisons for OS_32/OS_64,
  107. see http://stackoverflow.com/questions/4068973/c-performing-signed-comparison-in-unsigned-variables-without-casting ) }
  108. procedure a_cmp_stack_label(list : TAsmlist; size: tdef; cmp_op: topcmp; lab: tasmlabel);
  109. { these 2 routines perform the massaging expected by the previous one }
  110. procedure maybe_adjust_cmp_stackval(list : TAsmlist; size: tdef; cmp_op: topcmp);
  111. function maybe_adjust_cmp_constval(size: tdef; cmp_op: topcmp; a: aint): aint;
  112. { truncate/sign extend after performing operations on values < 32 bit
  113. that may have overflowed outside the range }
  114. procedure maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  115. { performs sign/zero extension as required }
  116. procedure resize_stack_int_val(list: TAsmList;fromsize,tosize: tcgsize; forarraystore: boolean);
  117. property maxevalstackheight: longint read fmaxevalstackheight;
  118. procedure gen_initialize_fields_code(list:TAsmList);
  119. protected
  120. procedure allocate_implicit_structs_for_st_with_base_ref(list: TAsmList; st: tsymtable; const ref: treference; allocvartyp: tsymtyp);
  121. procedure allocate_implicit_struct_with_base_ref(list: TAsmList; vs: tabstractvarsym; ref: treference);
  122. procedure gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara); override;
  123. procedure inittempvariables(list:TAsmList);override;
  124. { in case of an array, the array base address and index have to be
  125. put on the evaluation stack before the stored value; similarly, for
  126. fields the self pointer has to be loaded first. Also checks whether
  127. the reference is valid. If dup is true, the necessary values are stored
  128. twice. Returns how many stack slots have been consumed, disregarding
  129. the "dup". }
  130. function prepare_stack_for_ref(list: TAsmList; const ref: treference; dup: boolean): longint;
  131. { return the load/store opcode to load/store from/to ref; if the result
  132. has to be and'ed after a load to get the final value, that constant
  133. is returned in finishandval (otherwise that value is set to -1) }
  134. function loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: aint): tasmop;
  135. { return the load/store opcode to load/store from/to reg; if the result
  136. has to be and'ed after a load to get the final value, that constant
  137. is returned in finishandval (otherwise that value is set to -1) }
  138. function loadstoreopc(def: tdef; isload, isarray: boolean; out finishandval: aint): tasmop;
  139. procedure resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  140. { in case of an OS_32 OP_DIV, we have to use an OS_S64 OP_IDIV because the
  141. JVM does not support unsigned divisions }
  142. procedure maybepreparedivu32(list: TAsmList; var op: topcg; size: tdef; out isdivu32: boolean);
  143. { common implementation of a_call_* }
  144. procedure a_call_name_intern(list : TAsmList;pd : tprocdef;const s : TSymStr; inheritedcall: boolean);
  145. { concatcopy helpers }
  146. procedure concatcopy_normal_array(list: TAsmList; size: tdef; const source, dest: treference);
  147. procedure concatcopy_record(list: TAsmList; size: tdef; const source, dest: treference);
  148. procedure concatcopy_shortstring(list: TAsmList; size: tdef; const source, dest: treference);
  149. { generate a call to a routine in the system unit }
  150. procedure g_call_system_proc(list: TAsmList; const procname: string);
  151. end;
  152. procedure create_hlcodegen;
  153. const
  154. opcmp2if: array[topcmp] of tasmop = (A_None,
  155. a_ifeq,a_ifgt,a_iflt,a_ifge,a_ifle,
  156. a_ifne,a_ifle,a_iflt,a_ifge,a_ifgt);
  157. implementation
  158. uses
  159. verbose,cutils,globals,fmodule,
  160. defutil,
  161. aasmtai,aasmcpu,
  162. symtable,jvmdef,
  163. procinfo,cgcpu,tgobj;
  164. const
  165. TOpCG2IAsmOp : array[topcg] of TAsmOp=( { not = xor -1 }
  166. 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
  167. );
  168. TOpCG2LAsmOp : array[topcg] of TAsmOp=( { not = xor -1 }
  169. 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
  170. );
  171. constructor thlcgjvm.create;
  172. begin
  173. fevalstackheight:=0;
  174. fmaxevalstackheight:=0;
  175. end;
  176. procedure thlcgjvm.incstack(list: TasmList;slots: longint);
  177. begin
  178. if slots=0 then
  179. exit;
  180. inc(fevalstackheight,slots);
  181. if (fevalstackheight>fmaxevalstackheight) then
  182. fmaxevalstackheight:=fevalstackheight;
  183. if cs_asm_regalloc in current_settings.globalswitches then
  184. list.concat(tai_comment.Create(strpnew('allocated '+tostr(slots)+', stack height = '+tostr(fevalstackheight))));
  185. end;
  186. procedure thlcgjvm.decstack(list: TAsmList;slots: longint);
  187. begin
  188. if slots=0 then
  189. exit;
  190. dec(fevalstackheight,slots);
  191. if (fevalstackheight<0) and
  192. not(cs_no_regalloc in current_settings.globalswitches) then
  193. internalerror(2010120501);
  194. if cs_asm_regalloc in current_settings.globalswitches then
  195. list.concat(tai_comment.Create(strpnew(' freed '+tostr(slots)+', stack height = '+tostr(fevalstackheight))));
  196. end;
  197. function thlcgjvm.def2regtyp(def: tdef): tregistertype;
  198. begin
  199. case def.typ of
  200. { records and enums are implemented via classes }
  201. recorddef,
  202. enumdef:
  203. result:=R_ADDRESSREGISTER;
  204. setdef:
  205. if is_smallset(def) then
  206. result:=R_INTREGISTER
  207. else
  208. result:=R_ADDRESSREGISTER;
  209. { shortstrings are implemented via classes }
  210. else if is_shortstring(def) or
  211. { voiddef can only be typecasted into (implicit) pointers }
  212. is_void(def) then
  213. result:=R_ADDRESSREGISTER
  214. else
  215. result:=inherited;
  216. end;
  217. end;
  218. procedure thlcgjvm.a_call_name(list: TAsmList; pd: tprocdef; const s: TSymStr; weak: boolean);
  219. begin
  220. a_call_name_intern(list,pd,s,false);
  221. end;
  222. procedure thlcgjvm.a_call_name_inherited(list: TAsmList; pd: tprocdef; const s: TSymStr);
  223. begin
  224. a_call_name_intern(list,pd,s,true);
  225. end;
  226. procedure thlcgjvm.a_load_const_stack(list : TAsmList;size : tdef;a : aint; typ: TRegisterType);
  227. const
  228. int2opc: array[-1..5] of tasmop = (a_iconst_m1,a_iconst_0,a_iconst_1,
  229. a_iconst_2,a_iconst_3,a_iconst_4,a_iconst_5);
  230. begin
  231. case typ of
  232. R_INTREGISTER:
  233. begin
  234. case def_cgsize(size) of
  235. OS_8,OS_16,OS_32,
  236. OS_S8,OS_S16,OS_S32:
  237. begin
  238. { convert cardinals to longints }
  239. a:=longint(a);
  240. if (a>=-1) and
  241. (a<=5) then
  242. list.concat(taicpu.op_none(int2opc[a]))
  243. else if (a>=low(shortint)) and
  244. (a<=high(shortint)) then
  245. list.concat(taicpu.op_const(a_bipush,a))
  246. else if (a>=low(smallint)) and
  247. (a<=high(smallint)) then
  248. list.concat(taicpu.op_const(a_sipush,a))
  249. else
  250. list.concat(taicpu.op_const(a_ldc,a));
  251. end;
  252. OS_64,OS_S64:
  253. begin
  254. case a of
  255. 0:
  256. list.concat(taicpu.op_none(a_lconst_0));
  257. 1:
  258. list.concat(taicpu.op_none(a_lconst_1));
  259. else
  260. list.concat(taicpu.op_const(a_ldc2_w,a));
  261. end;
  262. incstack(list,1);
  263. end;
  264. else
  265. internalerror(2010110702);
  266. end;
  267. end;
  268. R_ADDRESSREGISTER:
  269. begin
  270. if a<>0 then
  271. internalerror(2010110701);
  272. list.concat(taicpu.op_none(a_aconst_null));
  273. end;
  274. else
  275. internalerror(2010110703);
  276. end;
  277. incstack(list,1);
  278. end;
  279. procedure thlcgjvm.a_load_stack_loc(list: TAsmList; size: tdef; const loc: tlocation);
  280. begin
  281. case loc.loc of
  282. LOC_REGISTER,LOC_CREGISTER,
  283. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  284. a_load_stack_reg(list,size,loc.register);
  285. LOC_REFERENCE:
  286. a_load_stack_ref(list,size,loc.reference,prepare_stack_for_ref(list,loc.reference,false));
  287. else
  288. internalerror(2011020501);
  289. end;
  290. end;
  291. procedure thlcgjvm.a_load_loc_stack(list: TAsmList;size: tdef;const loc: tlocation);
  292. begin
  293. case loc.loc of
  294. LOC_REGISTER,LOC_CREGISTER,
  295. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  296. a_load_reg_stack(list,size,loc.register);
  297. LOC_REFERENCE,LOC_CREFERENCE:
  298. a_load_ref_stack(list,size,loc.reference,prepare_stack_for_ref(list,loc.reference,false));
  299. LOC_CONSTANT:
  300. a_load_const_stack(list,size,loc.value,def2regtyp(size));
  301. else
  302. internalerror(2011010401);
  303. end;
  304. end;
  305. procedure thlcgjvm.a_loadfpu_const_stack(list: TAsmList; size: tdef; a: double);
  306. begin
  307. case tfloatdef(size).floattype of
  308. s32real:
  309. begin
  310. if a=0.0 then
  311. list.concat(taicpu.op_none(a_fconst_0))
  312. else if a=1.0 then
  313. list.concat(taicpu.op_none(a_fconst_1))
  314. else if a=2.0 then
  315. list.concat(taicpu.op_none(a_fconst_2))
  316. else
  317. list.concat(taicpu.op_single(a_ldc,a));
  318. incstack(list,1);
  319. end;
  320. s64real:
  321. begin
  322. if a=0.0 then
  323. list.concat(taicpu.op_none(a_dconst_0))
  324. else if a=1.0 then
  325. list.concat(taicpu.op_none(a_dconst_1))
  326. else
  327. list.concat(taicpu.op_double(a_ldc2_w,a));
  328. incstack(list,2);
  329. end
  330. else
  331. internalerror(2011010501);
  332. end;
  333. end;
  334. procedure thlcgjvm.a_op_stack(list: TAsmList; op: topcg; size: tdef; trunc32: boolean);
  335. var
  336. cgsize: tcgsize;
  337. begin
  338. if not trunc32 then
  339. cgsize:=def_cgsize(size)
  340. else
  341. begin
  342. resize_stack_int_val(list,OS_32,OS_S64,false);
  343. cgsize:=OS_S64;
  344. end;
  345. case cgsize of
  346. OS_8,OS_S8,
  347. OS_16,OS_S16,
  348. OS_32,OS_S32:
  349. begin
  350. { not = xor 1 for boolean, xor -1 for the rest}
  351. if op=OP_NOT then
  352. begin
  353. if not is_pasbool(size) then
  354. a_load_const_stack(list,s32inttype,high(cardinal),R_INTREGISTER)
  355. else
  356. a_load_const_stack(list,size,1,R_INTREGISTER);
  357. op:=OP_XOR;
  358. end;
  359. if TOpCG2IAsmOp[op]=A_None then
  360. internalerror(2010120532);
  361. list.concat(taicpu.op_none(TOpCG2IAsmOp[op]));
  362. maybe_adjust_op_result(list,op,size);
  363. if op<>OP_NEG then
  364. decstack(list,1);
  365. end;
  366. OS_64,OS_S64:
  367. begin
  368. { unsigned 64 bit division must be done via a helper }
  369. if op=OP_DIV then
  370. internalerror(2010120530);
  371. { not = xor -1 }
  372. if op=OP_NOT then
  373. begin
  374. a_load_const_stack(list,s64inttype,-1,R_INTREGISTER);
  375. op:=OP_XOR;
  376. end;
  377. if TOpCG2LAsmOp[op]=A_None then
  378. internalerror(2010120533);
  379. list.concat(taicpu.op_none(TOpCG2LAsmOp[op]));
  380. case op of
  381. OP_NOT,
  382. OP_NEG:
  383. ;
  384. { the second argument here is an int rather than a long }
  385. OP_SHL,OP_SHR,OP_SAR:
  386. decstack(list,1);
  387. else
  388. decstack(list,2);
  389. end;
  390. end;
  391. else
  392. internalerror(2010120531);
  393. end;
  394. if trunc32 then
  395. begin
  396. list.concat(taicpu.op_none(a_l2i));
  397. decstack(list,1);
  398. end;
  399. end;
  400. procedure thlcgjvm.a_op_const_stack(list: TAsmList;op: topcg;size: tdef;a: aint);
  401. var
  402. trunc32: boolean;
  403. begin
  404. { use "integer to (wide)char" narrowing opcode for "and 65535" }
  405. if (op=OP_AND) and
  406. (def_cgsize(size) in [OS_16,OS_S16,OS_32,OS_S32]) and
  407. (a=65535) then
  408. list.concat(taicpu.op_none(a_i2c))
  409. else
  410. begin
  411. maybepreparedivu32(list,op,size,trunc32);
  412. case op of
  413. OP_NEG,OP_NOT:
  414. internalerror(2011010801);
  415. OP_SHL,OP_SHR,OP_SAR:
  416. { the second argument here is an int rather than a long }
  417. a_load_const_stack(list,s32inttype,a,R_INTREGISTER);
  418. else
  419. a_load_const_stack(list,size,a,R_INTREGISTER);
  420. end;
  421. a_op_stack(list,op,size,trunc32);
  422. end;
  423. end;
  424. procedure thlcgjvm.a_op_reg_stack(list: TAsmList; op: topcg; size: tdef; reg: tregister);
  425. var
  426. trunc32: boolean;
  427. begin
  428. maybepreparedivu32(list,op,size,trunc32);
  429. case op of
  430. OP_NEG,OP_NOT:
  431. ;
  432. OP_SHL,OP_SHR,OP_SAR:
  433. if not is_64bitint(size) then
  434. a_load_reg_stack(list,size,reg)
  435. else
  436. begin
  437. { the second argument here is an int rather than a long }
  438. if getsubreg(reg)=R_SUBQ then
  439. internalerror(2011010802);
  440. a_load_reg_stack(list,s32inttype,reg)
  441. end
  442. else
  443. a_load_reg_stack(list,size,reg);
  444. end;
  445. a_op_stack(list,op,size,trunc32);
  446. end;
  447. procedure thlcgjvm.a_op_ref_stack(list: TAsmList; op: topcg; size: tdef; const ref: treference);
  448. var
  449. trunc32: boolean;
  450. begin
  451. { ref must not be the stack top, because that may indicate an error
  452. (it means that we will perform an operation of the stack top onto
  453. itself, so that means the two values have been loaded manually prior
  454. to calling this routine, instead of letting this routine load one of
  455. them; if something like that is needed, call a_op_stack() directly) }
  456. if ref.base=NR_EVAL_STACK_BASE then
  457. internalerror(2010121102);
  458. maybepreparedivu32(list,op,size,trunc32);
  459. case op of
  460. OP_NEG,OP_NOT:
  461. ;
  462. OP_SHL,OP_SHR,OP_SAR:
  463. begin
  464. if not is_64bitint(size) then
  465. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false))
  466. else
  467. a_load_ref_stack(list,s32inttype,ref,prepare_stack_for_ref(list,ref,false));
  468. end;
  469. else
  470. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  471. end;
  472. a_op_stack(list,op,size,trunc32);
  473. end;
  474. procedure thlcgjvm.a_op_loc_stack(list: TAsmList; op: topcg; size: tdef; const loc: tlocation);
  475. begin
  476. case loc.loc of
  477. LOC_REGISTER,LOC_CREGISTER:
  478. a_op_reg_stack(list,op,size,loc.register);
  479. LOC_REFERENCE,LOC_CREFERENCE:
  480. a_op_ref_stack(list,op,size,loc.reference);
  481. LOC_CONSTANT:
  482. a_op_const_stack(list,op,size,loc.value);
  483. else
  484. internalerror(2011011415)
  485. end;
  486. end;
  487. procedure thlcgjvm.g_reference_loc(list: TAsmList; def: tdef; const fromloc: tlocation; out toloc: tlocation);
  488. procedure handle_reg_move(regsize: tdef; const fromreg: tregister; out toreg: tregister; regtyp: tregistertype);
  489. begin
  490. case regtyp of
  491. R_INTREGISTER:
  492. toreg:=getintregister(list,regsize);
  493. R_ADDRESSREGISTER:
  494. toreg:=getaddressregister(list,regsize);
  495. R_FPUREGISTER:
  496. toreg:=getfpuregister(list,regsize);
  497. end;
  498. a_load_reg_reg(list,regsize,regsize,fromreg,toreg);
  499. end;
  500. begin
  501. toloc:=fromloc;
  502. case fromloc.loc of
  503. { volatile location, can't get a permanent reference }
  504. LOC_REGISTER,
  505. LOC_FPUREGISTER:
  506. internalerror(2011031406);
  507. LOC_CONSTANT:
  508. { finished }
  509. ;
  510. LOC_CREGISTER:
  511. handle_reg_move(def,fromloc.reference.index,toloc.reference.index,R_INTREGISTER);
  512. LOC_CFPUREGISTER:
  513. handle_reg_move(def,fromloc.reference.index,toloc.reference.index,R_FPUREGISTER);
  514. { although LOC_CREFERENCE cannot be an lvalue, we may want to take a
  515. reference to such a location for multiple reading }
  516. LOC_CREFERENCE,
  517. LOC_REFERENCE:
  518. begin
  519. if (fromloc.reference.base<>NR_NO) and
  520. (fromloc.reference.base<>current_procinfo.framepointer) and
  521. (fromloc.reference.base<>NR_STACK_POINTER_REG) then
  522. handle_reg_move(java_jlobject,fromloc.reference.base,toloc.reference.base,R_ADDRESSREGISTER);
  523. case fromloc.reference.arrayreftype of
  524. art_indexreg:
  525. begin
  526. { all array indices in Java are 32 bit ints }
  527. handle_reg_move(s32inttype,fromloc.reference.index,toloc.reference.index,R_INTREGISTER);
  528. end;
  529. art_indexref:
  530. begin
  531. if (fromloc.reference.indexbase<>NR_NO) and
  532. (fromloc.reference.indexbase<>NR_STACK_POINTER_REG) then
  533. handle_reg_move(s32inttype,fromloc.reference.indexbase,toloc.reference.indexbase,R_ADDRESSREGISTER);
  534. end;
  535. end;
  536. end;
  537. else
  538. internalerror(2011031407);
  539. end;
  540. end;
  541. procedure thlcgjvm.g_newarray(list: TAsmList; arrdef: tdef; initdim: longint);
  542. var
  543. recref: treference;
  544. elemdef: tdef;
  545. i: longint;
  546. mangledname: string;
  547. opc: tasmop;
  548. primitivetype: boolean;
  549. begin
  550. elemdef:=arrdef;
  551. if initdim>1 then
  552. begin
  553. { multianewarray typedesc ndim }
  554. list.concat(taicpu.op_sym_const(a_multianewarray,
  555. current_asmdata.RefAsmSymbol(jvmarrtype(elemdef,primitivetype)),initdim));
  556. { has to be a multi-dimensional array type }
  557. if primitivetype then
  558. internalerror(2011012207);
  559. end
  560. else
  561. begin
  562. { for primitive types:
  563. newarray typedesc
  564. for reference types:
  565. anewarray typedesc
  566. }
  567. { get the type of the elements of the array we are creating }
  568. elemdef:=tarraydef(arrdef).elementdef;
  569. mangledname:=jvmarrtype(elemdef,primitivetype);
  570. if primitivetype then
  571. opc:=a_newarray
  572. else
  573. opc:=a_anewarray;
  574. list.concat(taicpu.op_sym(opc,current_asmdata.RefAsmSymbol(mangledname)));
  575. end;
  576. { all dimensions are removed from the stack, an array reference is
  577. added }
  578. decstack(list,initdim-1);
  579. { in case of an array of records or shortstrings, initialise }
  580. elemdef:=tarraydef(arrdef).elementdef;
  581. for i:=1 to pred(initdim) do
  582. elemdef:=tarraydef(elemdef).elementdef;
  583. if (elemdef.typ=recorddef) or
  584. is_shortstring(elemdef) then
  585. begin
  586. { duplicate array reference }
  587. list.concat(taicpu.op_none(a_dup));
  588. incstack(list,1);
  589. a_load_const_stack(list,s32inttype,initdim-1,R_INTREGISTER);
  590. if elemdef.typ=recorddef then
  591. begin
  592. tg.gethltemp(list,elemdef,elemdef.size,tt_persistent,recref);
  593. a_load_ref_stack(list,elemdef,recref,prepare_stack_for_ref(list,recref,false));
  594. g_call_system_proc(list,'fpc_initialize_array_record');
  595. tg.ungettemp(list,recref);
  596. end
  597. else
  598. begin
  599. a_load_const_stack(list,u8inttype,tstringdef(elemdef).len,R_INTREGISTER);
  600. g_call_system_proc(list,'fpc_initialize_array_shortstring');
  601. end;
  602. decstack(list,3);
  603. end;
  604. end;
  605. procedure thlcgjvm.g_getarraylen(list: TAsmList; const arrloc: tlocation);
  606. var
  607. nillab,endlab: tasmlabel;
  608. begin
  609. { inline because we have to use the arraylength opcode, which
  610. cannot be represented directly in Pascal. Even though the JVM
  611. supports allocated arrays with length=0, we still also have to
  612. check for nil pointers because even if FPC always generates
  613. allocated empty arrays under all circumstances, external Java
  614. code could pass in nil pointers.
  615. Note that this means that assigned(arr) can be different from
  616. length(arr)<>0 for dynamic arrays when targeting the JVM.
  617. }
  618. current_asmdata.getjumplabel(nillab);
  619. current_asmdata.getjumplabel(endlab);
  620. { if assigned(arr) ... }
  621. a_load_loc_stack(list,java_jlobject,arrloc);
  622. list.concat(taicpu.op_none(a_dup));
  623. incstack(list,1);
  624. list.concat(taicpu.op_sym(a_ifnull,nillab));
  625. decstack(list,1);
  626. { ... then result:=arraylength(arr) ... }
  627. list.concat(taicpu.op_none(a_arraylength));
  628. a_jmp_always(list,endlab);
  629. { ... else result:=0 }
  630. a_label(list,nillab);
  631. list.concat(taicpu.op_none(a_pop));
  632. decstack(list,1);
  633. list.concat(taicpu.op_none(a_iconst_0));
  634. incstack(list,1);
  635. a_label(list,endlab);
  636. end;
  637. procedure thlcgjvm.a_cmp_stack_label(list: TAsmlist; size: tdef; cmp_op: topcmp; lab: tasmlabel);
  638. const
  639. opcmp2icmp: array[topcmp] of tasmop = (A_None,
  640. a_if_icmpeq,a_if_icmpgt,a_if_icmplt,a_if_icmpge,a_if_icmple,
  641. a_if_icmpne,a_if_icmple,a_if_icmplt,a_if_icmpge,a_if_icmpgt);
  642. var
  643. cgsize: tcgsize;
  644. begin
  645. case def2regtyp(size) of
  646. R_INTREGISTER:
  647. begin
  648. cgsize:=def_cgsize(size);
  649. case cgsize of
  650. OS_S8,OS_8,
  651. OS_16,OS_S16,
  652. OS_S32,OS_32:
  653. begin
  654. list.concat(taicpu.op_sym(opcmp2icmp[cmp_op],lab));
  655. decstack(list,2);
  656. end;
  657. OS_64,OS_S64:
  658. begin
  659. list.concat(taicpu.op_none(a_lcmp));
  660. decstack(list,3);
  661. list.concat(taicpu.op_sym(opcmp2if[cmp_op],lab));
  662. decstack(list,1);
  663. end;
  664. else
  665. internalerror(2010120538);
  666. end;
  667. end;
  668. R_ADDRESSREGISTER:
  669. begin
  670. case cmp_op of
  671. OC_EQ:
  672. list.concat(taicpu.op_sym(a_if_acmpeq,lab));
  673. OC_NE:
  674. list.concat(taicpu.op_sym(a_if_acmpne,lab));
  675. else
  676. internalerror(2010120537);
  677. end;
  678. decstack(list,2);
  679. end;
  680. else
  681. internalerror(2010120538);
  682. end;
  683. end;
  684. procedure thlcgjvm.maybe_adjust_cmp_stackval(list: TAsmlist; size: tdef; cmp_op: topcmp);
  685. begin
  686. if (cmp_op in [OC_EQ,OC_NE]) or
  687. (def2regtyp(size)<>R_INTREGISTER) then
  688. exit;
  689. { http://stackoverflow.com/questions/4068973/c-performing-signed-comparison-in-unsigned-variables-without-casting }
  690. case def_cgsize(size) of
  691. OS_32:
  692. a_op_const_stack(list,OP_XOR,size,cardinal($80000000));
  693. OS_64:
  694. a_op_const_stack(list,OP_XOR,size,aint($8000000000000000));
  695. end;
  696. end;
  697. function thlcgjvm.maybe_adjust_cmp_constval(size: tdef; cmp_op: topcmp; a: aint): aint;
  698. begin
  699. result:=a;
  700. if (cmp_op in [OC_EQ,OC_NE]) or
  701. (def2regtyp(size)<>R_INTREGISTER) then
  702. exit;
  703. case def_cgsize(size) of
  704. OS_32:
  705. result:=a xor cardinal($80000000);
  706. OS_64:
  707. result:=a xor aint($8000000000000000);
  708. end;
  709. end;
  710. procedure thlcgjvm.maybe_adjust_op_result(list: TAsmList; op: TOpCg; size: tdef);
  711. const
  712. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  713. begin
  714. if (op in overflowops) and
  715. (def_cgsize(size) in [OS_8,OS_S8,OS_16,OS_S16]) then
  716. resize_stack_int_val(list,OS_S32,def_cgsize(size),false);
  717. end;
  718. procedure thlcgjvm.gen_load_uninitialized_function_result(list: TAsmList; pd: tprocdef; resdef: tdef; const resloc: tcgpara);
  719. begin
  720. { constructors don't return anything in Java }
  721. if pd.proctypeoption=potype_constructor then
  722. exit;
  723. { must return a value of the correct type on the evaluation stack }
  724. case def2regtyp(resdef) of
  725. R_INTREGISTER,
  726. R_ADDRESSREGISTER:
  727. a_load_const_cgpara(list,resdef,0,resloc);
  728. R_FPUREGISTER:
  729. case tfloatdef(resdef).floattype of
  730. s32real:
  731. list.concat(taicpu.op_none(a_fconst_0));
  732. s64real:
  733. list.concat(taicpu.op_none(a_dconst_0));
  734. else
  735. internalerror(2011010302);
  736. end
  737. else
  738. internalerror(2011010301);
  739. end;
  740. end;
  741. procedure thlcgjvm.inittempvariables(list: TAsmList);
  742. begin
  743. { these are automatically initialised when allocated if necessary }
  744. end;
  745. function thlcgjvm.prepare_stack_for_ref(list: TAsmList; const ref: treference; dup: boolean): longint;
  746. var
  747. href: treference;
  748. begin
  749. result:=0;
  750. { fake location that indicates the value is already on the stack? }
  751. if (ref.base=NR_EVAL_STACK_BASE) then
  752. exit;
  753. if ref.arrayreftype=art_none then
  754. begin
  755. { non-array accesses cannot have an index reg }
  756. if ref.index<>NR_NO then
  757. internalerror(2010120509);
  758. if (ref.base<>NR_NO) then
  759. begin
  760. if (ref.base<>NR_STACK_POINTER_REG) then
  761. begin
  762. { regular field -> load self on the stack }
  763. a_load_reg_stack(list,voidpointertype,ref.base);
  764. if dup then
  765. begin
  766. list.concat(taicpu.op_none(a_dup));
  767. incstack(list,1);
  768. end;
  769. { field name/type encoded in symbol, no index/offset }
  770. if not assigned(ref.symbol) or
  771. (ref.offset<>0) then
  772. internalerror(2010120524);
  773. result:=1;
  774. end
  775. else
  776. begin
  777. { local variable -> offset encoded in opcode and nothing to
  778. do here, except for checking that it's a valid reference }
  779. if assigned(ref.symbol) then
  780. internalerror(2010120523);
  781. end;
  782. end
  783. else
  784. begin
  785. { static field -> nothing to do here, except for validity check }
  786. if not assigned(ref.symbol) or
  787. (ref.offset<>0) then
  788. internalerror(2010120525);
  789. end;
  790. end
  791. else
  792. begin
  793. { arrays have implicit dereference -> pointer to array must have been
  794. loaded into base reg }
  795. if (ref.base=NR_NO) or
  796. (ref.base=NR_STACK_POINTER_REG) then
  797. internalerror(2010120511);
  798. if assigned(ref.symbol) then
  799. internalerror(2010120512);
  800. { stack: ... -> ..., arrayref, index }
  801. { load array base address }
  802. a_load_reg_stack(list,voidpointertype,ref.base);
  803. { index can either be in a register, or located in a simple memory
  804. location (since we have to load it anyway) }
  805. case ref.arrayreftype of
  806. art_indexreg:
  807. begin
  808. if ref.index=NR_NO then
  809. internalerror(2010120513);
  810. { all array indices in Java are 32 bit ints }
  811. a_load_reg_stack(list,s32inttype,ref.index);
  812. end;
  813. art_indexref:
  814. begin
  815. reference_reset_base(href,ref.indexbase,ref.indexoffset,4);
  816. href.symbol:=ref.indexsymbol;
  817. a_load_ref_stack(list,s32inttype,href,prepare_stack_for_ref(list,href,false));
  818. end;
  819. art_indexconst:
  820. begin
  821. a_load_const_stack(list,s32inttype,ref.indexoffset,R_INTREGISTER);
  822. end;
  823. else
  824. internalerror(2011012001);
  825. end;
  826. { adjustment of the index }
  827. if ref.offset<>0 then
  828. a_op_const_stack(list,OP_ADD,s32inttype,ref.offset);
  829. if dup then
  830. begin
  831. list.concat(taicpu.op_none(a_dup2));
  832. incstack(list,2);
  833. end;
  834. result:=2;
  835. end;
  836. end;
  837. procedure thlcgjvm.a_load_const_reg(list: TAsmList; tosize: tdef; a: aint; register: tregister);
  838. begin
  839. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  840. a_load_stack_reg(list,tosize,register);
  841. end;
  842. procedure thlcgjvm.a_load_const_ref(list: TAsmList; tosize: tdef; a: aint; const ref: treference);
  843. var
  844. extra_slots: longint;
  845. begin
  846. extra_slots:=prepare_stack_for_ref(list,ref,false);
  847. a_load_const_stack(list,tosize,a,def2regtyp(tosize));
  848. a_load_stack_ref(list,tosize,ref,extra_slots);
  849. end;
  850. procedure thlcgjvm.a_load_reg_ref(list: TAsmList; fromsize, tosize: tdef; register: tregister; const ref: treference);
  851. var
  852. extra_slots: longint;
  853. begin
  854. extra_slots:=prepare_stack_for_ref(list,ref,false);
  855. a_load_reg_stack(list,fromsize,register);
  856. a_load_stack_ref(list,tosize,ref,extra_slots);
  857. end;
  858. procedure thlcgjvm.a_load_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  859. begin
  860. a_load_reg_stack(list,fromsize,reg1);
  861. if def2regtyp(fromsize)=R_INTREGISTER then
  862. resize_stack_int_val(list,def_cgsize(fromsize),def_cgsize(tosize),false);
  863. a_load_stack_reg(list,tosize,reg2);
  864. end;
  865. procedure thlcgjvm.a_load_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; register: tregister);
  866. var
  867. extra_slots: longint;
  868. begin
  869. extra_slots:=prepare_stack_for_ref(list,ref,false);
  870. a_load_ref_stack(list,fromsize,ref,extra_slots);
  871. if def2regtyp(fromsize)=R_INTREGISTER then
  872. resize_stack_int_val(list,def_cgsize(fromsize),def_cgsize(tosize),false);
  873. a_load_stack_reg(list,tosize,register);
  874. end;
  875. procedure thlcgjvm.a_load_ref_ref(list: TAsmList; fromsize, tosize: tdef; const sref: treference; const dref: treference);
  876. var
  877. extra_sslots,
  878. extra_dslots: longint;
  879. begin
  880. { make sure the destination reference is on top, since in the end the
  881. order has to be "destref, value" -> first create "destref, sourceref" }
  882. extra_dslots:=prepare_stack_for_ref(list,dref,false);
  883. extra_sslots:=prepare_stack_for_ref(list,sref,false);
  884. a_load_ref_stack(list,fromsize,sref,extra_sslots);
  885. if def2regtyp(fromsize)=R_INTREGISTER then
  886. resize_stack_int_val(list,def_cgsize(fromsize),def_cgsize(tosize),dref.arrayreftype<>art_none);
  887. a_load_stack_ref(list,tosize,dref,extra_dslots);
  888. end;
  889. procedure thlcgjvm.a_loadaddr_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; r: tregister);
  890. begin
  891. { only allowed for types that are not implicit pointers in Pascal (in
  892. that case, ref contains a pointer to the actual data and we simply
  893. return that pointer) }
  894. if not jvmimplicitpointertype(fromsize) then
  895. internalerror(2010120534);
  896. a_load_ref_reg(list,java_jlobject,java_jlobject,ref,r);
  897. end;
  898. procedure thlcgjvm.a_op_const_reg(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; reg: TRegister);
  899. begin
  900. a_op_const_reg_reg(list,op,size,a,reg,reg);
  901. end;
  902. procedure thlcgjvm.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tdef; a: aint; src, dst: tregister);
  903. begin
  904. a_load_reg_stack(list,size,src);
  905. a_op_const_stack(list,op,size,a);
  906. a_load_stack_reg(list,size,dst);
  907. end;
  908. procedure thlcgjvm.a_op_const_ref(list: TAsmList; Op: TOpCG; size: tdef; a: Aint; const ref: TReference);
  909. var
  910. extra_slots: longint;
  911. begin
  912. extra_slots:=prepare_stack_for_ref(list,ref,true);
  913. { TODO, here or in peepholeopt: use iinc when possible }
  914. a_load_ref_stack(list,size,ref,extra_slots);
  915. a_op_const_stack(list,op,size,a);
  916. a_load_stack_ref(list,size,ref,extra_slots);
  917. end;
  918. procedure thlcgjvm.a_op_ref_reg(list: TAsmList; Op: TOpCG; size: tdef; const ref: TReference; reg: TRegister);
  919. begin
  920. a_load_reg_stack(list,size,reg);
  921. a_op_ref_stack(list,op,size,ref);
  922. a_load_stack_reg(list,size,reg);
  923. end;
  924. procedure thlcgjvm.a_op_reg_reg_reg(list: TAsmList; op: TOpCg; size: tdef; src1, src2, dst: tregister);
  925. begin
  926. a_load_reg_stack(list,size,src2);
  927. a_op_reg_stack(list,op,size,src1);
  928. a_load_stack_reg(list,size,dst);
  929. end;
  930. procedure thlcgjvm.a_op_reg_reg(list: TAsmList; Op: TOpCG; size: tdef; reg1, reg2: TRegister);
  931. begin
  932. a_op_reg_reg_reg(list,op,size,reg1,reg2,reg2);
  933. end;
  934. procedure thlcgjvm.a_cmp_const_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; const ref: treference; l: tasmlabel);
  935. begin
  936. if ref.base<>NR_EVAL_STACK_BASE then
  937. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  938. maybe_adjust_cmp_stackval(list,size,cmp_op);
  939. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  940. a_cmp_stack_label(list,size,cmp_op,l);
  941. end;
  942. procedure thlcgjvm.a_cmp_const_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; a: aint; reg: tregister; l: tasmlabel);
  943. begin
  944. a_load_reg_stack(list,size,reg);
  945. maybe_adjust_cmp_stackval(list,size,cmp_op);
  946. a_load_const_stack(list,size,maybe_adjust_cmp_constval(size,cmp_op,a),def2regtyp(size));
  947. a_cmp_stack_label(list,size,cmp_op,l);
  948. end;
  949. procedure thlcgjvm.a_cmp_ref_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; const ref: treference; reg: tregister; l: tasmlabel);
  950. begin
  951. a_load_reg_stack(list,size,reg);
  952. maybe_adjust_cmp_stackval(list,size,cmp_op);
  953. if ref.base<>NR_EVAL_STACK_BASE then
  954. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false))
  955. else
  956. list.concat(taicpu.op_none(a_swap));
  957. maybe_adjust_cmp_stackval(list,size,cmp_op);
  958. a_cmp_stack_label(list,size,cmp_op,l);
  959. end;
  960. procedure thlcgjvm.a_cmp_reg_ref_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg: tregister; const ref: treference; l: tasmlabel);
  961. begin
  962. if ref.base<>NR_EVAL_STACK_BASE then
  963. a_load_ref_stack(list,size,ref,prepare_stack_for_ref(list,ref,false));
  964. maybe_adjust_cmp_stackval(list,size,cmp_op);
  965. a_load_reg_stack(list,size,reg);
  966. maybe_adjust_cmp_stackval(list,size,cmp_op);
  967. a_cmp_stack_label(list,size,cmp_op,l);
  968. end;
  969. procedure thlcgjvm.a_cmp_reg_reg_label(list: TAsmList; size: tdef; cmp_op: topcmp; reg1, reg2: tregister; l: tasmlabel);
  970. begin
  971. a_load_reg_stack(list,size,reg2);
  972. maybe_adjust_cmp_stackval(list,size,cmp_op);
  973. a_load_reg_stack(list,size,reg1);
  974. maybe_adjust_cmp_stackval(list,size,cmp_op);
  975. a_cmp_stack_label(list,size,cmp_op,l);
  976. end;
  977. procedure thlcgjvm.a_jmp_always(list: TAsmList; l: tasmlabel);
  978. begin
  979. list.concat(taicpu.op_sym(a_goto,current_asmdata.RefAsmSymbol(l.name)));
  980. end;
  981. procedure thlcgjvm.concatcopy_normal_array(list: TAsmList; size: tdef; const source, dest: treference);
  982. var
  983. procname: string;
  984. eledef: tdef;
  985. ndim: longint;
  986. adddefaultlenparas: boolean;
  987. begin
  988. { load copy helper parameters on the stack }
  989. a_load_ref_stack(list,java_jlobject,source,prepare_stack_for_ref(list,source,false));
  990. a_load_ref_stack(list,java_jlobject,dest,prepare_stack_for_ref(list,dest,false));
  991. { call copy helper }
  992. eledef:=tarraydef(size).elementdef;
  993. ndim:=1;
  994. adddefaultlenparas:=true;
  995. case eledef.typ of
  996. orddef:
  997. begin
  998. case torddef(eledef).ordtype of
  999. pasbool8,s8bit,u8bit,bool8bit,uchar,
  1000. s16bit,u16bit,bool16bit,pasbool16,
  1001. uwidechar,
  1002. s32bit,u32bit,bool32bit,pasbool32,
  1003. s64bit,u64bit,bool64bit,pasbool64,scurrency:
  1004. procname:='FPC_COPY_SHALLOW_ARRAY'
  1005. else
  1006. internalerror(2011020504);
  1007. end;
  1008. end;
  1009. arraydef:
  1010. begin
  1011. { call fpc_setlength_dynarr_multidim with deepcopy=true, and extra
  1012. parameters }
  1013. while (eledef.typ=arraydef) and
  1014. not is_dynamic_array(eledef) do
  1015. begin
  1016. eledef:=tarraydef(eledef).elementdef;
  1017. inc(ndim)
  1018. end;
  1019. if (ndim=1) then
  1020. procname:='FPC_COPY_SHALLOW_ARRAY'
  1021. else
  1022. begin
  1023. { deepcopy=true }
  1024. a_load_const_stack(list,pasbool8type,1,R_INTREGISTER);
  1025. { ndim }
  1026. a_load_const_stack(list,s32inttype,ndim,R_INTREGISTER);
  1027. { eletype }
  1028. a_load_const_stack(list,cwidechartype,ord(jvmarrtype_setlength(eledef)),R_INTREGISTER);
  1029. adddefaultlenparas:=false;
  1030. procname:='FPC_SETLENGTH_DYNARR_MULTIDIM';
  1031. end;
  1032. end;
  1033. recorddef:
  1034. procname:='FPC_COPY_JRECORD_ARRAY';
  1035. floatdef:
  1036. procname:='FPC_COPY_SHALLOW_ARRAY';
  1037. stringdef:
  1038. if is_shortstring(eledef) then
  1039. procname:='FPC_COPY_JSHORTSTRING_ARRAY'
  1040. else
  1041. procname:='FPC_COPY_SHALLOW_ARRAY';
  1042. setdef,
  1043. variantdef:
  1044. begin
  1045. {$ifndef nounsupported}
  1046. procname:='FPC_COPY_SHALLOW_ARRAY';
  1047. {$else}
  1048. { todo: make a deep copy via clone... }
  1049. internalerror(2011020505);
  1050. {$endif}
  1051. end;
  1052. else
  1053. procname:='FPC_COPY_SHALLOW_ARRAY';
  1054. end;
  1055. if adddefaultlenparas then
  1056. begin
  1057. { -1, -1 means "copy entire array" }
  1058. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  1059. a_load_const_stack(list,s32inttype,-1,R_INTREGISTER);
  1060. end;
  1061. g_call_system_proc(list,procname);
  1062. if ndim=1 then
  1063. begin
  1064. decstack(list,2);
  1065. if adddefaultlenparas then
  1066. decstack(list,2);
  1067. end
  1068. else
  1069. begin
  1070. decstack(list,4);
  1071. { pop return value, must be the same as dest }
  1072. list.concat(taicpu.op_none(a_pop));
  1073. decstack(list,1);
  1074. end;
  1075. end;
  1076. procedure thlcgjvm.concatcopy_record(list: TAsmList; size: tdef; const source, dest: treference);
  1077. var
  1078. srsym: tsym;
  1079. pd: tprocdef;
  1080. begin
  1081. { self }
  1082. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1083. { result }
  1084. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1085. { call fpcDeepCopy helper }
  1086. srsym:=search_struct_member(tabstractrecorddef(size),'FPCDEEPCOPY');
  1087. if not assigned(srsym) or
  1088. (srsym.typ<>procsym) then
  1089. Message1(cg_f_unknown_compilerproc,'FpcRecordBaseType.fpcDeepCopy');
  1090. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1091. a_call_name(list,pd,pd.mangledname,false);
  1092. { both parameters are removed, no function result }
  1093. decstack(list,2);
  1094. end;
  1095. procedure thlcgjvm.concatcopy_shortstring(list: TAsmList; size: tdef; const source, dest: treference);
  1096. var
  1097. srsym: tsym;
  1098. pd: tprocdef;
  1099. begin
  1100. { self }
  1101. a_load_ref_stack(list,size,source,prepare_stack_for_ref(list,source,false));
  1102. { result }
  1103. a_load_ref_stack(list,size,dest,prepare_stack_for_ref(list,dest,false));
  1104. { call fpcDeepCopy helper }
  1105. srsym:=search_struct_member(java_shortstring,'FPCDEEPCOPY');
  1106. if not assigned(srsym) or
  1107. (srsym.typ<>procsym) then
  1108. Message1(cg_f_unknown_compilerproc,'ShortstringClass.FpcDeepCopy');
  1109. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1110. a_call_name(list,pd,pd.mangledname,false);
  1111. { both parameters are removed, no function result }
  1112. decstack(list,2);
  1113. end;
  1114. procedure thlcgjvm.g_concatcopy(list: TAsmList; size: tdef; const source, dest: treference);
  1115. var
  1116. handled: boolean;
  1117. begin
  1118. handled:=false;
  1119. case size.typ of
  1120. arraydef:
  1121. begin
  1122. if not is_dynamic_array(size) then
  1123. begin
  1124. concatcopy_normal_array(list,size,source,dest);
  1125. handled:=true;
  1126. end;
  1127. end;
  1128. recorddef:
  1129. begin
  1130. concatcopy_record(list,size,source,dest);
  1131. handled:=true;
  1132. end;
  1133. stringdef:
  1134. begin
  1135. if is_shortstring(size) then
  1136. begin
  1137. concatcopy_shortstring(list,size,source,dest);
  1138. handled:=true;
  1139. end;
  1140. end;
  1141. end;
  1142. if not handled then
  1143. inherited;
  1144. end;
  1145. procedure thlcgjvm.g_copyshortstring(list: TAsmList; const source, dest: treference; strdef: tstringdef);
  1146. begin
  1147. concatcopy_shortstring(list,strdef,source,dest);
  1148. end;
  1149. procedure thlcgjvm.a_loadfpu_ref_ref(list: TAsmList; fromsize, tosize: tdef; const ref1, ref2: treference);
  1150. var
  1151. dstack_slots: longint;
  1152. begin
  1153. dstack_slots:=prepare_stack_for_ref(list,ref2,false);
  1154. a_load_ref_stack(list,fromsize,ref1,prepare_stack_for_ref(list,ref1,false));
  1155. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1156. a_load_stack_ref(list,tosize,ref2,dstack_slots);
  1157. end;
  1158. procedure thlcgjvm.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tdef; const ref: treference; reg: tregister);
  1159. begin
  1160. a_load_ref_stack(list,fromsize,ref,prepare_stack_for_ref(list,ref,false));
  1161. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1162. a_load_stack_reg(list,tosize,reg);
  1163. end;
  1164. procedure thlcgjvm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tdef; reg: tregister; const ref: treference);
  1165. var
  1166. dstack_slots: longint;
  1167. begin
  1168. dstack_slots:=prepare_stack_for_ref(list,ref,false);
  1169. a_load_reg_stack(list,fromsize,reg);
  1170. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1171. a_load_stack_ref(list,tosize,ref,dstack_slots);
  1172. end;
  1173. procedure thlcgjvm.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tdef; reg1, reg2: tregister);
  1174. begin
  1175. a_load_reg_stack(list,fromsize,reg1);
  1176. resizestackfpuval(list,def_cgsize(fromsize),def_cgsize(tosize));
  1177. a_load_stack_reg(list,tosize,reg2);
  1178. end;
  1179. procedure thlcgjvm.g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);
  1180. begin
  1181. { the localsize is based on tg.lasttemp -> already in terms of stack
  1182. slots rather than bytes }
  1183. list.concat(tai_directive.Create(asd_jlimit,'locals '+tostr(localsize)));
  1184. { we insert the unit initialisation code afterwards in the proginit code,
  1185. and it uses one stack slot }
  1186. if (current_procinfo.procdef.proctypeoption=potype_proginit) then
  1187. fmaxevalstackheight:=max(1,fmaxevalstackheight);
  1188. list.concat(tai_directive.Create(asd_jlimit,'stack '+tostr(fmaxevalstackheight)));
  1189. end;
  1190. procedure thlcgjvm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  1191. var
  1192. retdef: tdef;
  1193. opc: tasmop;
  1194. begin
  1195. if current_procinfo.procdef.proctypeoption in [potype_constructor,potype_class_constructor] then
  1196. retdef:=voidtype
  1197. else
  1198. retdef:=current_procinfo.procdef.returndef;
  1199. case retdef.typ of
  1200. orddef:
  1201. case torddef(retdef).ordtype of
  1202. uvoid:
  1203. opc:=a_return;
  1204. s64bit,
  1205. u64bit,
  1206. scurrency:
  1207. opc:=a_lreturn;
  1208. else
  1209. opc:=a_ireturn;
  1210. end;
  1211. setdef:
  1212. if is_smallset(retdef) then
  1213. opc:=a_ireturn
  1214. else
  1215. opc:=a_areturn;
  1216. floatdef:
  1217. case tfloatdef(retdef).floattype of
  1218. s32real:
  1219. opc:=a_freturn;
  1220. s64real:
  1221. opc:=a_dreturn;
  1222. else
  1223. internalerror(2011010213);
  1224. end;
  1225. else
  1226. opc:=a_areturn;
  1227. end;
  1228. list.concat(taicpu.op_none(opc));
  1229. end;
  1230. procedure thlcgjvm.gen_load_return_value(list: TAsmList);
  1231. begin
  1232. { constructors don't return anything in the jvm }
  1233. if current_procinfo.procdef.proctypeoption in [potype_constructor,potype_class_constructor] then
  1234. exit;
  1235. inherited gen_load_return_value(list);
  1236. end;
  1237. procedure thlcgjvm.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  1238. begin
  1239. { add something to the al_procedures list as well, because if all al_*
  1240. lists are empty, the assembler writer isn't called }
  1241. if not code.empty and
  1242. current_asmdata.asmlists[al_procedures].empty then
  1243. current_asmdata.asmlists[al_procedures].concat(tai_align.Create(4));
  1244. pd.exprasmlist:=TAsmList.create;
  1245. pd.exprasmlist.concatlist(code);
  1246. if assigned(data) and
  1247. not data.empty then
  1248. internalerror(2010122801);
  1249. end;
  1250. procedure thlcgjvm.g_incrrefcount(list: TAsmList; t: tdef; const ref: treference);
  1251. begin
  1252. // do nothing
  1253. end;
  1254. procedure thlcgjvm.g_decrrefcount(list: TAsmList; t: tdef; const ref: treference);
  1255. begin
  1256. // do nothing
  1257. end;
  1258. procedure thlcgjvm.g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation; const name: string);
  1259. var
  1260. normaldim: longint;
  1261. recref: treference;
  1262. begin
  1263. { only in case of initialisation, we have to set all elements to "empty" }
  1264. if name<>'FPC_INITIALIZE_ARRAY' then
  1265. exit;
  1266. { put array on the stack }
  1267. a_load_ref_stack(list,java_jlobject,ref,prepare_stack_for_ref(list,ref,false));
  1268. { in case it's an open array whose elements are regular arrays, put the
  1269. dimension of the regular arrays on the stack (otherwise pass 0) }
  1270. normaldim:=0;
  1271. while (t.typ=arraydef) and
  1272. not is_dynamic_array(t) do
  1273. begin
  1274. inc(normaldim);
  1275. t:=tarraydef(t).elementdef;
  1276. end;
  1277. a_load_const_stack(list,s32inttype,normaldim,R_INTREGISTER);
  1278. { highloc is invalid, the length is part of the array in Java }
  1279. if is_wide_or_unicode_string(t) then
  1280. g_call_system_proc(list,'fpc_initialize_array_unicodestring')
  1281. else if is_ansistring(t) then
  1282. g_call_system_proc(list,'fpc_initialize_array_ansistring')
  1283. else if is_dynamic_array(t) then
  1284. g_call_system_proc(list,'fpc_initialize_array_dynarr')
  1285. else if is_record(t) then
  1286. begin
  1287. tg.gethltemp(list,t,t.size,tt_persistent,recref);
  1288. a_load_ref_stack(list,t,recref,prepare_stack_for_ref(list,recref,false));
  1289. g_call_system_proc(list,'fpc_initialize_array_record');
  1290. tg.ungettemp(list,recref);
  1291. end
  1292. else
  1293. internalerror(2011031901);
  1294. end;
  1295. procedure thlcgjvm.g_initialize(list: TAsmList; t: tdef; const ref: treference);
  1296. var
  1297. dummyloc: tlocation;
  1298. recref: treference;
  1299. begin
  1300. if (t.typ=arraydef) and
  1301. not is_dynamic_array(t) then
  1302. begin
  1303. dummyloc.loc:=LOC_INVALID;
  1304. g_array_rtti_helper(list,tarraydef(t).elementdef,ref,dummyloc,'FPC_INITIALIZE_ARRAY')
  1305. end
  1306. else if is_record(t) then
  1307. begin
  1308. { create a new, empty record and replace the contents of the old one
  1309. with those of the new one (in the future we can generate a dedicate
  1310. initialization helper) }
  1311. tg.gethltemp(list,t,t.size,tt_persistent,recref);
  1312. g_concatcopy(list,t,recref,ref);
  1313. tg.ungettemp(list,recref);
  1314. end
  1315. else
  1316. a_load_const_ref(list,t,0,ref);
  1317. end;
  1318. procedure thlcgjvm.g_finalize(list: TAsmList; t: tdef; const ref: treference);
  1319. begin
  1320. // do nothing
  1321. end;
  1322. procedure thlcgjvm.location_get_data_ref(list: TAsmList; def: tdef; const l: tlocation; var ref: treference; loadref: boolean; alignment: longint);
  1323. var
  1324. tmploc: tlocation;
  1325. begin
  1326. { This routine is a combination of a generalised a_loadaddr_ref_reg()
  1327. that also works for addresses in registers (in case loadref is false)
  1328. and of a_load_ref_reg (in case loadref is true). It is used for
  1329. a) getting the address of managed var/out parameters
  1330. b) getting to the actual data of value types that are passed by
  1331. reference by the compiler (and then get a local copy at the caller
  1332. side). Normally, depending on whether this reference is passed in a
  1333. register or reference, we either need a reference with that register
  1334. as base or load the address in that reference and use that as a new
  1335. base.
  1336. Since the JVM cannot take the address of anything, all
  1337. "pass-by-reference" value parameters (which are always aggregate types)
  1338. are already simply the implicit pointer to the data (since arrays,
  1339. records, etc are already internally implicit pointers). This means
  1340. that if "loadref" is true, we must simply return this implicit pointer.
  1341. If it is false, we are supposed the take the address of this implicit
  1342. pointer, which is not possible.
  1343. However, managed types are also implicit pointers in Pascal, so in that
  1344. case "taking the address" again consists of simply returning the
  1345. implicit pointer/current value (in case of a var/out parameter, this
  1346. value is stored inside an array).
  1347. }
  1348. if not loadref then
  1349. begin
  1350. if not is_managed_type(def) then
  1351. internalerror(2011020601);
  1352. tmploc:=l;
  1353. end
  1354. else
  1355. begin
  1356. if not jvmimplicitpointertype(def) then
  1357. begin
  1358. { passed by reference in array of single element; l contains the
  1359. base address of the array }
  1360. location_reset_ref(tmploc,LOC_REFERENCE,OS_ADDR,4);
  1361. reference_reset_base(tmploc.reference,getaddressregister(list,java_jlobject),0,4);
  1362. tmploc.reference.arrayreftype:=art_indexconst;
  1363. tmploc.reference.indexoffset:=0;
  1364. a_load_loc_reg(list,java_jlobject,java_jlobject,l,tmploc.reference.base);
  1365. end
  1366. else
  1367. tmploc:=l;
  1368. end;
  1369. case tmploc.loc of
  1370. LOC_REGISTER,
  1371. LOC_CREGISTER :
  1372. begin
  1373. { the implicit pointer is in a register and has to be in a
  1374. reference -> create a reference and put it there }
  1375. location_force_mem(list,tmploc,java_jlobject);
  1376. ref:=tmploc.reference;
  1377. end;
  1378. LOC_REFERENCE,
  1379. LOC_CREFERENCE :
  1380. begin
  1381. ref:=tmploc.reference;
  1382. end;
  1383. else
  1384. internalerror(2011020603);
  1385. end;
  1386. end;
  1387. procedure thlcgjvm.g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister);
  1388. var
  1389. localref: treference;
  1390. arrloc: tlocation;
  1391. stackslots: longint;
  1392. begin
  1393. { temporary reference for passing to concatcopy }
  1394. tg.gethltemp(list,java_jlobject,java_jlobject.size,tt_persistent,localref);
  1395. stackslots:=prepare_stack_for_ref(list,localref,false);
  1396. { create the local copy of the array (lenloc is invalid, get length
  1397. directly from the array) }
  1398. location_reset_ref(arrloc,LOC_REFERENCE,OS_ADDR,sizeof(pint));
  1399. arrloc.reference:=ref;
  1400. g_getarraylen(list,arrloc);
  1401. g_newarray(list,arrdef,1);
  1402. a_load_stack_ref(list,java_jlobject,localref,stackslots);
  1403. { copy the source array to the destination }
  1404. g_concatcopy(list,arrdef,ref,localref);
  1405. { and put the array pointer in the register as expected by the caller }
  1406. a_load_ref_reg(list,java_jlobject,java_jlobject,localref,destreg);
  1407. end;
  1408. procedure thlcgjvm.g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation);
  1409. begin
  1410. // do nothing, long live garbage collection!
  1411. end;
  1412. procedure thlcgjvm.gen_initialize_code(list: TAsmList);
  1413. var
  1414. ref: treference;
  1415. begin
  1416. { create globals with wrapped types such as arrays/records }
  1417. case current_procinfo.procdef.proctypeoption of
  1418. potype_unitinit:
  1419. begin
  1420. reference_reset_base(ref,NR_NO,0,1);
  1421. if assigned(current_module.globalsymtable) then
  1422. allocate_implicit_structs_for_st_with_base_ref(list,current_module.globalsymtable,ref,staticvarsym);
  1423. allocate_implicit_structs_for_st_with_base_ref(list,current_module.localsymtable,ref,staticvarsym);
  1424. end;
  1425. potype_class_constructor:
  1426. begin
  1427. { also initialise local variables, if any }
  1428. inherited;
  1429. { initialise class fields }
  1430. reference_reset_base(ref,NR_NO,0,1);
  1431. allocate_implicit_structs_for_st_with_base_ref(list,tabstractrecorddef(current_procinfo.procdef.owner.defowner).symtable,ref,staticvarsym);
  1432. end
  1433. else
  1434. inherited
  1435. end;
  1436. end;
  1437. procedure thlcgjvm.gen_entry_code(list: TAsmList);
  1438. begin
  1439. list.concat(Tai_force_line.Create);
  1440. end;
  1441. procedure thlcgjvm.gen_exit_code(list: TAsmList);
  1442. begin
  1443. { nothing }
  1444. end;
  1445. procedure thlcgjvm.a_load_stack_reg(list: TAsmList; size: tdef; reg: tregister);
  1446. var
  1447. opc: tasmop;
  1448. finishandval: aint;
  1449. begin
  1450. opc:=loadstoreopc(size,false,false,finishandval);
  1451. list.concat(taicpu.op_reg(opc,reg));
  1452. { avoid problems with getting the size of an open array etc }
  1453. if jvmimplicitpointertype(size) then
  1454. size:=java_jlobject;
  1455. decstack(list,1+ord(size.size>4));
  1456. end;
  1457. procedure thlcgjvm.a_load_stack_ref(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1458. var
  1459. opc: tasmop;
  1460. finishandval: aint;
  1461. begin
  1462. { fake location that indicates the value has to remain on the stack }
  1463. if ref.base=NR_EVAL_STACK_BASE then
  1464. exit;
  1465. opc:=loadstoreopcref(size,false,ref,finishandval);
  1466. if ref.arrayreftype=art_none then
  1467. list.concat(taicpu.op_ref(opc,ref))
  1468. else
  1469. list.concat(taicpu.op_none(opc));
  1470. { avoid problems with getting the size of an open array etc }
  1471. if jvmimplicitpointertype(size) then
  1472. size:=java_jlobject;
  1473. decstack(list,1+ord(size.size>4)+extra_slots);
  1474. end;
  1475. procedure thlcgjvm.a_load_reg_stack(list: TAsmList; size: tdef; reg: tregister);
  1476. var
  1477. opc: tasmop;
  1478. finishandval: aint;
  1479. begin
  1480. opc:=loadstoreopc(size,true,false,finishandval);
  1481. list.concat(taicpu.op_reg(opc,reg));
  1482. { avoid problems with getting the size of an open array etc }
  1483. if jvmimplicitpointertype(size) then
  1484. size:=java_jlobject;
  1485. incstack(list,1+ord(size.size>4));
  1486. if finishandval<>-1 then
  1487. a_op_const_stack(list,OP_AND,size,finishandval);
  1488. end;
  1489. procedure thlcgjvm.a_load_ref_stack(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1490. var
  1491. opc: tasmop;
  1492. finishandval: aint;
  1493. begin
  1494. { fake location that indicates the value is already on the stack? }
  1495. if (ref.base=NR_EVAL_STACK_BASE) then
  1496. exit;
  1497. opc:=loadstoreopcref(size,true,ref,finishandval);
  1498. if ref.arrayreftype=art_none then
  1499. list.concat(taicpu.op_ref(opc,ref))
  1500. else
  1501. list.concat(taicpu.op_none(opc));
  1502. { avoid problems with getting the size of an open array etc }
  1503. if jvmimplicitpointertype(size) then
  1504. size:=java_jlobject;
  1505. incstack(list,1+ord(size.size>4)-extra_slots);
  1506. if finishandval<>-1 then
  1507. a_op_const_stack(list,OP_AND,size,finishandval);
  1508. end;
  1509. function thlcgjvm.loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: aint): tasmop;
  1510. const
  1511. { isload static }
  1512. getputopc: array[boolean,boolean] of tasmop =
  1513. ((a_putfield,a_putstatic),
  1514. (a_getfield,a_getstatic));
  1515. begin
  1516. if assigned(ref.symbol) then
  1517. begin
  1518. { -> either a global (static) field, or a regular field. If a regular
  1519. field, then ref.base contains the self pointer, otherwise
  1520. ref.base=NR_NO. In both cases, the symbol contains all other
  1521. information (combined field name and type descriptor) }
  1522. result:=getputopc[isload,ref.base=NR_NO];
  1523. finishandval:=-1;
  1524. { erase sign extension for byte/smallint loads }
  1525. if (def2regtyp(def)=R_INTREGISTER) and
  1526. not is_signed(def) then
  1527. case def.size of
  1528. 1: finishandval:=255;
  1529. 2: finishandval:=65535;
  1530. end;
  1531. end
  1532. else
  1533. result:=loadstoreopc(def,isload,ref.arrayreftype<>art_none,finishandval);
  1534. end;
  1535. function thlcgjvm.loadstoreopc(def: tdef; isload, isarray: boolean; out finishandval: aint): tasmop;
  1536. var
  1537. size: longint;
  1538. begin
  1539. finishandval:=-1;
  1540. case def2regtyp(def) of
  1541. R_INTREGISTER:
  1542. begin
  1543. size:=def.size;
  1544. if not isarray then
  1545. begin
  1546. case size of
  1547. 1,2,3,4:
  1548. if isload then
  1549. result:=a_iload
  1550. else
  1551. result:=a_istore;
  1552. 8:
  1553. if isload then
  1554. result:=a_lload
  1555. else
  1556. result:=a_lstore;
  1557. else
  1558. internalerror(2011032814);
  1559. end;
  1560. end
  1561. { array }
  1562. else if isload then
  1563. begin
  1564. case size of
  1565. 1:
  1566. begin
  1567. result:=a_baload;
  1568. if not is_signed(def) then
  1569. finishandval:=255;
  1570. end;
  1571. 2:
  1572. begin
  1573. if is_widechar(def) then
  1574. result:=a_caload
  1575. else
  1576. begin
  1577. result:=a_saload;
  1578. { if we'd treat arrays of word as "array of widechar" we
  1579. could use a_caload, but that would make for even more
  1580. awkward interfacing with external Java code }
  1581. if not is_signed(def) then
  1582. finishandval:=65535;
  1583. end;
  1584. end;
  1585. 4: result:=a_iaload;
  1586. 8: result:=a_laload;
  1587. else
  1588. internalerror(2010120503);
  1589. end
  1590. end
  1591. else
  1592. begin
  1593. case size of
  1594. 1: result:=a_bastore;
  1595. 2: if not is_widechar(def) then
  1596. result:=a_sastore
  1597. else
  1598. result:=a_castore;
  1599. 4: result:=a_iastore;
  1600. 8: result:=a_lastore;
  1601. else
  1602. internalerror(2010120508);
  1603. end
  1604. end
  1605. end;
  1606. R_ADDRESSREGISTER:
  1607. if not isarray then
  1608. if isload then
  1609. result:=a_aload
  1610. else
  1611. result:=a_astore
  1612. else if isload then
  1613. result:=a_aaload
  1614. else
  1615. result:=a_aastore;
  1616. R_FPUREGISTER:
  1617. begin
  1618. case tfloatdef(def).floattype of
  1619. s32real:
  1620. if not isarray then
  1621. if isload then
  1622. result:=a_fload
  1623. else
  1624. result:=a_fstore
  1625. else if isload then
  1626. result:=a_faload
  1627. else
  1628. result:=a_fastore;
  1629. s64real:
  1630. if not isarray then
  1631. if isload then
  1632. result:=a_dload
  1633. else
  1634. result:=a_dstore
  1635. else if isload then
  1636. result:=a_daload
  1637. else
  1638. result:=a_dastore;
  1639. else
  1640. internalerror(2010120504);
  1641. end
  1642. end
  1643. else
  1644. internalerror(2010120502);
  1645. end;
  1646. end;
  1647. procedure thlcgjvm.resize_stack_int_val(list: TAsmList; fromsize, tosize: tcgsize; forarraystore: boolean);
  1648. begin
  1649. if fromsize in [OS_S64,OS_64] then
  1650. begin
  1651. if not(tosize in [OS_S64,OS_64]) then
  1652. begin
  1653. { truncate }
  1654. list.concat(taicpu.op_none(a_l2i));
  1655. decstack(list,1);
  1656. end;
  1657. end
  1658. else if tosize in [OS_S64,OS_64] then
  1659. begin
  1660. { extend }
  1661. list.concat(taicpu.op_none(a_i2l));
  1662. incstack(list,1);
  1663. { if it was an unsigned 32 bit value, remove sign extension }
  1664. if fromsize=OS_32 then
  1665. a_op_const_stack(list,OP_AND,s64inttype,cardinal($ffffffff));
  1666. end;
  1667. { if the value is immediately stored to an array afterwards, the store
  1668. instruction will properly truncate the value; otherwise we may need
  1669. additional truncation, except for 64/32 bit conversions, which are
  1670. already handled above }
  1671. if not forarraystore and
  1672. (not(fromsize in [OS_S64,OS_64,OS_32,OS_S32]) or
  1673. not(tosize in [OS_S64,OS_64,OS_32,OS_S32])) and
  1674. (tcgsize2size[fromsize]>tcgsize2size[tosize]) or
  1675. ((tcgsize2size[fromsize]=tcgsize2size[tosize]) and
  1676. (fromsize<>tosize)) or
  1677. { needs to mask out the sign in the top 16 bits }
  1678. ((fromsize=OS_S8) and
  1679. (tosize=OS_16)) then
  1680. case tosize of
  1681. OS_8:
  1682. a_op_const_stack(list,OP_AND,s32inttype,255);
  1683. OS_S8:
  1684. list.concat(taicpu.op_none(a_i2b));
  1685. OS_16:
  1686. list.concat(taicpu.op_none(a_i2c));
  1687. OS_S16:
  1688. list.concat(taicpu.op_none(a_i2s));
  1689. end;
  1690. end;
  1691. procedure thlcgjvm.allocate_implicit_struct_with_base_ref(list: TAsmList; vs: tabstractvarsym; ref: treference);
  1692. var
  1693. tmpref: treference;
  1694. begin
  1695. ref.symbol:=current_asmdata.RefAsmSymbol(vs.mangledname);
  1696. tg.gethltemp(list,vs.vardef,vs.vardef.size,tt_persistent,tmpref);
  1697. { only copy the reference, not the actual data }
  1698. a_load_ref_ref(list,java_jlobject,java_jlobject,tmpref,ref);
  1699. { remains live since there's still a reference to the created
  1700. entity }
  1701. tg.ungettemp(list,tmpref);
  1702. end;
  1703. procedure thlcgjvm.allocate_implicit_structs_for_st_with_base_ref(list: TAsmList; st: tsymtable; const ref: treference; allocvartyp: tsymtyp);
  1704. var
  1705. vs: tabstractvarsym;
  1706. i: longint;
  1707. begin
  1708. for i:=0 to st.symlist.count-1 do
  1709. begin
  1710. if (tsym(st.symlist[i]).typ<>allocvartyp) then
  1711. continue;
  1712. vs:=tabstractvarsym(st.symlist[i]);
  1713. if sp_internal in vs.symoptions then
  1714. continue;
  1715. if not jvmimplicitpointertype(vs.vardef) then
  1716. continue;
  1717. allocate_implicit_struct_with_base_ref(list,vs,ref);
  1718. end;
  1719. end;
  1720. procedure thlcgjvm.gen_initialize_fields_code(list: TAsmList);
  1721. var
  1722. selfpara: tparavarsym;
  1723. selfreg: tregister;
  1724. ref: treference;
  1725. obj: tabstractrecorddef;
  1726. i: longint;
  1727. needinit: boolean;
  1728. begin
  1729. obj:=tabstractrecorddef(current_procinfo.procdef.owner.defowner);
  1730. { check whether there are any fields that need initialisation }
  1731. needinit:=false;
  1732. for i:=0 to obj.symtable.symlist.count-1 do
  1733. if (tsym(obj.symtable.symlist[i]).typ=fieldvarsym) and
  1734. jvmimplicitpointertype(tfieldvarsym(obj.symtable.symlist[i]).vardef) then
  1735. begin
  1736. needinit:=true;
  1737. break;
  1738. end;
  1739. if not needinit then
  1740. exit;
  1741. selfpara:=tparavarsym(current_procinfo.procdef.parast.find('self'));
  1742. if not assigned(selfpara) then
  1743. internalerror(2011033001);
  1744. selfreg:=getaddressregister(list,selfpara.vardef);
  1745. a_load_loc_reg(list,obj,obj,selfpara.localloc,selfreg);
  1746. reference_reset_base(ref,selfreg,0,1);
  1747. allocate_implicit_structs_for_st_with_base_ref(list,obj.symtable,ref,fieldvarsym);
  1748. end;
  1749. procedure thlcgjvm.resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  1750. begin
  1751. if (fromsize=OS_F32) and
  1752. (tosize=OS_F64) then
  1753. begin
  1754. list.concat(taicpu.op_none(a_f2d));
  1755. incstack(list,1);
  1756. end
  1757. else if (fromsize=OS_F64) and
  1758. (tosize=OS_F32) then
  1759. begin
  1760. list.concat(taicpu.op_none(a_d2f));
  1761. decstack(list,1);
  1762. end;
  1763. end;
  1764. procedure thlcgjvm.maybepreparedivu32(list: TAsmList; var op: topcg; size: tdef; out isdivu32: boolean);
  1765. begin
  1766. if (op=OP_DIV) and
  1767. (def_cgsize(size)=OS_32) then
  1768. begin
  1769. { needs zero-extension to 64 bit, because the JVM only supports
  1770. signed divisions }
  1771. resize_stack_int_val(list,OS_32,OS_S64,false);
  1772. op:=OP_IDIV;
  1773. isdivu32:=true;
  1774. end
  1775. else
  1776. isdivu32:=false;
  1777. end;
  1778. procedure thlcgjvm.a_call_name_intern(list: TAsmList; pd: tprocdef; const s: TSymStr; inheritedcall: boolean);
  1779. var
  1780. opc: tasmop;
  1781. begin
  1782. {
  1783. invoke types:
  1784. * invokeinterface: call method from an interface (must also specify
  1785. number of parameters in terms of stack slot count!)
  1786. * invokespecial: invoke a constructor, method in a superclass,
  1787. or private instance method
  1788. * invokestatic: invoke a class method (private or not)
  1789. * invokevirtual: invoke a regular method
  1790. }
  1791. case pd.owner.symtabletype of
  1792. globalsymtable,
  1793. staticsymtable,
  1794. localsymtable:
  1795. { regular and nested procedures are turned into static methods }
  1796. opc:=a_invokestatic;
  1797. objectsymtable:
  1798. begin
  1799. case tobjectdef(pd.owner.defowner).objecttype of
  1800. odt_javaclass:
  1801. begin
  1802. if (po_classmethod in pd.procoptions) then
  1803. opc:=a_invokestatic
  1804. else if (pd.visibility=vis_private) or
  1805. (pd.proctypeoption=potype_constructor) or
  1806. inheritedcall then
  1807. opc:=a_invokespecial
  1808. else
  1809. opc:=a_invokevirtual;
  1810. end;
  1811. odt_interfacejava:
  1812. { static interface methods are not allowed }
  1813. opc:=a_invokeinterface;
  1814. else
  1815. internalerror(2010122601);
  1816. end;
  1817. end;
  1818. recordsymtable:
  1819. begin
  1820. if (po_staticmethod in pd.procoptions) then
  1821. opc:=a_invokestatic
  1822. else if (pd.visibility=vis_private) or
  1823. (pd.proctypeoption=potype_constructor) or
  1824. inheritedcall then
  1825. opc:=a_invokespecial
  1826. else
  1827. opc:=a_invokevirtual;
  1828. end
  1829. else
  1830. internalerror(2010122602);
  1831. end;
  1832. if (opc<>a_invokeinterface) then
  1833. list.concat(taicpu.op_sym(opc,current_asmdata.RefAsmSymbol(s)))
  1834. else
  1835. begin
  1836. pd.init_paraloc_info(calleeside);
  1837. list.concat(taicpu.op_sym_const(opc,current_asmdata.RefAsmSymbol(s),pd.calleeargareasize));
  1838. end;
  1839. end;
  1840. procedure thlcgjvm.g_call_system_proc(list: TAsmList; const procname: string);
  1841. var
  1842. srsym: tsym;
  1843. pd: tprocdef;
  1844. begin
  1845. srsym:=tsym(systemunit.find(procname));
  1846. if not assigned(srsym) or
  1847. (srsym.typ<>procsym) then
  1848. Message1(cg_f_unknown_compilerproc,procname);
  1849. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1850. a_call_name(list,pd,pd.mangledname,false);
  1851. end;
  1852. procedure create_hlcodegen;
  1853. begin
  1854. hlcg:=thlcgjvm.create;
  1855. create_codegen;
  1856. end;
  1857. end.