hlcgcpu.pas 100 KB

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