hlcgcpu.pas 99 KB

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