hlcgcpu.pas 101 KB

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