hlcgcpu.pas 88 KB

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