hlcgcpu.pas 105 KB

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