hlcgcpu.pas 100 KB

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