hlcgcpu.pas 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006
  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. enumdef:
  1213. opc:=a_ireturn;
  1214. setdef:
  1215. if is_smallset(retdef) then
  1216. opc:=a_ireturn
  1217. else
  1218. opc:=a_areturn;
  1219. floatdef:
  1220. case tfloatdef(retdef).floattype of
  1221. s32real:
  1222. opc:=a_freturn;
  1223. s64real:
  1224. opc:=a_dreturn;
  1225. else
  1226. internalerror(2011010213);
  1227. end;
  1228. else
  1229. opc:=a_areturn;
  1230. end;
  1231. list.concat(taicpu.op_none(opc));
  1232. end;
  1233. procedure thlcgjvm.gen_load_return_value(list: TAsmList);
  1234. begin
  1235. { constructors don't return anything in the jvm }
  1236. if current_procinfo.procdef.proctypeoption in [potype_constructor,potype_class_constructor] then
  1237. exit;
  1238. inherited gen_load_return_value(list);
  1239. end;
  1240. procedure thlcgjvm.record_generated_code_for_procdef(pd: tprocdef; code, data: TAsmList);
  1241. begin
  1242. { add something to the al_procedures list as well, because if all al_*
  1243. lists are empty, the assembler writer isn't called }
  1244. if not code.empty and
  1245. current_asmdata.asmlists[al_procedures].empty then
  1246. current_asmdata.asmlists[al_procedures].concat(tai_align.Create(4));
  1247. pd.exprasmlist:=TAsmList.create;
  1248. pd.exprasmlist.concatlist(code);
  1249. if assigned(data) and
  1250. not data.empty then
  1251. internalerror(2010122801);
  1252. end;
  1253. procedure thlcgjvm.g_incrrefcount(list: TAsmList; t: tdef; const ref: treference);
  1254. begin
  1255. // do nothing
  1256. end;
  1257. procedure thlcgjvm.g_decrrefcount(list: TAsmList; t: tdef; const ref: treference);
  1258. begin
  1259. // do nothing
  1260. end;
  1261. procedure thlcgjvm.g_array_rtti_helper(list: TAsmList; t: tdef; const ref: treference; const highloc: tlocation; const name: string);
  1262. var
  1263. normaldim: longint;
  1264. recref: treference;
  1265. begin
  1266. { only in case of initialisation, we have to set all elements to "empty" }
  1267. if name<>'FPC_INITIALIZE_ARRAY' then
  1268. exit;
  1269. { put array on the stack }
  1270. a_load_ref_stack(list,java_jlobject,ref,prepare_stack_for_ref(list,ref,false));
  1271. { in case it's an open array whose elements are regular arrays, put the
  1272. dimension of the regular arrays on the stack (otherwise pass 0) }
  1273. normaldim:=0;
  1274. while (t.typ=arraydef) and
  1275. not is_dynamic_array(t) do
  1276. begin
  1277. inc(normaldim);
  1278. t:=tarraydef(t).elementdef;
  1279. end;
  1280. a_load_const_stack(list,s32inttype,normaldim,R_INTREGISTER);
  1281. { highloc is invalid, the length is part of the array in Java }
  1282. if is_wide_or_unicode_string(t) then
  1283. g_call_system_proc(list,'fpc_initialize_array_unicodestring')
  1284. else if is_ansistring(t) then
  1285. g_call_system_proc(list,'fpc_initialize_array_ansistring')
  1286. else if is_dynamic_array(t) then
  1287. g_call_system_proc(list,'fpc_initialize_array_dynarr')
  1288. else if is_record(t) then
  1289. begin
  1290. tg.gethltemp(list,t,t.size,tt_persistent,recref);
  1291. a_load_ref_stack(list,t,recref,prepare_stack_for_ref(list,recref,false));
  1292. g_call_system_proc(list,'fpc_initialize_array_record');
  1293. tg.ungettemp(list,recref);
  1294. end
  1295. else
  1296. internalerror(2011031901);
  1297. end;
  1298. procedure thlcgjvm.g_initialize(list: TAsmList; t: tdef; const ref: treference);
  1299. var
  1300. dummyloc: tlocation;
  1301. recref: treference;
  1302. begin
  1303. if (t.typ=arraydef) and
  1304. not is_dynamic_array(t) then
  1305. begin
  1306. dummyloc.loc:=LOC_INVALID;
  1307. g_array_rtti_helper(list,tarraydef(t).elementdef,ref,dummyloc,'FPC_INITIALIZE_ARRAY')
  1308. end
  1309. else if is_record(t) then
  1310. begin
  1311. { create a new, empty record and replace the contents of the old one
  1312. with those of the new one (in the future we can generate a dedicate
  1313. initialization helper) }
  1314. tg.gethltemp(list,t,t.size,tt_persistent,recref);
  1315. g_concatcopy(list,t,recref,ref);
  1316. tg.ungettemp(list,recref);
  1317. end
  1318. else
  1319. a_load_const_ref(list,t,0,ref);
  1320. end;
  1321. procedure thlcgjvm.g_finalize(list: TAsmList; t: tdef; const ref: treference);
  1322. begin
  1323. // do nothing
  1324. end;
  1325. procedure thlcgjvm.location_get_data_ref(list: TAsmList; def: tdef; const l: tlocation; var ref: treference; loadref: boolean; alignment: longint);
  1326. var
  1327. tmploc: tlocation;
  1328. begin
  1329. { This routine is a combination of a generalised a_loadaddr_ref_reg()
  1330. that also works for addresses in registers (in case loadref is false)
  1331. and of a_load_ref_reg (in case loadref is true). It is used for
  1332. a) getting the address of managed var/out parameters
  1333. b) getting to the actual data of value types that are passed by
  1334. reference by the compiler (and then get a local copy at the caller
  1335. side). Normally, depending on whether this reference is passed in a
  1336. register or reference, we either need a reference with that register
  1337. as base or load the address in that reference and use that as a new
  1338. base.
  1339. Since the JVM cannot take the address of anything, all
  1340. "pass-by-reference" value parameters (which are always aggregate types)
  1341. are already simply the implicit pointer to the data (since arrays,
  1342. records, etc are already internally implicit pointers). This means
  1343. that if "loadref" is true, we must simply return this implicit pointer.
  1344. If it is false, we are supposed the take the address of this implicit
  1345. pointer, which is not possible.
  1346. However, managed types are also implicit pointers in Pascal, so in that
  1347. case "taking the address" again consists of simply returning the
  1348. implicit pointer/current value (in case of a var/out parameter, this
  1349. value is stored inside an array).
  1350. }
  1351. if not loadref then
  1352. begin
  1353. if not is_managed_type(def) then
  1354. internalerror(2011020601);
  1355. tmploc:=l;
  1356. end
  1357. else
  1358. begin
  1359. if not jvmimplicitpointertype(def) then
  1360. begin
  1361. { passed by reference in array of single element; l contains the
  1362. base address of the array }
  1363. location_reset_ref(tmploc,LOC_REFERENCE,OS_ADDR,4);
  1364. reference_reset_base(tmploc.reference,getaddressregister(list,java_jlobject),0,4);
  1365. tmploc.reference.arrayreftype:=art_indexconst;
  1366. tmploc.reference.indexoffset:=0;
  1367. a_load_loc_reg(list,java_jlobject,java_jlobject,l,tmploc.reference.base);
  1368. end
  1369. else
  1370. tmploc:=l;
  1371. end;
  1372. case tmploc.loc of
  1373. LOC_REGISTER,
  1374. LOC_CREGISTER :
  1375. begin
  1376. { the implicit pointer is in a register and has to be in a
  1377. reference -> create a reference and put it there }
  1378. location_force_mem(list,tmploc,java_jlobject);
  1379. ref:=tmploc.reference;
  1380. end;
  1381. LOC_REFERENCE,
  1382. LOC_CREFERENCE :
  1383. begin
  1384. ref:=tmploc.reference;
  1385. end;
  1386. else
  1387. internalerror(2011020603);
  1388. end;
  1389. end;
  1390. procedure thlcgjvm.g_copyvaluepara_openarray(list: TAsmList; const ref: treference; const lenloc: tlocation; arrdef: tarraydef; destreg: tregister);
  1391. var
  1392. localref: treference;
  1393. arrloc: tlocation;
  1394. stackslots: longint;
  1395. begin
  1396. { temporary reference for passing to concatcopy }
  1397. tg.gethltemp(list,java_jlobject,java_jlobject.size,tt_persistent,localref);
  1398. stackslots:=prepare_stack_for_ref(list,localref,false);
  1399. { create the local copy of the array (lenloc is invalid, get length
  1400. directly from the array) }
  1401. location_reset_ref(arrloc,LOC_REFERENCE,OS_ADDR,sizeof(pint));
  1402. arrloc.reference:=ref;
  1403. g_getarraylen(list,arrloc);
  1404. g_newarray(list,arrdef,1);
  1405. a_load_stack_ref(list,java_jlobject,localref,stackslots);
  1406. { copy the source array to the destination }
  1407. g_concatcopy(list,arrdef,ref,localref);
  1408. { and put the array pointer in the register as expected by the caller }
  1409. a_load_ref_reg(list,java_jlobject,java_jlobject,localref,destreg);
  1410. end;
  1411. procedure thlcgjvm.g_releasevaluepara_openarray(list: TAsmList; arrdef: tarraydef; const l: tlocation);
  1412. begin
  1413. // do nothing, long live garbage collection!
  1414. end;
  1415. procedure thlcgjvm.gen_initialize_code(list: TAsmList);
  1416. var
  1417. ref: treference;
  1418. begin
  1419. { create globals with wrapped types such as arrays/records }
  1420. case current_procinfo.procdef.proctypeoption of
  1421. potype_unitinit:
  1422. begin
  1423. reference_reset_base(ref,NR_NO,0,1);
  1424. if assigned(current_module.globalsymtable) then
  1425. allocate_implicit_structs_for_st_with_base_ref(list,current_module.globalsymtable,ref,staticvarsym);
  1426. allocate_implicit_structs_for_st_with_base_ref(list,current_module.localsymtable,ref,staticvarsym);
  1427. end;
  1428. potype_class_constructor:
  1429. begin
  1430. { also initialise local variables, if any }
  1431. inherited;
  1432. { initialise class fields }
  1433. reference_reset_base(ref,NR_NO,0,1);
  1434. allocate_implicit_structs_for_st_with_base_ref(list,tabstractrecorddef(current_procinfo.procdef.owner.defowner).symtable,ref,staticvarsym);
  1435. end
  1436. else
  1437. inherited
  1438. end;
  1439. end;
  1440. procedure thlcgjvm.gen_entry_code(list: TAsmList);
  1441. begin
  1442. list.concat(Tai_force_line.Create);
  1443. end;
  1444. procedure thlcgjvm.gen_exit_code(list: TAsmList);
  1445. begin
  1446. { nothing }
  1447. end;
  1448. procedure thlcgjvm.a_load_stack_reg(list: TAsmList; size: tdef; reg: tregister);
  1449. var
  1450. opc: tasmop;
  1451. finishandval: aint;
  1452. begin
  1453. opc:=loadstoreopc(size,false,false,finishandval);
  1454. list.concat(taicpu.op_reg(opc,reg));
  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));
  1459. end;
  1460. procedure thlcgjvm.a_load_stack_ref(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1461. var
  1462. opc: tasmop;
  1463. finishandval: aint;
  1464. begin
  1465. { fake location that indicates the value has to remain on the stack }
  1466. if ref.base=NR_EVAL_STACK_BASE then
  1467. exit;
  1468. opc:=loadstoreopcref(size,false,ref,finishandval);
  1469. if ref.arrayreftype=art_none then
  1470. list.concat(taicpu.op_ref(opc,ref))
  1471. else
  1472. list.concat(taicpu.op_none(opc));
  1473. { avoid problems with getting the size of an open array etc }
  1474. if jvmimplicitpointertype(size) then
  1475. size:=java_jlobject;
  1476. decstack(list,1+ord(size.size>4)+extra_slots);
  1477. end;
  1478. procedure thlcgjvm.a_load_reg_stack(list: TAsmList; size: tdef; reg: tregister);
  1479. var
  1480. opc: tasmop;
  1481. finishandval: aint;
  1482. begin
  1483. opc:=loadstoreopc(size,true,false,finishandval);
  1484. list.concat(taicpu.op_reg(opc,reg));
  1485. { avoid problems with getting the size of an open array etc }
  1486. if jvmimplicitpointertype(size) then
  1487. size:=java_jlobject;
  1488. incstack(list,1+ord(size.size>4));
  1489. if finishandval<>-1 then
  1490. a_op_const_stack(list,OP_AND,size,finishandval);
  1491. end;
  1492. procedure thlcgjvm.a_load_ref_stack(list: TAsmList; size: tdef; const ref: treference; extra_slots: longint);
  1493. var
  1494. opc: tasmop;
  1495. finishandval: aint;
  1496. begin
  1497. { fake location that indicates the value is already on the stack? }
  1498. if (ref.base=NR_EVAL_STACK_BASE) then
  1499. exit;
  1500. opc:=loadstoreopcref(size,true,ref,finishandval);
  1501. if ref.arrayreftype=art_none then
  1502. list.concat(taicpu.op_ref(opc,ref))
  1503. else
  1504. list.concat(taicpu.op_none(opc));
  1505. { avoid problems with getting the size of an open array etc }
  1506. if jvmimplicitpointertype(size) then
  1507. size:=java_jlobject;
  1508. incstack(list,1+ord(size.size>4)-extra_slots);
  1509. if finishandval<>-1 then
  1510. a_op_const_stack(list,OP_AND,size,finishandval);
  1511. end;
  1512. function thlcgjvm.loadstoreopcref(def: tdef; isload: boolean; const ref: treference; out finishandval: aint): tasmop;
  1513. const
  1514. { isload static }
  1515. getputopc: array[boolean,boolean] of tasmop =
  1516. ((a_putfield,a_putstatic),
  1517. (a_getfield,a_getstatic));
  1518. begin
  1519. if assigned(ref.symbol) then
  1520. begin
  1521. { -> either a global (static) field, or a regular field. If a regular
  1522. field, then ref.base contains the self pointer, otherwise
  1523. ref.base=NR_NO. In both cases, the symbol contains all other
  1524. information (combined field name and type descriptor) }
  1525. result:=getputopc[isload,ref.base=NR_NO];
  1526. finishandval:=-1;
  1527. { erase sign extension for byte/smallint loads }
  1528. if (def2regtyp(def)=R_INTREGISTER) and
  1529. not is_signed(def) then
  1530. case def.size of
  1531. 1: finishandval:=255;
  1532. 2: finishandval:=65535;
  1533. end;
  1534. end
  1535. else
  1536. result:=loadstoreopc(def,isload,ref.arrayreftype<>art_none,finishandval);
  1537. end;
  1538. function thlcgjvm.loadstoreopc(def: tdef; isload, isarray: boolean; out finishandval: aint): tasmop;
  1539. var
  1540. size: longint;
  1541. begin
  1542. finishandval:=-1;
  1543. case def2regtyp(def) of
  1544. R_INTREGISTER:
  1545. begin
  1546. size:=def.size;
  1547. if not isarray then
  1548. begin
  1549. case size of
  1550. 1,2,3,4:
  1551. if isload then
  1552. result:=a_iload
  1553. else
  1554. result:=a_istore;
  1555. 8:
  1556. if isload then
  1557. result:=a_lload
  1558. else
  1559. result:=a_lstore;
  1560. else
  1561. internalerror(2011032814);
  1562. end;
  1563. end
  1564. { array }
  1565. else if isload then
  1566. begin
  1567. case size of
  1568. 1:
  1569. begin
  1570. result:=a_baload;
  1571. if not is_signed(def) then
  1572. finishandval:=255;
  1573. end;
  1574. 2:
  1575. begin
  1576. if is_widechar(def) then
  1577. result:=a_caload
  1578. else
  1579. begin
  1580. result:=a_saload;
  1581. { if we'd treat arrays of word as "array of widechar" we
  1582. could use a_caload, but that would make for even more
  1583. awkward interfacing with external Java code }
  1584. if not is_signed(def) then
  1585. finishandval:=65535;
  1586. end;
  1587. end;
  1588. 4: result:=a_iaload;
  1589. 8: result:=a_laload;
  1590. else
  1591. internalerror(2010120503);
  1592. end
  1593. end
  1594. else
  1595. begin
  1596. case size of
  1597. 1: result:=a_bastore;
  1598. 2: if not is_widechar(def) then
  1599. result:=a_sastore
  1600. else
  1601. result:=a_castore;
  1602. 4: result:=a_iastore;
  1603. 8: result:=a_lastore;
  1604. else
  1605. internalerror(2010120508);
  1606. end
  1607. end
  1608. end;
  1609. R_ADDRESSREGISTER:
  1610. if not isarray then
  1611. if isload then
  1612. result:=a_aload
  1613. else
  1614. result:=a_astore
  1615. else if isload then
  1616. result:=a_aaload
  1617. else
  1618. result:=a_aastore;
  1619. R_FPUREGISTER:
  1620. begin
  1621. case tfloatdef(def).floattype of
  1622. s32real:
  1623. if not isarray then
  1624. if isload then
  1625. result:=a_fload
  1626. else
  1627. result:=a_fstore
  1628. else if isload then
  1629. result:=a_faload
  1630. else
  1631. result:=a_fastore;
  1632. s64real:
  1633. if not isarray then
  1634. if isload then
  1635. result:=a_dload
  1636. else
  1637. result:=a_dstore
  1638. else if isload then
  1639. result:=a_daload
  1640. else
  1641. result:=a_dastore;
  1642. else
  1643. internalerror(2010120504);
  1644. end
  1645. end
  1646. else
  1647. internalerror(2010120502);
  1648. end;
  1649. end;
  1650. procedure thlcgjvm.resize_stack_int_val(list: TAsmList; fromsize, tosize: tcgsize; forarraystore: boolean);
  1651. begin
  1652. if fromsize in [OS_S64,OS_64] then
  1653. begin
  1654. if not(tosize in [OS_S64,OS_64]) then
  1655. begin
  1656. { truncate }
  1657. list.concat(taicpu.op_none(a_l2i));
  1658. decstack(list,1);
  1659. end;
  1660. end
  1661. else if tosize in [OS_S64,OS_64] then
  1662. begin
  1663. { extend }
  1664. list.concat(taicpu.op_none(a_i2l));
  1665. incstack(list,1);
  1666. { if it was an unsigned 32 bit value, remove sign extension }
  1667. if fromsize=OS_32 then
  1668. a_op_const_stack(list,OP_AND,s64inttype,cardinal($ffffffff));
  1669. end;
  1670. { if the value is immediately stored to an array afterwards, the store
  1671. instruction will properly truncate the value; otherwise we may need
  1672. additional truncation, except for 64/32 bit conversions, which are
  1673. already handled above }
  1674. if not forarraystore and
  1675. (not(fromsize in [OS_S64,OS_64,OS_32,OS_S32]) or
  1676. not(tosize in [OS_S64,OS_64,OS_32,OS_S32])) and
  1677. (tcgsize2size[fromsize]>tcgsize2size[tosize]) or
  1678. ((tcgsize2size[fromsize]=tcgsize2size[tosize]) and
  1679. (fromsize<>tosize)) or
  1680. { needs to mask out the sign in the top 16 bits }
  1681. ((fromsize=OS_S8) and
  1682. (tosize=OS_16)) then
  1683. case tosize of
  1684. OS_8:
  1685. a_op_const_stack(list,OP_AND,s32inttype,255);
  1686. OS_S8:
  1687. list.concat(taicpu.op_none(a_i2b));
  1688. OS_16:
  1689. list.concat(taicpu.op_none(a_i2c));
  1690. OS_S16:
  1691. list.concat(taicpu.op_none(a_i2s));
  1692. end;
  1693. end;
  1694. procedure thlcgjvm.allocate_implicit_struct_with_base_ref(list: TAsmList; vs: tabstractvarsym; ref: treference);
  1695. var
  1696. tmpref: treference;
  1697. begin
  1698. ref.symbol:=current_asmdata.RefAsmSymbol(vs.mangledname);
  1699. tg.gethltemp(list,vs.vardef,vs.vardef.size,tt_persistent,tmpref);
  1700. { only copy the reference, not the actual data }
  1701. a_load_ref_ref(list,java_jlobject,java_jlobject,tmpref,ref);
  1702. { remains live since there's still a reference to the created
  1703. entity }
  1704. tg.ungettemp(list,tmpref);
  1705. end;
  1706. procedure thlcgjvm.allocate_implicit_structs_for_st_with_base_ref(list: TAsmList; st: tsymtable; const ref: treference; allocvartyp: tsymtyp);
  1707. var
  1708. vs: tabstractvarsym;
  1709. i: longint;
  1710. begin
  1711. for i:=0 to st.symlist.count-1 do
  1712. begin
  1713. if (tsym(st.symlist[i]).typ<>allocvartyp) then
  1714. continue;
  1715. vs:=tabstractvarsym(st.symlist[i]);
  1716. if sp_internal in vs.symoptions then
  1717. continue;
  1718. if not jvmimplicitpointertype(vs.vardef) then
  1719. continue;
  1720. allocate_implicit_struct_with_base_ref(list,vs,ref);
  1721. end;
  1722. end;
  1723. procedure thlcgjvm.gen_initialize_fields_code(list: TAsmList);
  1724. var
  1725. selfpara: tparavarsym;
  1726. selfreg: tregister;
  1727. ref: treference;
  1728. obj: tabstractrecorddef;
  1729. i: longint;
  1730. needinit: boolean;
  1731. begin
  1732. obj:=tabstractrecorddef(current_procinfo.procdef.owner.defowner);
  1733. { check whether there are any fields that need initialisation }
  1734. needinit:=false;
  1735. for i:=0 to obj.symtable.symlist.count-1 do
  1736. if (tsym(obj.symtable.symlist[i]).typ=fieldvarsym) and
  1737. jvmimplicitpointertype(tfieldvarsym(obj.symtable.symlist[i]).vardef) then
  1738. begin
  1739. needinit:=true;
  1740. break;
  1741. end;
  1742. if not needinit then
  1743. exit;
  1744. selfpara:=tparavarsym(current_procinfo.procdef.parast.find('self'));
  1745. if not assigned(selfpara) then
  1746. internalerror(2011033001);
  1747. selfreg:=getaddressregister(list,selfpara.vardef);
  1748. a_load_loc_reg(list,obj,obj,selfpara.localloc,selfreg);
  1749. reference_reset_base(ref,selfreg,0,1);
  1750. allocate_implicit_structs_for_st_with_base_ref(list,obj.symtable,ref,fieldvarsym);
  1751. end;
  1752. procedure thlcgjvm.resizestackfpuval(list: TAsmList; fromsize, tosize: tcgsize);
  1753. begin
  1754. if (fromsize=OS_F32) and
  1755. (tosize=OS_F64) then
  1756. begin
  1757. list.concat(taicpu.op_none(a_f2d));
  1758. incstack(list,1);
  1759. end
  1760. else if (fromsize=OS_F64) and
  1761. (tosize=OS_F32) then
  1762. begin
  1763. list.concat(taicpu.op_none(a_d2f));
  1764. decstack(list,1);
  1765. end;
  1766. end;
  1767. procedure thlcgjvm.maybepreparedivu32(list: TAsmList; var op: topcg; size: tdef; out isdivu32: boolean);
  1768. begin
  1769. if (op=OP_DIV) and
  1770. (def_cgsize(size)=OS_32) then
  1771. begin
  1772. { needs zero-extension to 64 bit, because the JVM only supports
  1773. signed divisions }
  1774. resize_stack_int_val(list,OS_32,OS_S64,false);
  1775. op:=OP_IDIV;
  1776. isdivu32:=true;
  1777. end
  1778. else
  1779. isdivu32:=false;
  1780. end;
  1781. procedure thlcgjvm.a_call_name_intern(list: TAsmList; pd: tprocdef; const s: TSymStr; inheritedcall: boolean);
  1782. var
  1783. opc: tasmop;
  1784. begin
  1785. {
  1786. invoke types:
  1787. * invokeinterface: call method from an interface (must also specify
  1788. number of parameters in terms of stack slot count!)
  1789. * invokespecial: invoke a constructor, method in a superclass,
  1790. or private instance method
  1791. * invokestatic: invoke a class method (private or not)
  1792. * invokevirtual: invoke a regular method
  1793. }
  1794. case pd.owner.symtabletype of
  1795. globalsymtable,
  1796. staticsymtable,
  1797. localsymtable:
  1798. { regular and nested procedures are turned into static methods }
  1799. opc:=a_invokestatic;
  1800. objectsymtable:
  1801. begin
  1802. case tobjectdef(pd.owner.defowner).objecttype of
  1803. odt_javaclass:
  1804. begin
  1805. if (po_classmethod 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. odt_interfacejava:
  1815. { static interface methods are not allowed }
  1816. opc:=a_invokeinterface;
  1817. else
  1818. internalerror(2010122601);
  1819. end;
  1820. end;
  1821. recordsymtable:
  1822. begin
  1823. if (po_staticmethod in pd.procoptions) then
  1824. opc:=a_invokestatic
  1825. else if (pd.visibility=vis_private) or
  1826. (pd.proctypeoption=potype_constructor) or
  1827. inheritedcall then
  1828. opc:=a_invokespecial
  1829. else
  1830. opc:=a_invokevirtual;
  1831. end
  1832. else
  1833. internalerror(2010122602);
  1834. end;
  1835. if (opc<>a_invokeinterface) then
  1836. list.concat(taicpu.op_sym(opc,current_asmdata.RefAsmSymbol(s)))
  1837. else
  1838. begin
  1839. pd.init_paraloc_info(calleeside);
  1840. list.concat(taicpu.op_sym_const(opc,current_asmdata.RefAsmSymbol(s),pd.calleeargareasize));
  1841. end;
  1842. end;
  1843. procedure thlcgjvm.g_call_system_proc(list: TAsmList; const procname: string);
  1844. var
  1845. srsym: tsym;
  1846. pd: tprocdef;
  1847. begin
  1848. srsym:=tsym(systemunit.find(procname));
  1849. if not assigned(srsym) or
  1850. (srsym.typ<>procsym) then
  1851. Message1(cg_f_unknown_compilerproc,procname);
  1852. pd:=tprocdef(tprocsym(srsym).procdeflist[0]);
  1853. a_call_name(list,pd,pd.mangledname,false);
  1854. end;
  1855. procedure create_hlcodegen;
  1856. begin
  1857. hlcg:=thlcgjvm.create;
  1858. create_codegen;
  1859. end;
  1860. end.