hlcgcpu.pas 104 KB

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