hlcgcpu.pas 74 KB

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