hlcgcpu.pas 99 KB

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