aasmcpu.pas 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125
  1. {
  2. Copyright (c) 2019 by Free Pascal and Lazarus foundation
  3. Contains the assembler object for the WebAssembly
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit aasmcpu;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. globtype,globals,verbose,
  23. aasmbase,aasmtai,aasmdata,aasmsym,
  24. cgbase,cgutils,cpubase,cpuinfo,ogbase,
  25. symtype,
  26. widestr;
  27. { fake, there are no "mov reg,reg" instructions here }
  28. const
  29. { "mov reg,reg" source operand number }
  30. O_MOV_SOURCE = 0;
  31. { "mov reg,reg" source operand number }
  32. O_MOV_DEST = 0;
  33. type
  34. TWasmBasicTypeList = array of TWasmBasicType;
  35. { TWasmGlobalAsmSymbol }
  36. TWasmGlobalAsmSymbol = class(TAsmSymbol)
  37. private
  38. FWasmGlobalType: TWasmBasicType;
  39. procedure SetWasmGlobalType(AValue: TWasmBasicType);
  40. public
  41. property WasmGlobalType: TWasmBasicType read FWasmGlobalType write SetWasmGlobalType;
  42. end;
  43. { TWasmValueStack }
  44. TWasmValueStack = class
  45. private
  46. FValStack: array of TWasmBasicType;
  47. function GetCount: Integer;
  48. function GetItems(AIndex: Integer): TWasmBasicType;
  49. procedure SetCount(AValue: Integer);
  50. procedure SetItems(AIndex: Integer; AValue: TWasmBasicType);
  51. public
  52. procedure Push(wbt: TWasmBasicType);
  53. function Pop: TWasmBasicType;
  54. property Items[AIndex: Integer]: TWasmBasicType read GetItems write SetItems; default;
  55. property Count: Integer read GetCount write SetCount;
  56. end;
  57. { TWasmControlFrame }
  58. PWasmControlFrame = ^TWasmControlFrame;
  59. TWasmControlFrame = record
  60. opcode: tasmop;
  61. start_types: TWasmBasicTypeList;
  62. end_types: TWasmBasicTypeList;
  63. height: Integer;
  64. unreachable: Boolean;
  65. end;
  66. { TWasmControlStack }
  67. TWasmControlStack = class
  68. private
  69. FControlStack: array of TWasmControlFrame;
  70. function GetCount: Integer;
  71. function GetItems(AIndex: Integer): TWasmControlFrame;
  72. function GetPItems(AIndex: Integer): PWasmControlFrame;
  73. procedure SetItems(AIndex: Integer; const AValue: TWasmControlFrame);
  74. public
  75. procedure Push(const wcf: TWasmControlFrame);
  76. function Pop: TWasmControlFrame;
  77. property Items[AIndex: Integer]: TWasmControlFrame read GetItems write SetItems; default;
  78. property PItems[AIndex: Integer]: PWasmControlFrame read GetPItems;
  79. property Count: Integer read GetCount;
  80. end;
  81. taicpu = class;
  82. TGetLocalTypeProc = function(localidx: Integer): TWasmBasicType of object;
  83. { TWasmValidationStacks }
  84. TWasmValidationStacks = class
  85. private
  86. FValueStack: TWasmValueStack;
  87. FCtrlStack: TWasmControlStack;
  88. FGetLocalType: TGetLocalTypeProc;
  89. FFuncType: TWasmFuncType;
  90. FEndFunctionReached: Boolean;
  91. public
  92. constructor Create(AGetLocalType: TGetLocalTypeProc; AFuncType: TWasmFuncType);
  93. destructor Destroy; override;
  94. procedure PushVal(vt: TWasmBasicType);
  95. function PopVal: TWasmBasicType;
  96. function PopVal(expect: TWasmBasicType): TWasmBasicType;
  97. function PopVal_RefType: TWasmBasicType;
  98. procedure PushVals(vals: TWasmBasicTypeList);
  99. function PopVals(vals: TWasmBasicTypeList): TWasmBasicTypeList;
  100. procedure PushCtrl(_opcode: tasmop; _in, _out: TWasmBasicTypeList);
  101. function PopCtrl: TWasmControlFrame;
  102. function label_types(const frame: TWasmControlFrame): TWasmBasicTypeList;
  103. procedure Unreachable;
  104. procedure Validate(a: taicpu);
  105. end;
  106. twasmstruc_stack = class;
  107. TAsmMapFuncResultType = (amfrtNoChange, amfrtNewAi, amfrtNewList, amfrtDeleteAi);
  108. TAsmMapFuncResult = record
  109. case typ: TAsmMapFuncResultType of
  110. amfrtNoChange: ();
  111. amfrtNewAi: (newai: tai);
  112. amfrtNewList: (newlist: TAsmList);
  113. amfrtDeleteAi: ();
  114. end;
  115. TAsmMapFunc = function(ai: tai; blockstack: twasmstruc_stack): TAsmMapFuncResult of object;
  116. TWasmLocalAllocator = function(wbt: TWasmBasicType): Integer of object;
  117. { taicpu }
  118. taicpu = class(tai_cpu_abstract_sym)
  119. private type
  120. TCatchArray = array of taicpu;
  121. private
  122. insoffset : longint;
  123. public
  124. is_br_generated_by_goto: boolean;
  125. try_table_catch_clauses: tasmlist;
  126. constructor Create(op : tasmop);override;
  127. destructor Destroy;override;
  128. constructor op_none(op : tasmop);
  129. constructor op_reg(op : tasmop;_op1 : tregister);
  130. constructor op_const(op : tasmop;_op1 : aint);
  131. constructor op_ref(op : tasmop;const _op1 : treference);
  132. constructor op_sym(op : tasmop;_op1 : tasmsymbol);
  133. constructor op_sym_const(op : tasmop;_op1 : tasmsymbol;_op2 : aint);
  134. constructor op_sym_functype(op : tasmop;_op1 : tasmsymbol;_op2 : TWasmFuncType);
  135. constructor op_single(op : tasmop;_op1 : single);
  136. constructor op_double(op : tasmop;_op1 : double);
  137. constructor op_functype(op : tasmop; _op1: TWasmFuncType);
  138. constructor op_catch(op : tasmop;_op1 : TCatchArray);
  139. procedure loadfunctype(opidx:longint;ft:TWasmFuncType);
  140. procedure loadsingle(opidx:longint;f:single);
  141. procedure loaddouble(opidx:longint;d:double);
  142. { register allocation }
  143. function is_same_reg_move(regtype: Tregistertype):boolean; override;
  144. { register spilling code }
  145. function spilling_get_operation_type(opnr: longint): topertype;override;
  146. function Pass1(objdata:TObjData):longint;override;
  147. procedure Pass2(objdata:TObjData);override;
  148. end;
  149. taiwstype = (
  150. aitws_if,
  151. aitws_block,
  152. aitws_loop,
  153. aitws_legacy_try_delegate,
  154. aitws_legacy_try_catch
  155. );
  156. { taicpu_wasm_structured_instruction }
  157. taicpu_wasm_structured_instruction = class(tai)
  158. protected
  159. FLabel: TAsmLabel;
  160. FLabelIsNew: Boolean;
  161. public
  162. wstyp: taiwstype;
  163. constructor Create;
  164. procedure Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);virtual;abstract;
  165. procedure ConvertToFlatList(l: TAsmList);virtual;abstract;
  166. function getlabel: TAsmLabel;
  167. end;
  168. { tai_wasmstruc_if }
  169. tai_wasmstruc_if = class(taicpu_wasm_structured_instruction)
  170. if_instr: taicpu;
  171. then_asmlist: TAsmList;
  172. else_asmlist: TAsmList;
  173. constructor create_from(a_if_instr: taicpu; srclist: TAsmList);
  174. destructor Destroy; override;
  175. function getcopy:TLinkedListItem;override;
  176. procedure Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);override;
  177. procedure ConvertToFlatList(l: TAsmList);override;
  178. procedure ConvertToBrIf(list: TAsmList; local_alloc: TWasmLocalAllocator);
  179. end;
  180. { tai_wasmstruc_block }
  181. tai_wasmstruc_block = class(taicpu_wasm_structured_instruction)
  182. block_instr: taicpu;
  183. inner_asmlist: TAsmList;
  184. constructor create_from(a_block_instr: taicpu; srclist: TAsmList);
  185. destructor Destroy; override;
  186. function getcopy:TLinkedListItem;override;
  187. procedure Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);override;
  188. procedure ConvertToFlatList(l: TAsmList);override;
  189. end;
  190. { tai_wasmstruc_loop }
  191. tai_wasmstruc_loop = class(taicpu_wasm_structured_instruction)
  192. loop_instr: taicpu;
  193. inner_asmlist: TAsmList;
  194. constructor create_from(a_loop_instr: taicpu; srclist: TAsmList);
  195. destructor Destroy; override;
  196. function getcopy:TLinkedListItem;override;
  197. procedure Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);override;
  198. procedure ConvertToBr(list: TAsmList);
  199. procedure ConvertToFlatList(l: TAsmList);override;
  200. end;
  201. { tai_wasmstruc_legacy_try }
  202. tai_wasmstruc_legacy_try = class(taicpu_wasm_structured_instruction)
  203. private
  204. class function create_from(srclist: TAsmList): tai_wasmstruc_legacy_try;
  205. public
  206. try_asmlist: TAsmList;
  207. constructor internal_create(a_try_asmlist: TAsmList);
  208. destructor Destroy; override;
  209. function getcopy:TLinkedListItem;override;
  210. procedure ConvertToFlatList(l: TAsmList);override;
  211. end;
  212. { tai_wasmstruc_legacy_try_delegate }
  213. tai_wasmstruc_legacy_try_delegate = class(tai_wasmstruc_legacy_try)
  214. delegate_instr: taicpu;
  215. constructor internal_create(first_ins: taicpu; a_try_asmlist, srclist: TAsmList);
  216. destructor Destroy; override;
  217. function getcopy:TLinkedListItem;override;
  218. procedure Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);override;
  219. procedure ConvertToFlatList(l: TAsmList);override;
  220. end;
  221. { tai_wasmstruc_legacy_try_catch }
  222. tai_wasmstruc_legacy_try_catch = class(tai_wasmstruc_legacy_try)
  223. catch_list: array of record
  224. catch_instr: taicpu;
  225. asmlist: TAsmList;
  226. end;
  227. catch_all_asmlist: TAsmList;
  228. constructor internal_create(first_ins: taicpu; a_try_asmlist, srclist: TAsmList);
  229. destructor Destroy; override;
  230. function getcopy:TLinkedListItem;override;
  231. procedure Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);override;
  232. procedure ConvertToFlatList(l: TAsmList);override;
  233. end;
  234. { tai_wasmstruc_try_table }
  235. tai_wasmstruc_try_table = class(taicpu_wasm_structured_instruction)
  236. try_table_instr: taicpu;
  237. inner_asmlist: TAsmList;
  238. constructor create_from(a_try_table_instr: taicpu; srclist: TAsmList);
  239. destructor Destroy;override;
  240. function getcopy:TLinkedListItem;override;
  241. procedure Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);override;
  242. procedure ConvertToFlatList(l: TAsmList);override;
  243. end;
  244. { twasmstruc_stack }
  245. twasmstruc_stack = class
  246. private
  247. FStack: array of taicpu_wasm_structured_instruction;
  248. function Get(Index: Integer): taicpu_wasm_structured_instruction;
  249. public
  250. procedure push(ins: taicpu_wasm_structured_instruction);
  251. procedure pop;
  252. property Items [Index: Integer]: taicpu_wasm_structured_instruction read Get;default;
  253. end;
  254. tai_align = class(tai_align_abstract)
  255. { nothing to add }
  256. end;
  257. TImpExpType= (
  258. ie_Func, // functions
  259. ie_Table, // tables (arrays of methods)
  260. ie_Memory, // memory reference
  261. ie_Global // global variables
  262. );
  263. { tai_export_name }
  264. tai_export_name = class(tai)
  265. extname : ansistring; // external name
  266. intname : ansistring; // internal name
  267. symstype: TImpExpType;
  268. constructor create(const aextname, aintname: ansistring; asymtype: timpexptype);
  269. end;
  270. // local variable declaration
  271. { tai_local }
  272. tai_local = class(tai)
  273. locals: TWasmLocalsDynArray;
  274. constructor create(alocals: TWasmLocalsDynArray);
  275. procedure AddLocal(abasictype: TWasmBasicType);
  276. procedure AddLocals(alocals: TWasmLocalsDynArray);
  277. end;
  278. { tai_globaltype }
  279. tai_globaltype = class(tai)
  280. globalname: string;
  281. gtype: TWasmBasicType;
  282. immutable: boolean;
  283. is_external: boolean;
  284. is_global: boolean;
  285. sym : TWasmGlobalAsmSymbol;
  286. constructor create(const aglobalname:string; atype: TWasmBasicType; aimmutable: boolean);
  287. constructor create_local(const aglobalname:string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
  288. constructor create_global(const aglobalname:string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
  289. end;
  290. { tai_functype }
  291. tai_functype = class(tai)
  292. funcname: string;
  293. functype: TWasmFuncType;
  294. is_forward: Boolean;
  295. constructor create(const afuncname: string; afunctype: TWasmFuncType; aisforward: Boolean);
  296. destructor destroy;override;
  297. end;
  298. { tai_tagtype }
  299. tai_tagtype = class(tai)
  300. tagname: string;
  301. params: TWasmResultType;
  302. constructor create(const atagname: string; aparams: TWasmResultType);
  303. end;
  304. { tai_import_module }
  305. tai_import_module = class(tai)
  306. symname: string;
  307. importmodule: string;
  308. constructor create(const asymname, aimportmodule: string);
  309. end;
  310. { tai_import_name }
  311. tai_import_name = class(tai)
  312. symname: string;
  313. importname: string;
  314. constructor create(const asymname, aimportname: string);
  315. end;
  316. procedure InitAsm;
  317. procedure DoneAsm;
  318. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  319. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  320. procedure wasm_convert_to_structured_asmlist(var asmlist: TAsmList);
  321. procedure wasm_convert_to_flat_asmlist(var asmlist: TAsmList);
  322. procedure map_structured_asmlist(l: TAsmList; f: TAsmMapFunc);
  323. implementation
  324. uses
  325. ogwasm;
  326. function wasm_convert_first_item_to_structured(srclist: TAsmList): tai; forward;
  327. procedure map_structured_asmlist_inner(l: TAsmList; f: TAsmMapFunc; blockstack: twasmstruc_stack); forward;
  328. { TWasmGlobalAsmSymbol }
  329. procedure TWasmGlobalAsmSymbol.SetWasmGlobalType(AValue: TWasmBasicType);
  330. begin
  331. if FWasmGlobalType=AValue then
  332. Exit;
  333. if FWasmGlobalType<>wbt_Unknown then
  334. Internalerror(2024022503);
  335. FWasmGlobalType:=AValue;
  336. end;
  337. { TWasmValueStack }
  338. function TWasmValueStack.GetItems(AIndex: Integer): TWasmBasicType;
  339. var
  340. I: Integer;
  341. begin
  342. I:=High(FValStack)-AIndex;
  343. if (I<Low(FValStack)) or (I>High(FValStack)) then
  344. internalerror(2024011702);
  345. Result:=FValStack[I];
  346. end;
  347. procedure TWasmValueStack.SetCount(AValue: Integer);
  348. begin
  349. SetLength(FValStack,AValue);
  350. end;
  351. function TWasmValueStack.GetCount: Integer;
  352. begin
  353. Result:=Length(FValStack);
  354. end;
  355. procedure TWasmValueStack.SetItems(AIndex: Integer; AValue: TWasmBasicType);
  356. var
  357. I: Integer;
  358. begin
  359. I:=High(FValStack)-AIndex;
  360. if (I<Low(FValStack)) or (I>High(FValStack)) then
  361. internalerror(2024011703);
  362. FValStack[I]:=AValue;
  363. end;
  364. procedure TWasmValueStack.Push(wbt: TWasmBasicType);
  365. begin
  366. SetLength(FValStack,Length(FValStack)+1);
  367. FValStack[High(FValStack)]:=wbt;
  368. end;
  369. function TWasmValueStack.Pop: TWasmBasicType;
  370. begin
  371. if Length(FValStack)=0 then
  372. internalerror(2024011701);
  373. Result:=FValStack[High(FValStack)];
  374. SetLength(FValStack,Length(FValStack)-1);
  375. end;
  376. { TWasmControlStack }
  377. function TWasmControlStack.GetItems(AIndex: Integer): TWasmControlFrame;
  378. var
  379. I: Integer;
  380. begin
  381. I:=High(FControlStack)-AIndex;
  382. if (I<Low(FControlStack)) or (I>High(FControlStack)) then
  383. internalerror(2024013101);
  384. Result:=FControlStack[I];
  385. end;
  386. function TWasmControlStack.GetPItems(AIndex: Integer): PWasmControlFrame;
  387. var
  388. I: Integer;
  389. begin
  390. I:=High(FControlStack)-AIndex;
  391. if (I<Low(FControlStack)) or (I>High(FControlStack)) then
  392. internalerror(2024013101);
  393. Result:=@(FControlStack[I]);
  394. end;
  395. function TWasmControlStack.GetCount: Integer;
  396. begin
  397. Result:=Length(FControlStack);
  398. end;
  399. procedure TWasmControlStack.SetItems(AIndex: Integer; const AValue: TWasmControlFrame);
  400. var
  401. I: Integer;
  402. begin
  403. I:=High(FControlStack)-AIndex;
  404. if (I<Low(FControlStack)) or (I>High(FControlStack)) then
  405. internalerror(2024013102);
  406. FControlStack[I]:=AValue;
  407. end;
  408. procedure TWasmControlStack.Push(const wcf: TWasmControlFrame);
  409. begin
  410. SetLength(FControlStack,Length(FControlStack)+1);
  411. FControlStack[High(FControlStack)]:=wcf;
  412. end;
  413. function TWasmControlStack.Pop: TWasmControlFrame;
  414. begin
  415. if Length(FControlStack)=0 then
  416. internalerror(2024013103);
  417. Result:=FControlStack[High(FControlStack)];
  418. SetLength(FControlStack,Length(FControlStack)-1);
  419. end;
  420. { TWasmValidationStacks }
  421. constructor TWasmValidationStacks.Create(AGetLocalType: TGetLocalTypeProc; AFuncType: TWasmFuncType);
  422. begin
  423. FEndFunctionReached:=False;
  424. FGetLocalType:=AGetLocalType;
  425. FValueStack:=TWasmValueStack.Create;
  426. FCtrlStack:=TWasmControlStack.Create;
  427. FFuncType:=AFuncType;
  428. PushCtrl(a_block,[],[]);
  429. end;
  430. destructor TWasmValidationStacks.Destroy;
  431. begin
  432. FValueStack.Free;
  433. FCtrlStack.Free;
  434. inherited Destroy;
  435. end;
  436. procedure TWasmValidationStacks.PushVal(vt: TWasmBasicType);
  437. begin
  438. FValueStack.Push(vt);
  439. end;
  440. function TWasmValidationStacks.PopVal: TWasmBasicType;
  441. begin
  442. if FValueStack.Count = FCtrlStack[0].height then
  443. begin
  444. Result:=wbt_Unknown;
  445. if not FCtrlStack[0].unreachable then
  446. internalerror(2024013104);
  447. end
  448. else
  449. Result:=FValueStack.Pop;
  450. end;
  451. function TWasmValidationStacks.PopVal(expect: TWasmBasicType): TWasmBasicType;
  452. begin
  453. Result:=wbt_Unknown;
  454. Result:=PopVal();
  455. if (Result<>expect) and (Result<>wbt_Unknown) and (expect<>wbt_Unknown) then
  456. internalerror(2024013105);
  457. end;
  458. function TWasmValidationStacks.PopVal_RefType: TWasmBasicType;
  459. begin
  460. Result:=wbt_Unknown;
  461. Result:=PopVal;
  462. if not (Result in (WasmReferenceTypes + [wbt_Unknown])) then
  463. internalerror(2024020501);
  464. end;
  465. procedure TWasmValidationStacks.PushVals(vals: TWasmBasicTypeList);
  466. var
  467. v: TWasmBasicType;
  468. begin
  469. for v in vals do
  470. PushVal(v);
  471. end;
  472. function TWasmValidationStacks.PopVals(vals: TWasmBasicTypeList): TWasmBasicTypeList;
  473. var
  474. I: Integer;
  475. begin
  476. Result:=nil;
  477. SetLength(Result,Length(vals));
  478. for I:=High(vals) downto Low(Vals) do
  479. Result[I]:=PopVal(vals[I]);
  480. end;
  481. procedure TWasmValidationStacks.PushCtrl(_opcode: tasmop; _in, _out: TWasmBasicTypeList);
  482. var
  483. frame: TWasmControlFrame;
  484. begin
  485. FillChar(frame,SizeOf(frame),0);
  486. with frame do
  487. begin
  488. opcode:=_opcode;
  489. start_types:=Copy(_in);
  490. end_types:=Copy(_out);
  491. height:=FValueStack.Count;
  492. unreachable:=False;
  493. end;
  494. FCtrlStack.Push(frame);
  495. end;
  496. function TWasmValidationStacks.PopCtrl: TWasmControlFrame;
  497. begin
  498. Result:=Default(TWasmControlFrame);
  499. if FCtrlStack.Count=0 then
  500. internalerror(2024013106);
  501. Result:=FCtrlStack[0];
  502. PopVals(Result.end_types);
  503. if FValueStack.Count<>Result.height then
  504. internalerror(2024013107);
  505. FCtrlStack.Pop;
  506. end;
  507. function TWasmValidationStacks.label_types(const frame: TWasmControlFrame): TWasmBasicTypeList;
  508. begin
  509. if frame.opcode=a_loop then
  510. Result:=frame.start_types
  511. else
  512. Result:=frame.end_types;
  513. end;
  514. procedure TWasmValidationStacks.Unreachable;
  515. var
  516. c: PWasmControlFrame;
  517. begin
  518. c:=FCtrlStack.PItems[0];
  519. FValueStack.Count:=c^.height;
  520. c^.unreachable:=true;
  521. end;
  522. procedure TWasmValidationStacks.Validate(a: taicpu);
  523. function GetLocalIndex: Integer;
  524. begin
  525. Result:=-1;
  526. with a do
  527. begin
  528. if ops<>1 then
  529. internalerror(2024020801);
  530. with oper[0]^ do
  531. case typ of
  532. top_ref:
  533. begin
  534. if assigned(ref^.symbol) then
  535. internalerror(2024020802);
  536. if ref^.base<>NR_STACK_POINTER_REG then
  537. internalerror(2024020803);
  538. if ref^.index<>NR_NO then
  539. internalerror(2024020804);
  540. Result:=ref^.offset;
  541. end;
  542. top_const:
  543. Result:=val;
  544. else
  545. internalerror(2024020805);
  546. end;
  547. end;
  548. end;
  549. var
  550. frame: TWasmControlFrame;
  551. n: TCGInt;
  552. begin
  553. if FEndFunctionReached then
  554. internalerror(2024022602);
  555. case a.opcode of
  556. a_nop:
  557. ;
  558. a_i32_const:
  559. PushVal(wbt_i32);
  560. a_i64_const:
  561. PushVal(wbt_i64);
  562. a_f32_const:
  563. PushVal(wbt_f32);
  564. a_f64_const:
  565. PushVal(wbt_f64);
  566. a_i32_add,
  567. a_i32_sub,
  568. a_i32_mul,
  569. a_i32_div_s, a_i32_div_u,
  570. a_i32_rem_s, a_i32_rem_u,
  571. a_i32_and,
  572. a_i32_or,
  573. a_i32_xor,
  574. a_i32_shl,
  575. a_i32_shr_s, a_i32_shr_u,
  576. a_i32_rotl,
  577. a_i32_rotr:
  578. begin
  579. PopVal(wbt_i32);
  580. PopVal(wbt_i32);
  581. PushVal(wbt_i32);
  582. end;
  583. a_i64_add,
  584. a_i64_sub,
  585. a_i64_mul,
  586. a_i64_div_s, a_i64_div_u,
  587. a_i64_rem_s, a_i64_rem_u,
  588. a_i64_and,
  589. a_i64_or,
  590. a_i64_xor,
  591. a_i64_shl,
  592. a_i64_shr_s, a_i64_shr_u,
  593. a_i64_rotl,
  594. a_i64_rotr:
  595. begin
  596. PopVal(wbt_i64);
  597. PopVal(wbt_i64);
  598. PushVal(wbt_i64);
  599. end;
  600. a_f32_add,
  601. a_f32_sub,
  602. a_f32_mul,
  603. a_f32_div,
  604. a_f32_min,
  605. a_f32_max,
  606. a_f32_copysign:
  607. begin
  608. PopVal(wbt_f32);
  609. PopVal(wbt_f32);
  610. PushVal(wbt_f32);
  611. end;
  612. a_f64_add,
  613. a_f64_sub,
  614. a_f64_mul,
  615. a_f64_div,
  616. a_f64_min,
  617. a_f64_max,
  618. a_f64_copysign:
  619. begin
  620. PopVal(wbt_f64);
  621. PopVal(wbt_f64);
  622. PushVal(wbt_f64);
  623. end;
  624. a_i32_clz,
  625. a_i32_ctz,
  626. a_i32_popcnt:
  627. begin
  628. PopVal(wbt_i32);
  629. PushVal(wbt_i32);
  630. end;
  631. a_i64_clz,
  632. a_i64_ctz,
  633. a_i64_popcnt:
  634. begin
  635. PopVal(wbt_i64);
  636. PushVal(wbt_i64);
  637. end;
  638. a_f32_abs,
  639. a_f32_neg,
  640. a_f32_sqrt,
  641. a_f32_ceil,
  642. a_f32_floor,
  643. a_f32_trunc,
  644. a_f32_nearest:
  645. begin
  646. PopVal(wbt_f32);
  647. PushVal(wbt_f32);
  648. end;
  649. a_f64_abs,
  650. a_f64_neg,
  651. a_f64_sqrt,
  652. a_f64_ceil,
  653. a_f64_floor,
  654. a_f64_trunc,
  655. a_f64_nearest:
  656. begin
  657. PopVal(wbt_f64);
  658. PushVal(wbt_f64);
  659. end;
  660. a_i32_eqz:
  661. begin
  662. PopVal(wbt_i32);
  663. PushVal(wbt_i32);
  664. end;
  665. a_i64_eqz:
  666. begin
  667. PopVal(wbt_i64);
  668. PushVal(wbt_i32);
  669. end;
  670. a_i32_eq,
  671. a_i32_ne,
  672. a_i32_lt_s, a_i32_lt_u,
  673. a_i32_gt_s, a_i32_gt_u,
  674. a_i32_le_s, a_i32_le_u,
  675. a_i32_ge_s, a_i32_ge_u:
  676. begin
  677. PopVal(wbt_i32);
  678. PopVal(wbt_i32);
  679. PushVal(wbt_i32);
  680. end;
  681. a_i64_eq,
  682. a_i64_ne,
  683. a_i64_lt_s, a_i64_lt_u,
  684. a_i64_gt_s, a_i64_gt_u,
  685. a_i64_le_s, a_i64_le_u,
  686. a_i64_ge_s, a_i64_ge_u:
  687. begin
  688. PopVal(wbt_i64);
  689. PopVal(wbt_i64);
  690. PushVal(wbt_i32);
  691. end;
  692. a_f32_eq,
  693. a_f32_ne,
  694. a_f32_lt,
  695. a_f32_gt,
  696. a_f32_le,
  697. a_f32_ge:
  698. begin
  699. PopVal(wbt_f32);
  700. PopVal(wbt_f32);
  701. PushVal(wbt_i32);
  702. end;
  703. a_f64_eq,
  704. a_f64_ne,
  705. a_f64_lt,
  706. a_f64_gt,
  707. a_f64_le,
  708. a_f64_ge:
  709. begin
  710. PopVal(wbt_f64);
  711. PopVal(wbt_f64);
  712. PushVal(wbt_i32);
  713. end;
  714. a_i32_extend8_s,
  715. a_i32_extend16_s:
  716. begin
  717. PopVal(wbt_i32);
  718. PushVal(wbt_i32);
  719. end;
  720. a_i64_extend8_s,
  721. a_i64_extend16_s,
  722. a_i64_extend32_s:
  723. begin
  724. PopVal(wbt_i64);
  725. PushVal(wbt_i64);
  726. end;
  727. a_i32_wrap_i64:
  728. begin
  729. PopVal(wbt_i64);
  730. PushVal(wbt_i32);
  731. end;
  732. a_i64_extend_i32_s,
  733. a_i64_extend_i32_u:
  734. begin
  735. PopVal(wbt_i32);
  736. PushVal(wbt_i64);
  737. end;
  738. a_i32_trunc_f32_s,
  739. a_i32_trunc_f32_u,
  740. a_i32_trunc_sat_f32_s,
  741. a_i32_trunc_sat_f32_u:
  742. begin
  743. PopVal(wbt_f32);
  744. PushVal(wbt_i32);
  745. end;
  746. a_i32_trunc_f64_s,
  747. a_i32_trunc_f64_u,
  748. a_i32_trunc_sat_f64_s,
  749. a_i32_trunc_sat_f64_u:
  750. begin
  751. PopVal(wbt_f64);
  752. PushVal(wbt_i32);
  753. end;
  754. a_i64_trunc_f32_s,
  755. a_i64_trunc_f32_u,
  756. a_i64_trunc_sat_f32_s,
  757. a_i64_trunc_sat_f32_u:
  758. begin
  759. PopVal(wbt_f32);
  760. PushVal(wbt_i64);
  761. end;
  762. a_i64_trunc_f64_s,
  763. a_i64_trunc_f64_u,
  764. a_i64_trunc_sat_f64_s,
  765. a_i64_trunc_sat_f64_u:
  766. begin
  767. PopVal(wbt_f64);
  768. PushVal(wbt_i64);
  769. end;
  770. a_f32_demote_f64:
  771. begin
  772. PopVal(wbt_f64);
  773. PushVal(wbt_f32);
  774. end;
  775. a_f64_promote_f32:
  776. begin
  777. PopVal(wbt_f32);
  778. PushVal(wbt_f64);
  779. end;
  780. a_f32_convert_i32_s,
  781. a_f32_convert_i32_u:
  782. begin
  783. PopVal(wbt_i32);
  784. PushVal(wbt_f32);
  785. end;
  786. a_f32_convert_i64_s,
  787. a_f32_convert_i64_u:
  788. begin
  789. PopVal(wbt_i64);
  790. PushVal(wbt_f32);
  791. end;
  792. a_f64_convert_i32_s,
  793. a_f64_convert_i32_u:
  794. begin
  795. PopVal(wbt_i32);
  796. PushVal(wbt_f64);
  797. end;
  798. a_f64_convert_i64_s,
  799. a_f64_convert_i64_u:
  800. begin
  801. PopVal(wbt_i64);
  802. PushVal(wbt_f64);
  803. end;
  804. a_i32_reinterpret_f32:
  805. begin
  806. PopVal(wbt_f32);
  807. PushVal(wbt_i32);
  808. end;
  809. a_i64_reinterpret_f64:
  810. begin
  811. PopVal(wbt_f64);
  812. PushVal(wbt_i64);
  813. end;
  814. a_f32_reinterpret_i32:
  815. begin
  816. PopVal(wbt_i32);
  817. PushVal(wbt_f32);
  818. end;
  819. a_f64_reinterpret_i64:
  820. begin
  821. PopVal(wbt_i64);
  822. PushVal(wbt_f64);
  823. end;
  824. a_ref_null_externref:
  825. PushVal(wbt_externref);
  826. a_ref_null_funcref:
  827. PushVal(wbt_funcref);
  828. a_ref_is_null:
  829. begin
  830. PopVal_RefType;
  831. PushVal(wbt_i32);
  832. end;
  833. a_drop:
  834. PopVal;
  835. a_unreachable:
  836. Unreachable;
  837. a_i32_load,
  838. a_i32_load16_s, a_i32_load16_u,
  839. a_i32_load8_s, a_i32_load8_u:
  840. begin
  841. PopVal(wbt_i32);
  842. PushVal(wbt_i32);
  843. end;
  844. a_i64_load,
  845. a_i64_load32_s, a_i64_load32_u,
  846. a_i64_load16_s, a_i64_load16_u,
  847. a_i64_load8_s, a_i64_load8_u:
  848. begin
  849. PopVal(wbt_i32);
  850. PushVal(wbt_i64);
  851. end;
  852. a_f32_load:
  853. begin
  854. PopVal(wbt_i32);
  855. PushVal(wbt_f32);
  856. end;
  857. a_f64_load:
  858. begin
  859. PopVal(wbt_i32);
  860. PushVal(wbt_f64);
  861. end;
  862. a_i32_store,
  863. a_i32_store16,
  864. a_i32_store8:
  865. begin
  866. PopVal(wbt_i32);
  867. PopVal(wbt_i32);
  868. end;
  869. a_i64_store,
  870. a_i64_store32,
  871. a_i64_store16,
  872. a_i64_store8:
  873. begin
  874. PopVal(wbt_i64);
  875. PopVal(wbt_i32);
  876. end;
  877. a_f32_store:
  878. begin
  879. PopVal(wbt_f32);
  880. PopVal(wbt_i32);
  881. end;
  882. a_f64_store:
  883. begin
  884. PopVal(wbt_f64);
  885. PopVal(wbt_i32);
  886. end;
  887. a_memory_size:
  888. PushVal(wbt_i32);
  889. a_memory_grow:
  890. begin
  891. PopVal(wbt_i32);
  892. PushVal(wbt_i32);
  893. end;
  894. a_memory_fill,
  895. a_memory_copy:
  896. begin
  897. PopVal(wbt_i32);
  898. PopVal(wbt_i32);
  899. PopVal(wbt_i32);
  900. end;
  901. a_local_get:
  902. PushVal(FGetLocalType(GetLocalIndex));
  903. a_local_set:
  904. PopVal(FGetLocalType(GetLocalIndex));
  905. a_local_tee:
  906. begin
  907. PopVal(FGetLocalType(GetLocalIndex));
  908. PushVal(FGetLocalType(GetLocalIndex));
  909. end;
  910. a_global_get,
  911. a_global_set:
  912. begin
  913. if a.ops<>1 then
  914. internalerror(2024022504);
  915. if a.oper[0]^.typ<>top_ref then
  916. internalerror(2024022505);
  917. if not assigned(a.oper[0]^.ref^.symbol) then
  918. internalerror(2024022506);
  919. if (a.oper[0]^.ref^.base<>NR_NO) or (a.oper[0]^.ref^.index<>NR_NO) or (a.oper[0]^.ref^.offset<>0) then
  920. internalerror(2024022507);
  921. if a.oper[0]^.ref^.symbol.typ<>AT_WASM_GLOBAL then
  922. internalerror(2024022508);
  923. case a.opcode of
  924. a_global_get:
  925. PushVal(TWasmGlobalAsmSymbol(a.oper[0]^.ref^.symbol).WasmGlobalType);
  926. a_global_set:
  927. PopVal(TWasmGlobalAsmSymbol(a.oper[0]^.ref^.symbol).WasmGlobalType);
  928. else
  929. internalerror(2024022509);
  930. end;
  931. end;
  932. a_call:
  933. begin
  934. if a.ops<>2 then
  935. internalerror(2024022501);
  936. if a.oper[1]^.typ<>top_functype then
  937. internalerror(2024022502);
  938. PopVals(a.oper[1]^.functype.params);
  939. PushVals(a.oper[1]^.functype.results);
  940. end;
  941. a_call_indirect:
  942. begin
  943. if a.ops<>1 then
  944. internalerror(2024022401);
  945. if a.oper[0]^.typ<>top_functype then
  946. internalerror(2024022402);
  947. PopVal(wbt_i32);
  948. PopVals(a.oper[0]^.functype.params);
  949. PushVals(a.oper[0]^.functype.results);
  950. end;
  951. a_if,
  952. a_block,
  953. a_loop,
  954. a_legacy_try:
  955. begin
  956. if a.opcode=a_if then
  957. PopVal(wbt_i32);
  958. if a.ops>1 then
  959. internalerror(2024022510);
  960. if a.ops=0 then
  961. PushCtrl(a.opcode,[],[])
  962. else
  963. begin
  964. if a.oper[0]^.typ<>top_functype then
  965. internalerror(2024022511);
  966. PopVals(a.oper[0]^.functype.params);
  967. PushCtrl(a.opcode,a.oper[0]^.functype.params,a.oper[0]^.functype.results);
  968. end;
  969. end;
  970. a_else:
  971. begin
  972. frame:=PopCtrl;
  973. if frame.opcode<>a_if then
  974. internalerror(2024022512);
  975. PushCtrl(a_else,frame.start_types,frame.end_types);
  976. end;
  977. a_legacy_catch:
  978. begin
  979. frame:=PopCtrl;
  980. if (frame.opcode<>a_legacy_try) and (frame.opcode<>a_legacy_catch) then
  981. internalerror(2024022701);
  982. PushCtrl(a_legacy_catch,frame.start_types,frame.end_types);
  983. end;
  984. a_end_if:
  985. begin
  986. frame:=PopCtrl;
  987. if (frame.opcode<>a_if) and (frame.opcode<>a_else) then
  988. internalerror(2024022513);
  989. PushVals(frame.end_types);
  990. end;
  991. a_end_block:
  992. begin
  993. frame:=PopCtrl;
  994. if frame.opcode<>a_block then
  995. internalerror(2024022514);
  996. PushVals(frame.end_types);
  997. end;
  998. a_end_loop:
  999. begin
  1000. frame:=PopCtrl;
  1001. if frame.opcode<>a_loop then
  1002. internalerror(2024022515);
  1003. PushVals(frame.end_types);
  1004. end;
  1005. a_end_legacy_try:
  1006. begin
  1007. frame:=PopCtrl;
  1008. if (frame.opcode<>a_legacy_try) and (frame.opcode<>a_legacy_catch) then
  1009. internalerror(2024022702);
  1010. PushVals(frame.end_types);
  1011. end;
  1012. a_br:
  1013. begin
  1014. if a.ops<>1 then
  1015. internalerror(2024022516);
  1016. if a.oper[0]^.typ<>top_const then
  1017. internalerror(2024022517);
  1018. n:=a.oper[0]^.val;
  1019. if FCtrlStack.Count < n then
  1020. internalerror(2024022518);
  1021. PopVals(label_types(FCtrlStack[n]));
  1022. Unreachable;
  1023. end;
  1024. a_br_if:
  1025. begin
  1026. if a.ops<>1 then
  1027. internalerror(2024022519);
  1028. if a.oper[0]^.typ<>top_const then
  1029. internalerror(2024022520);
  1030. n:=a.oper[0]^.val;
  1031. if FCtrlStack.Count < n then
  1032. internalerror(2024022521);
  1033. PopVal(wbt_i32);
  1034. PopVals(label_types(FCtrlStack[n]));
  1035. PushVals(label_types(FCtrlStack[n]));
  1036. end;
  1037. a_legacy_throw:
  1038. Unreachable;
  1039. a_legacy_rethrow:
  1040. Unreachable;
  1041. a_return:
  1042. begin
  1043. PopVals(FFuncType.results);
  1044. Unreachable;
  1045. end;
  1046. a_end_function:
  1047. FEndFunctionReached:=True;
  1048. else
  1049. internalerror(2024030502);
  1050. end;
  1051. end;
  1052. { twasmstruc_stack }
  1053. function twasmstruc_stack.Get(Index: Integer): taicpu_wasm_structured_instruction;
  1054. begin
  1055. {$push}{$r+,q+}
  1056. Result:=FStack[High(FStack)-Index];
  1057. {$pop}
  1058. end;
  1059. procedure twasmstruc_stack.push(ins: taicpu_wasm_structured_instruction);
  1060. begin
  1061. SetLength(FStack,Length(FStack)+1);
  1062. FStack[High(FStack)]:=ins;
  1063. end;
  1064. procedure twasmstruc_stack.pop;
  1065. begin
  1066. SetLength(FStack,Length(FStack)-1);
  1067. end;
  1068. { taicpu_wasm_structured_instruction }
  1069. constructor taicpu_wasm_structured_instruction.Create;
  1070. begin
  1071. inherited;
  1072. typ:=ait_wasm_structured_instruction;
  1073. end;
  1074. function taicpu_wasm_structured_instruction.getlabel: TAsmLabel;
  1075. begin
  1076. if not assigned(FLabel) then
  1077. begin
  1078. current_asmdata.getjumplabel(FLabel);
  1079. FLabelIsNew:=true;
  1080. end;
  1081. result:=FLabel;
  1082. end;
  1083. { tai_wasmstruc_if }
  1084. constructor tai_wasmstruc_if.create_from(a_if_instr: taicpu; srclist: TAsmList);
  1085. var
  1086. p: tai;
  1087. ThenDone, ElsePresent, ElseDone: Boolean;
  1088. begin
  1089. wstyp:=aitws_if;
  1090. inherited Create;
  1091. if assigned(a_if_instr.Previous) or assigned(a_if_instr.Next) then
  1092. internalerror(2023100301);
  1093. if_instr:=a_if_instr;
  1094. then_asmlist:=TAsmList.Create;
  1095. ThenDone:=False;
  1096. ElsePresent:=False;
  1097. repeat
  1098. p:=tai(srclist.First);
  1099. if not assigned(p) then
  1100. internalerror(2023100302);
  1101. if (p.typ=ait_instruction) and (taicpu(p).opcode in [a_else,a_end_if,a_end_block,a_end_loop,a_end_legacy_try,a_legacy_catch,a_legacy_catch_all,a_legacy_delegate]) then
  1102. begin
  1103. srclist.Remove(p);
  1104. case taicpu(p).opcode of
  1105. a_else:
  1106. begin
  1107. ThenDone:=True;
  1108. ElsePresent:=True;
  1109. end;
  1110. a_end_if:
  1111. ThenDone:=True;
  1112. else
  1113. internalerror(2023100501);
  1114. end;
  1115. end
  1116. else
  1117. then_asmlist.Concat(wasm_convert_first_item_to_structured(srclist));
  1118. until ThenDone;
  1119. if ElsePresent then
  1120. begin
  1121. else_asmlist:=TAsmList.Create;
  1122. ElseDone:=False;
  1123. repeat
  1124. p:=tai(srclist.First);
  1125. if not assigned(p) then
  1126. internalerror(2023100303);
  1127. if (p.typ=ait_instruction) and (taicpu(p).opcode=a_end_if) then
  1128. begin
  1129. srclist.Remove(p);
  1130. ElseDone:=True;
  1131. end
  1132. else
  1133. else_asmlist.Concat(wasm_convert_first_item_to_structured(srclist));
  1134. until ElseDone;
  1135. end;
  1136. end;
  1137. destructor tai_wasmstruc_if.Destroy;
  1138. begin
  1139. then_asmlist.free;
  1140. else_asmlist.free;
  1141. if_instr.free;
  1142. inherited Destroy;
  1143. end;
  1144. function tai_wasmstruc_if.getcopy: TLinkedListItem;
  1145. var
  1146. p: tai_wasmstruc_if;
  1147. begin
  1148. p:=tai_wasmstruc_if(inherited getcopy);
  1149. if assigned(if_instr) then
  1150. p.if_instr:=taicpu(if_instr.getcopy);
  1151. if assigned(then_asmlist) then
  1152. begin
  1153. p.then_asmlist:=TAsmList.Create;
  1154. p.then_asmlist.concatListcopy(then_asmlist);
  1155. end;
  1156. if assigned(else_asmlist) then
  1157. begin
  1158. p.else_asmlist:=TAsmList.Create;
  1159. p.else_asmlist.concatListcopy(else_asmlist);
  1160. end;
  1161. getcopy:=p;
  1162. end;
  1163. procedure tai_wasmstruc_if.Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);
  1164. begin
  1165. blockstack.push(self);
  1166. map_structured_asmlist_inner(then_asmlist,f,blockstack);
  1167. map_structured_asmlist_inner(else_asmlist,f,blockstack);
  1168. blockstack.pop;
  1169. end;
  1170. procedure tai_wasmstruc_if.ConvertToFlatList(l: TAsmList);
  1171. begin
  1172. l.Concat(if_instr);
  1173. if_instr:=nil;
  1174. l.concatList(then_asmlist);
  1175. if assigned(else_asmlist) then
  1176. begin
  1177. l.Concat(taicpu.op_none(A_ELSE));
  1178. l.concatList(else_asmlist);
  1179. end;
  1180. l.Concat(taicpu.op_none(a_end_if));
  1181. if FLabelIsNew then
  1182. l.concat(tai_label.create(FLabel));
  1183. end;
  1184. procedure tai_wasmstruc_if.ConvertToBrIf(list: TAsmList; local_alloc: TWasmLocalAllocator);
  1185. var
  1186. res_ft: TWasmFuncType;
  1187. save_if_reg: Integer;
  1188. save_param_reg: array of Integer;
  1189. save_result_reg: array of Integer;
  1190. procedure AllocateLocalsForSavingParamsAndResult;
  1191. var
  1192. i: Integer;
  1193. begin
  1194. if not assigned(res_ft) then
  1195. exit;
  1196. if length(res_ft.params)<>0 then
  1197. begin
  1198. save_if_reg:=local_alloc(wbt_i32);
  1199. SetLength(save_param_reg,length(res_ft.params));
  1200. for i:=low(res_ft.params) to high(res_ft.params) do
  1201. save_param_reg[i]:=local_alloc(res_ft.params[i]);
  1202. end;
  1203. if length(res_ft.results)<>0 then
  1204. begin
  1205. SetLength(save_result_reg,length(res_ft.results));
  1206. for i:=low(res_ft.results) to high(res_ft.results) do
  1207. save_result_reg[i]:=local_alloc(res_ft.results[i]);
  1208. end;
  1209. end;
  1210. procedure SaveParams;
  1211. var
  1212. i: Integer;
  1213. begin
  1214. if (not assigned(res_ft)) or (length(res_ft.params)=0) then
  1215. exit;
  1216. list.concat(taicpu.op_const(a_local_set,save_if_reg));
  1217. for i:=high(res_ft.params) downto low(res_ft.params) do
  1218. list.concat(taicpu.op_const(a_local_set,save_param_reg[i]));
  1219. list.concat(taicpu.op_const(a_local_get,save_if_reg));
  1220. end;
  1221. procedure RestoreParams;
  1222. var
  1223. i: Integer;
  1224. begin
  1225. if (not assigned(res_ft)) or (length(res_ft.params)=0) then
  1226. exit;
  1227. for i:=low(res_ft.params) to high(res_ft.params) do
  1228. list.concat(taicpu.op_const(a_local_get,save_param_reg[i]));
  1229. end;
  1230. procedure SaveResults;
  1231. var
  1232. i: Integer;
  1233. begin
  1234. if (not assigned(res_ft)) or (length(res_ft.results)=0) then
  1235. exit;
  1236. for i:=high(res_ft.results) downto low(res_ft.results) do
  1237. list.concat(taicpu.op_const(a_local_set,save_result_reg[i]));
  1238. end;
  1239. procedure RestoreResults;
  1240. var
  1241. i: Integer;
  1242. begin
  1243. if (not assigned(res_ft)) or (length(res_ft.results)=0) then
  1244. exit;
  1245. for i:=low(res_ft.results) to high(res_ft.results) do
  1246. list.concat(taicpu.op_const(a_local_get,save_result_reg[i]));
  1247. end;
  1248. var
  1249. then_label: TAsmLabel;
  1250. begin
  1251. if if_instr.ops>1 then
  1252. internalerror(2023101701);
  1253. if (if_instr.ops=1) and (if_instr.oper[0]^.typ=top_functype) then
  1254. res_ft:=if_instr.oper[0]^.functype
  1255. else
  1256. res_ft:=nil;
  1257. AllocateLocalsForSavingParamsAndResult;
  1258. current_asmdata.getjumplabel(then_label);
  1259. SaveParams;
  1260. list.concat(taicpu.op_sym(a_br_if,then_label));
  1261. RestoreParams;
  1262. if assigned(else_asmlist) then
  1263. list.concatList(else_asmlist);
  1264. SaveResults;
  1265. list.concat(taicpu.op_sym(a_br, GetLabel));
  1266. list.concat(tai_label.create(then_label));
  1267. RestoreParams;
  1268. list.concatList(then_asmlist);
  1269. SaveResults;
  1270. list.concat(tai_label.create(GetLabel));
  1271. RestoreResults;
  1272. end;
  1273. { tai_wasmstruc_block }
  1274. constructor tai_wasmstruc_block.create_from(a_block_instr: taicpu; srclist: TAsmList);
  1275. var
  1276. Done: Boolean;
  1277. p: tai;
  1278. begin
  1279. wstyp:=aitws_block;
  1280. inherited Create;
  1281. if assigned(a_block_instr.Previous) or assigned(a_block_instr.Next) then
  1282. internalerror(2023100304);
  1283. block_instr:=a_block_instr;
  1284. inner_asmlist:=TAsmList.Create;
  1285. Done:=False;
  1286. repeat
  1287. p:=tai(srclist.First);
  1288. if not assigned(p) then
  1289. internalerror(2023100305);
  1290. if (p.typ=ait_instruction) and (taicpu(p).opcode=a_end_block) then
  1291. begin
  1292. srclist.Remove(p);
  1293. Done:=True;
  1294. end
  1295. else
  1296. inner_asmlist.Concat(wasm_convert_first_item_to_structured(srclist));
  1297. until Done;
  1298. end;
  1299. destructor tai_wasmstruc_block.Destroy;
  1300. begin
  1301. inner_asmlist.free;
  1302. block_instr.free;
  1303. inherited Destroy;
  1304. end;
  1305. function tai_wasmstruc_block.getcopy: TLinkedListItem;
  1306. var
  1307. p: tai_wasmstruc_block;
  1308. begin
  1309. p:=tai_wasmstruc_block(inherited getcopy);
  1310. if assigned(block_instr) then
  1311. p.block_instr:=taicpu(block_instr.getcopy);
  1312. if assigned(inner_asmlist) then
  1313. begin
  1314. p.inner_asmlist:=TAsmList.Create;
  1315. p.inner_asmlist.concatListcopy(inner_asmlist);
  1316. end;
  1317. getcopy:=p;
  1318. end;
  1319. procedure tai_wasmstruc_block.Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);
  1320. begin
  1321. blockstack.push(self);
  1322. map_structured_asmlist_inner(inner_asmlist,f,blockstack);
  1323. blockstack.pop;
  1324. end;
  1325. procedure tai_wasmstruc_block.ConvertToFlatList(l: TAsmList);
  1326. begin
  1327. l.Concat(block_instr);
  1328. block_instr:=nil;
  1329. l.concatList(inner_asmlist);
  1330. l.Concat(taicpu.op_none(a_end_block));
  1331. if FLabelIsNew then
  1332. l.concat(tai_label.create(FLabel));
  1333. end;
  1334. { tai_wasmstruc_loop }
  1335. constructor tai_wasmstruc_loop.create_from(a_loop_instr: taicpu; srclist: TAsmList);
  1336. var
  1337. Done: Boolean;
  1338. p: tai;
  1339. begin
  1340. wstyp:=aitws_loop;
  1341. inherited Create;
  1342. if assigned(a_loop_instr.Previous) or assigned(a_loop_instr.Next) then
  1343. internalerror(2023100306);
  1344. loop_instr:=a_loop_instr;
  1345. inner_asmlist:=TAsmList.Create;
  1346. Done:=False;
  1347. repeat
  1348. p:=tai(srclist.First);
  1349. if not assigned(p) then
  1350. internalerror(2023100307);
  1351. if (p.typ=ait_instruction) and (taicpu(p).opcode=a_end_loop) then
  1352. begin
  1353. srclist.Remove(p);
  1354. Done:=True;
  1355. end
  1356. else
  1357. inner_asmlist.Concat(wasm_convert_first_item_to_structured(srclist));
  1358. until Done;
  1359. end;
  1360. destructor tai_wasmstruc_loop.Destroy;
  1361. begin
  1362. inner_asmlist.free;
  1363. loop_instr.free;
  1364. inherited Destroy;
  1365. end;
  1366. function tai_wasmstruc_loop.getcopy: TLinkedListItem;
  1367. var
  1368. p: tai_wasmstruc_loop;
  1369. begin
  1370. p:=tai_wasmstruc_loop(inherited getcopy);
  1371. if assigned(loop_instr) then
  1372. p.loop_instr:=taicpu(loop_instr.getcopy);
  1373. if assigned(inner_asmlist) then
  1374. begin
  1375. p.inner_asmlist:=TAsmList.Create;
  1376. p.inner_asmlist.concatListcopy(inner_asmlist);
  1377. end;
  1378. getcopy:=p;
  1379. end;
  1380. procedure tai_wasmstruc_loop.Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);
  1381. begin
  1382. blockstack.push(self);
  1383. map_structured_asmlist_inner(inner_asmlist,f,blockstack);
  1384. blockstack.pop;
  1385. end;
  1386. procedure tai_wasmstruc_loop.ConvertToBr(list: TAsmList);
  1387. begin
  1388. list.concat(tai_label.create(GetLabel));
  1389. list.concatList(inner_asmlist);
  1390. list.concat(taicpu.op_sym(a_br,GetLabel));
  1391. end;
  1392. procedure tai_wasmstruc_loop.ConvertToFlatList(l: TAsmList);
  1393. begin
  1394. l.Concat(loop_instr);
  1395. loop_instr:=nil;
  1396. if FLabelIsNew then
  1397. l.concat(tai_label.create(FLabel));
  1398. l.concatList(inner_asmlist);
  1399. l.Concat(taicpu.op_none(a_end_loop));
  1400. end;
  1401. { tai_wasmstruc_legacy_try }
  1402. class function tai_wasmstruc_legacy_try.create_from(srclist: TAsmList): tai_wasmstruc_legacy_try;
  1403. var
  1404. Done: Boolean;
  1405. p: tai;
  1406. tmp_asmlist: TAsmList;
  1407. begin
  1408. result:=nil;
  1409. tmp_asmlist:=TAsmList.Create;
  1410. Done:=False;
  1411. repeat
  1412. p:=tai(srclist.First);
  1413. if not assigned(p) then
  1414. internalerror(2023100308);
  1415. if (p.typ=ait_instruction) and (taicpu(p).opcode in [a_end_legacy_try,a_legacy_catch,a_legacy_catch_all,a_legacy_delegate]) then
  1416. begin
  1417. srclist.Remove(p);
  1418. Done:=True;
  1419. end
  1420. else
  1421. tmp_asmlist.Concat(wasm_convert_first_item_to_structured(srclist));
  1422. until Done;
  1423. case taicpu(p).opcode of
  1424. a_end_legacy_try,a_legacy_catch,a_legacy_catch_all:
  1425. result:=tai_wasmstruc_legacy_try_catch.internal_create(taicpu(p),tmp_asmlist,srclist);
  1426. a_legacy_delegate:
  1427. result:=tai_wasmstruc_legacy_try_delegate.internal_create(taicpu(p),tmp_asmlist,srclist);
  1428. else
  1429. internalerror(2023100502);
  1430. end;
  1431. end;
  1432. constructor tai_wasmstruc_legacy_try.internal_create(a_try_asmlist: TAsmList);
  1433. begin
  1434. inherited Create;
  1435. try_asmlist:=a_try_asmlist;
  1436. end;
  1437. destructor tai_wasmstruc_legacy_try.Destroy;
  1438. begin
  1439. try_asmlist.free;
  1440. inherited Destroy;
  1441. end;
  1442. function tai_wasmstruc_legacy_try.getcopy: TLinkedListItem;
  1443. var
  1444. p: tai_wasmstruc_legacy_try;
  1445. begin
  1446. p:=tai_wasmstruc_legacy_try(inherited getcopy);
  1447. if assigned(try_asmlist) then
  1448. begin
  1449. p.try_asmlist:=TAsmList.Create;
  1450. p.try_asmlist.concatListcopy(try_asmlist);
  1451. end;
  1452. getcopy:=p;
  1453. end;
  1454. procedure tai_wasmstruc_legacy_try.ConvertToFlatList(l: TAsmList);
  1455. begin
  1456. l.Concat(taicpu.op_none(a_legacy_try));
  1457. l.concatList(try_asmlist);
  1458. end;
  1459. { tai_wasmstruc_legacy_try_catch }
  1460. constructor tai_wasmstruc_legacy_try_catch.internal_create(first_ins: taicpu; a_try_asmlist, srclist: TAsmList);
  1461. var
  1462. p: tai;
  1463. procedure parse_next_catch_block;
  1464. var
  1465. new_catch_index: Integer;
  1466. al: TAsmList;
  1467. Done: Boolean;
  1468. pp: tai;
  1469. begin
  1470. SetLength(catch_list,Length(catch_list)+1);
  1471. new_catch_index:=High(catch_list);
  1472. catch_list[new_catch_index].catch_instr:=taicpu(p);
  1473. al:=TAsmList.Create;
  1474. catch_list[new_catch_index].asmlist:=al;
  1475. Done:=False;
  1476. repeat
  1477. pp:=tai(srclist.First);
  1478. if (pp.typ=ait_instruction) and (taicpu(pp).opcode in [a_legacy_catch,a_legacy_catch_all,a_end_legacy_try]) then
  1479. Done:=True
  1480. else
  1481. al.Concat(wasm_convert_first_item_to_structured(srclist));
  1482. until Done;
  1483. end;
  1484. procedure parse_catch_all;
  1485. var
  1486. Done: Boolean;
  1487. pp: tai;
  1488. begin
  1489. catch_all_asmlist:=TAsmList.Create;
  1490. Done:=False;
  1491. repeat
  1492. pp:=tai(srclist.First);
  1493. if (pp.typ=ait_instruction) and (taicpu(pp).opcode=a_end_legacy_try) then
  1494. begin
  1495. srclist.Remove(pp);
  1496. Done:=True;
  1497. end
  1498. else
  1499. catch_all_asmlist.Concat(wasm_convert_first_item_to_structured(srclist));
  1500. until Done;
  1501. end;
  1502. var
  1503. Done: Boolean;
  1504. begin
  1505. wstyp:=aitws_legacy_try_catch;
  1506. inherited internal_create(a_try_asmlist);
  1507. if assigned(first_ins.Previous) or assigned(first_ins.Next) then
  1508. internalerror(2023100310);
  1509. Done:=False;
  1510. p:=first_ins;
  1511. repeat
  1512. if p.typ=ait_instruction then
  1513. case taicpu(p).opcode of
  1514. a_legacy_catch:
  1515. begin
  1516. parse_next_catch_block;
  1517. p:=tai(srclist.First);
  1518. srclist.Remove(p);
  1519. end;
  1520. a_legacy_catch_all:
  1521. begin
  1522. parse_catch_all;
  1523. Done:=True;
  1524. end;
  1525. a_end_legacy_try:
  1526. Done:=True;
  1527. else
  1528. internalerror(2023100311);
  1529. end
  1530. else
  1531. internalerror(2023100312);
  1532. until Done;
  1533. end;
  1534. destructor tai_wasmstruc_legacy_try_catch.Destroy;
  1535. var
  1536. i: Integer;
  1537. begin
  1538. for i:=low(catch_list) to high(catch_list) do
  1539. begin
  1540. catch_list[i].asmlist.free;
  1541. catch_list[i].catch_instr.free;
  1542. end;
  1543. catch_all_asmlist.free;
  1544. inherited Destroy;
  1545. end;
  1546. function tai_wasmstruc_legacy_try_catch.getcopy: TLinkedListItem;
  1547. var
  1548. p: tai_wasmstruc_legacy_try_catch;
  1549. i: Integer;
  1550. begin
  1551. p:=tai_wasmstruc_legacy_try_catch(inherited getcopy);
  1552. p.catch_list:=Copy(catch_list);
  1553. for i:=0 to length(catch_list)-1 do
  1554. begin
  1555. if assigned(catch_list[i].asmlist) then
  1556. begin
  1557. p.catch_list[i].asmlist:=TAsmList.Create;
  1558. p.catch_list[i].asmlist.concatListcopy(catch_list[i].asmlist);
  1559. end;
  1560. if assigned(catch_list[i].catch_instr) then
  1561. p.catch_list[i].catch_instr:=taicpu(catch_list[i].catch_instr.getcopy);
  1562. end;
  1563. if assigned(catch_all_asmlist) then
  1564. begin
  1565. p.catch_all_asmlist:=TAsmList.Create;
  1566. p.catch_all_asmlist.concatListcopy(catch_all_asmlist);
  1567. end;
  1568. getcopy:=p;
  1569. end;
  1570. procedure tai_wasmstruc_legacy_try_catch.Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);
  1571. var
  1572. i: Integer;
  1573. begin
  1574. blockstack.push(self);
  1575. map_structured_asmlist_inner(try_asmlist,f,blockstack);
  1576. for i:=low(catch_list) to high(catch_list) do
  1577. map_structured_asmlist_inner(catch_list[i].asmlist,f,blockstack);
  1578. map_structured_asmlist_inner(catch_all_asmlist,f,blockstack);
  1579. blockstack.pop;
  1580. end;
  1581. procedure tai_wasmstruc_legacy_try_catch.ConvertToFlatList(l: TAsmList);
  1582. var
  1583. i: Integer;
  1584. begin
  1585. inherited ConvertToFlatList(l);
  1586. for i:=low(catch_list) to high(catch_list) do
  1587. begin
  1588. l.Concat(catch_list[i].catch_instr);
  1589. catch_list[i].catch_instr:=nil;
  1590. l.concatList(catch_list[i].asmlist);
  1591. end;
  1592. if assigned(catch_all_asmlist) then
  1593. begin
  1594. l.Concat(taicpu.op_none(a_legacy_catch_all));
  1595. l.concatList(catch_all_asmlist);
  1596. end;
  1597. l.Concat(taicpu.op_none(a_end_legacy_try));
  1598. if FLabelIsNew then
  1599. l.concat(tai_label.create(FLabel));
  1600. end;
  1601. { tai_wasmstruc_try_table }
  1602. constructor tai_wasmstruc_try_table.create_from(a_try_table_instr: taicpu;srclist: TAsmList);
  1603. var
  1604. Done: Boolean;
  1605. p: tai;
  1606. begin
  1607. wstyp:=aitws_block;
  1608. inherited Create;
  1609. if assigned(a_try_table_instr.Previous) or assigned(a_try_table_instr.Next) then
  1610. internalerror(2023100304);
  1611. try_table_instr:=a_try_table_instr;
  1612. inner_asmlist:=TAsmList.Create;
  1613. Done:=False;
  1614. repeat
  1615. p:=tai(srclist.First);
  1616. if not assigned(p) then
  1617. internalerror(2023100305);
  1618. if (p.typ=ait_instruction) and (taicpu(p).opcode=a_end_block) then
  1619. begin
  1620. srclist.Remove(p);
  1621. Done:=True;
  1622. end
  1623. else
  1624. inner_asmlist.Concat(wasm_convert_first_item_to_structured(srclist));
  1625. until Done;
  1626. end;
  1627. destructor tai_wasmstruc_try_table.Destroy;
  1628. begin
  1629. inner_asmlist.free;
  1630. try_table_instr.free;
  1631. inherited Destroy;
  1632. end;
  1633. function tai_wasmstruc_try_table.getcopy: TLinkedListItem;
  1634. var
  1635. p: tai_wasmstruc_try_table;
  1636. begin
  1637. p:=tai_wasmstruc_try_table(inherited getcopy);
  1638. if assigned(try_table_instr) then
  1639. p.try_table_instr:=taicpu(try_table_instr.getcopy);
  1640. if assigned(inner_asmlist) then
  1641. begin
  1642. p.inner_asmlist:=TAsmList.Create;
  1643. p.inner_asmlist.concatListcopy(inner_asmlist);
  1644. end;
  1645. getcopy:=p;
  1646. end;
  1647. procedure tai_wasmstruc_try_table.Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);
  1648. begin
  1649. map_structured_asmlist_inner(try_table_instr.try_table_catch_clauses,f,blockstack);
  1650. blockstack.push(self);
  1651. map_structured_asmlist_inner(inner_asmlist,f,blockstack);
  1652. blockstack.pop;
  1653. end;
  1654. procedure tai_wasmstruc_try_table.ConvertToFlatList(l: TAsmList);
  1655. begin
  1656. l.Concat(try_table_instr);
  1657. try_table_instr:=nil;
  1658. l.concatList(inner_asmlist);
  1659. l.Concat(taicpu.op_none(a_end_try_table));
  1660. if FLabelIsNew then
  1661. l.concat(tai_label.create(FLabel));
  1662. end;
  1663. { tai_wasmstruc_legacy_try_delegate }
  1664. constructor tai_wasmstruc_legacy_try_delegate.internal_create(first_ins: taicpu; a_try_asmlist, srclist: TAsmList);
  1665. begin
  1666. wstyp:=aitws_legacy_try_delegate;
  1667. inherited internal_create(a_try_asmlist);
  1668. if assigned(first_ins.Previous) or assigned(first_ins.Next) then
  1669. internalerror(2023100309);
  1670. delegate_instr:=first_ins;
  1671. end;
  1672. destructor tai_wasmstruc_legacy_try_delegate.Destroy;
  1673. begin
  1674. delegate_instr.free;
  1675. inherited Destroy;
  1676. end;
  1677. function tai_wasmstruc_legacy_try_delegate.getcopy: TLinkedListItem;
  1678. var
  1679. p: tai_wasmstruc_legacy_try_delegate;
  1680. begin
  1681. p:=tai_wasmstruc_legacy_try_delegate(inherited getcopy);
  1682. if assigned(delegate_instr) then
  1683. p.delegate_instr:=taicpu(delegate_instr.getcopy);
  1684. getcopy:=p;
  1685. end;
  1686. procedure tai_wasmstruc_legacy_try_delegate.Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);
  1687. begin
  1688. blockstack.push(self);
  1689. map_structured_asmlist_inner(try_asmlist,f,blockstack);
  1690. blockstack.pop;
  1691. end;
  1692. procedure tai_wasmstruc_legacy_try_delegate.ConvertToFlatList(l: TAsmList);
  1693. begin
  1694. inherited ConvertToFlatList(l);
  1695. l.Concat(delegate_instr);
  1696. delegate_instr:=nil;
  1697. if FLabelIsNew then
  1698. l.concat(tai_label.create(FLabel));
  1699. end;
  1700. { tai_globaltype }
  1701. constructor tai_globaltype.create(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean);
  1702. begin
  1703. inherited Create;
  1704. sym:=TWasmGlobalAsmSymbol(current_asmdata.RefAsmSymbolByClass(TWasmGlobalAsmSymbol,aglobalname,AT_WASM_GLOBAL));
  1705. sym.WasmGlobalType:=atype;
  1706. typ:=ait_globaltype;
  1707. globalname:=aglobalname;
  1708. gtype:=atype;
  1709. immutable:=aimmutable;
  1710. is_external:=true;
  1711. is_global:=false;
  1712. end;
  1713. constructor tai_globaltype.create_local(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
  1714. begin
  1715. inherited Create;
  1716. sym:=TWasmGlobalAsmSymbol(current_asmdata.DefineAsmSymbolByClass(TWasmGlobalAsmSymbol,aglobalname,AB_LOCAL,AT_WASM_GLOBAL,def));
  1717. sym.WasmGlobalType:=atype;
  1718. typ:=ait_globaltype;
  1719. globalname:=aglobalname;
  1720. gtype:=atype;
  1721. immutable:=aimmutable;
  1722. is_external:=false;
  1723. is_global:=false;
  1724. end;
  1725. constructor tai_globaltype.create_global(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
  1726. begin
  1727. inherited Create;
  1728. sym:=TWasmGlobalAsmSymbol(current_asmdata.DefineAsmSymbolByClass(TWasmGlobalAsmSymbol,aglobalname,AB_GLOBAL,AT_WASM_GLOBAL,def));
  1729. sym.WasmGlobalType:=atype;
  1730. typ:=ait_globaltype;
  1731. globalname:=aglobalname;
  1732. gtype:=atype;
  1733. immutable:=aimmutable;
  1734. is_external:=false;
  1735. is_global:=true;
  1736. end;
  1737. { tai_import_name }
  1738. constructor tai_import_name.create(const asymname, aimportname: string);
  1739. begin
  1740. inherited Create;
  1741. typ:=ait_import_name;
  1742. symname:=asymname;
  1743. importname:=aimportname;
  1744. end;
  1745. { tai_import_module }
  1746. constructor tai_import_module.create(const asymname, aimportmodule: string);
  1747. begin
  1748. inherited Create;
  1749. typ:=ait_import_module;
  1750. symname:=asymname;
  1751. importmodule:=aimportmodule;
  1752. end;
  1753. { tai_functype }
  1754. constructor tai_functype.create(const afuncname: string; afunctype: TWasmFuncType; aisforward: Boolean);
  1755. begin
  1756. inherited Create;
  1757. typ:=ait_functype;
  1758. is_forward:=aisforward;
  1759. funcname:=afuncname;
  1760. functype:=afunctype;
  1761. end;
  1762. destructor tai_functype.destroy;
  1763. begin
  1764. functype.free;
  1765. inherited;
  1766. end;
  1767. { tai_tagtype }
  1768. constructor tai_tagtype.create(const atagname: string; aparams: TWasmResultType);
  1769. begin
  1770. typ:=ait_tagtype;
  1771. tagname:=atagname;
  1772. params:=aparams;
  1773. end;
  1774. { tai_local }
  1775. constructor tai_local.create(alocals: TWasmLocalsDynArray);
  1776. begin
  1777. inherited Create;
  1778. locals := Copy(alocals);
  1779. typ := ait_local;
  1780. end;
  1781. procedure tai_local.AddLocal(abasictype: TWasmBasicType);
  1782. begin
  1783. SetLength(locals,Length(locals)+1);
  1784. locals[high(locals)]:=abasictype;
  1785. end;
  1786. procedure tai_local.AddLocals(alocals: TWasmLocalsDynArray);
  1787. begin
  1788. locals:=Concat(locals,alocals);
  1789. end;
  1790. { timpexp_ai }
  1791. constructor tai_export_name.create(const aextname, aintname: ansistring;
  1792. asymtype: timpexptype);
  1793. begin
  1794. inherited create;
  1795. typ := ait_export_name;
  1796. extname := aextname;
  1797. intname := aintname;
  1798. symstype:= asymtype;
  1799. end;
  1800. {*****************************************************************************
  1801. taicpu Constructors
  1802. *****************************************************************************}
  1803. constructor taicpu.Create(op: tasmop);
  1804. begin
  1805. inherited Create(op);
  1806. if not (ts_wasm_threads in current_settings.targetswitches) and is_atomic_op(op) then
  1807. internalerror(2024070701);
  1808. if op=a_try_table then
  1809. try_table_catch_clauses:=TAsmList.Create;
  1810. end;
  1811. destructor taicpu.Destroy;
  1812. begin
  1813. try_table_catch_clauses.free;
  1814. inherited;
  1815. end;
  1816. constructor taicpu.op_none(op : tasmop);
  1817. begin
  1818. create(op);
  1819. end;
  1820. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  1821. begin
  1822. create(op);
  1823. ops:=1;
  1824. {$ifdef EXTDEBUG}
  1825. if getregtype(_op1)=R_INVALIDREGISTER then
  1826. InternalError(2021011901);
  1827. {$endif EXTDEBUG}
  1828. loadreg(0,_op1);
  1829. end;
  1830. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  1831. begin
  1832. create(op);
  1833. ops:=1;
  1834. loadref(0,_op1);
  1835. if op in [a_local_get,a_local_set,a_local_tee] then
  1836. begin
  1837. if (_op1.base<>NR_LOCAL_STACK_POINTER_REG) or (_op1.index<>NR_NO) then
  1838. internalerror(2021010201);
  1839. end
  1840. else
  1841. begin
  1842. if (_op1.base<>NR_NO) or (_op1.index<>NR_NO) then
  1843. internalerror(2021010202);
  1844. end;
  1845. end;
  1846. constructor taicpu.op_const(op : tasmop;_op1 : aint);
  1847. begin
  1848. create(op);
  1849. ops:=1;
  1850. loadconst(0,_op1);
  1851. end;
  1852. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  1853. begin
  1854. create(op);
  1855. ops:=1;
  1856. loadsymbol(0,_op1,0);
  1857. end;
  1858. constructor taicpu.op_sym_const(op: tasmop; _op1: tasmsymbol; _op2: aint);
  1859. begin
  1860. create(op);
  1861. ops:=2;
  1862. loadsymbol(0,_op1,0);
  1863. loadconst(1,_op2);
  1864. end;
  1865. constructor taicpu.op_sym_functype(op : tasmop;_op1 : tasmsymbol;_op2 : TWasmFuncType);
  1866. begin
  1867. create(op);
  1868. ops:=2;
  1869. loadsymbol(0,_op1,0);
  1870. loadfunctype(1,_op2);
  1871. end;
  1872. constructor taicpu.op_single(op: tasmop; _op1: single);
  1873. begin
  1874. create(op);
  1875. ops:=1;
  1876. loadsingle(0,_op1);
  1877. end;
  1878. constructor taicpu.op_double(op: tasmop; _op1: double);
  1879. begin
  1880. create(op);
  1881. ops:=1;
  1882. loaddouble(0,_op1);
  1883. end;
  1884. constructor taicpu.op_functype(op: tasmop; _op1: TWasmFuncType);
  1885. begin
  1886. create(op);
  1887. ops:=1;
  1888. loadfunctype(0,_op1);
  1889. end;
  1890. constructor taicpu.op_catch(op: tasmop; _op1: TCatchArray);
  1891. var
  1892. c: taicpu;
  1893. begin
  1894. create(op);
  1895. for c in _op1 do
  1896. try_table_catch_clauses.Concat(c);
  1897. end;
  1898. procedure taicpu.loadfunctype(opidx: longint; ft: TWasmFuncType);
  1899. begin
  1900. allocate_oper(opidx+1);
  1901. with oper[opidx]^ do
  1902. begin
  1903. if typ<>top_functype then
  1904. clearop(opidx);
  1905. functype:=ft;
  1906. typ:=top_functype;
  1907. end;
  1908. end;
  1909. procedure taicpu.loadsingle(opidx:longint;f:single);
  1910. begin
  1911. allocate_oper(opidx+1);
  1912. with oper[opidx]^ do
  1913. begin
  1914. if typ<>top_single then
  1915. clearop(opidx);
  1916. sval:=f;
  1917. typ:=top_single;
  1918. end;
  1919. end;
  1920. procedure taicpu.loaddouble(opidx: longint; d: double);
  1921. begin
  1922. allocate_oper(opidx+1);
  1923. with oper[opidx]^ do
  1924. begin
  1925. if typ<>top_double then
  1926. clearop(opidx);
  1927. dval:=d;
  1928. typ:=top_double;
  1929. end;
  1930. end;
  1931. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  1932. begin
  1933. result:=false;
  1934. end;
  1935. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  1936. begin
  1937. if opcode in AsmOp_Store then
  1938. result:=operand_write
  1939. else
  1940. result:=operand_read;
  1941. end;
  1942. function taicpu.Pass1(objdata: TObjData): longint;
  1943. function SlebSize(v: tcgint): longint;
  1944. var
  1945. b: byte;
  1946. begin
  1947. result:=0;
  1948. repeat
  1949. b:=byte(v) and 127;
  1950. v:=SarInt64(v,7);
  1951. Inc(result);
  1952. until ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0));
  1953. end;
  1954. function UlebSize(v: tcgint): longint;
  1955. begin
  1956. result:=0;
  1957. repeat
  1958. v:=v shr 7;
  1959. Inc(result);
  1960. until v=0;
  1961. end;
  1962. var
  1963. hp: taicpu;
  1964. begin
  1965. result:=0;
  1966. { Save the old offset and set the new offset }
  1967. InsOffset:=ObjData.CurrObjSec.Size;
  1968. case opcode of
  1969. a_unreachable,
  1970. a_nop,
  1971. a_return,
  1972. a_drop,
  1973. a_i32_eqz,
  1974. a_i32_eq,
  1975. a_i32_ne,
  1976. a_i32_lt_s,
  1977. a_i32_lt_u,
  1978. a_i32_gt_s,
  1979. a_i32_gt_u,
  1980. a_i32_le_s,
  1981. a_i32_le_u,
  1982. a_i32_ge_s,
  1983. a_i32_ge_u,
  1984. a_i64_eqz,
  1985. a_i64_eq,
  1986. a_i64_ne,
  1987. a_i64_lt_s,
  1988. a_i64_lt_u,
  1989. a_i64_gt_s,
  1990. a_i64_gt_u,
  1991. a_i64_le_s,
  1992. a_i64_le_u,
  1993. a_i64_ge_s,
  1994. a_i64_ge_u,
  1995. a_f32_eq,
  1996. a_f32_ne,
  1997. a_f32_lt,
  1998. a_f32_gt,
  1999. a_f32_le,
  2000. a_f32_ge,
  2001. a_f64_eq,
  2002. a_f64_ne,
  2003. a_f64_lt,
  2004. a_f64_gt,
  2005. a_f64_le,
  2006. a_f64_ge,
  2007. a_i32_clz,
  2008. a_i32_ctz,
  2009. a_i32_popcnt,
  2010. a_i32_add,
  2011. a_i32_sub,
  2012. a_i32_mul,
  2013. a_i32_div_s,
  2014. a_i32_div_u,
  2015. a_i32_rem_s,
  2016. a_i32_rem_u,
  2017. a_i32_and,
  2018. a_i32_or,
  2019. a_i32_xor,
  2020. a_i32_shl,
  2021. a_i32_shr_s,
  2022. a_i32_shr_u,
  2023. a_i32_rotl,
  2024. a_i32_rotr,
  2025. a_i64_clz,
  2026. a_i64_ctz,
  2027. a_i64_popcnt,
  2028. a_i64_add,
  2029. a_i64_sub,
  2030. a_i64_mul,
  2031. a_i64_div_s,
  2032. a_i64_div_u,
  2033. a_i64_rem_s,
  2034. a_i64_rem_u,
  2035. a_i64_and,
  2036. a_i64_or,
  2037. a_i64_xor,
  2038. a_i64_shl,
  2039. a_i64_shr_s,
  2040. a_i64_shr_u,
  2041. a_i64_rotl,
  2042. a_i64_rotr,
  2043. a_f32_abs,
  2044. a_f32_neg,
  2045. a_f32_ceil,
  2046. a_f32_floor,
  2047. a_f32_trunc,
  2048. a_f32_nearest,
  2049. a_f32_sqrt,
  2050. a_f32_add,
  2051. a_f32_sub,
  2052. a_f32_mul,
  2053. a_f32_div,
  2054. a_f32_min,
  2055. a_f32_max,
  2056. a_f32_copysign,
  2057. a_f64_abs,
  2058. a_f64_neg,
  2059. a_f64_ceil,
  2060. a_f64_floor,
  2061. a_f64_trunc,
  2062. a_f64_nearest,
  2063. a_f64_sqrt,
  2064. a_f64_add,
  2065. a_f64_sub,
  2066. a_f64_mul,
  2067. a_f64_div,
  2068. a_f64_min,
  2069. a_f64_max,
  2070. a_f64_copysign,
  2071. a_i32_wrap_i64,
  2072. a_i32_trunc_f32_s,
  2073. a_i32_trunc_f32_u,
  2074. a_i32_trunc_f64_s,
  2075. a_i32_trunc_f64_u,
  2076. a_i64_extend_i32_s,
  2077. a_i64_extend_i32_u,
  2078. a_i64_trunc_f32_s,
  2079. a_i64_trunc_f32_u,
  2080. a_i64_trunc_f64_s,
  2081. a_i64_trunc_f64_u,
  2082. a_f32_convert_i32_s,
  2083. a_f32_convert_i32_u,
  2084. a_f32_convert_i64_s,
  2085. a_f32_convert_i64_u,
  2086. a_f32_demote_f64,
  2087. a_f64_convert_i32_s,
  2088. a_f64_convert_i32_u,
  2089. a_f64_convert_i64_s,
  2090. a_f64_convert_i64_u,
  2091. a_f64_promote_f32,
  2092. a_i32_reinterpret_f32,
  2093. a_i64_reinterpret_f64,
  2094. a_f32_reinterpret_i32,
  2095. a_f64_reinterpret_i64,
  2096. a_i32_extend8_s,
  2097. a_i32_extend16_s,
  2098. a_i64_extend8_s,
  2099. a_i64_extend16_s,
  2100. a_i64_extend32_s,
  2101. a_throw_ref,
  2102. a_else,
  2103. a_end_block,
  2104. a_end_if,
  2105. a_end_loop,
  2106. a_end_try_table,
  2107. a_end_legacy_try,
  2108. a_end_function,
  2109. a_legacy_catch_all,
  2110. a_ref_is_null:
  2111. result:=1;
  2112. a_i32_trunc_sat_f32_s,
  2113. a_i32_trunc_sat_f32_u,
  2114. a_i32_trunc_sat_f64_s,
  2115. a_i32_trunc_sat_f64_u,
  2116. a_i64_trunc_sat_f32_s,
  2117. a_i64_trunc_sat_f32_u,
  2118. a_i64_trunc_sat_f64_s,
  2119. a_i64_trunc_sat_f64_u,
  2120. a_memory_size,
  2121. a_memory_grow,
  2122. a_ref_null_funcref,
  2123. a_ref_null_externref:
  2124. result:=2;
  2125. a_memory_copy:
  2126. result:=4;
  2127. a_memory_fill,
  2128. a_atomic_fence:
  2129. result:=3;
  2130. a_i32_const:
  2131. begin
  2132. if ops<>1 then
  2133. internalerror(2021092001);
  2134. with oper[0]^ do
  2135. case typ of
  2136. top_ref:
  2137. begin
  2138. if assigned(ref^.symbol) then
  2139. result:=6
  2140. else
  2141. begin
  2142. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  2143. internalerror(2021092018);
  2144. result:=1+SlebSize(longint(ref^.offset));
  2145. end;
  2146. end;
  2147. top_const:
  2148. result:=1+SlebSize(longint(val));
  2149. else
  2150. internalerror(2021092615);
  2151. end;
  2152. end;
  2153. a_i64_const:
  2154. begin
  2155. if ops<>1 then
  2156. internalerror(2021092001);
  2157. with oper[0]^ do
  2158. case typ of
  2159. top_ref:
  2160. begin
  2161. if assigned(ref^.symbol) then
  2162. result:=6
  2163. else
  2164. begin
  2165. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  2166. internalerror(2021092018);
  2167. result:=1+SlebSize(int64(ref^.offset));
  2168. end;
  2169. end;
  2170. top_const:
  2171. result:=1+SlebSize(int64(val));
  2172. else
  2173. internalerror(2021092615);
  2174. end;
  2175. end;
  2176. a_f32_const:
  2177. result:=5;
  2178. a_f64_const:
  2179. result:=9;
  2180. a_local_get,
  2181. a_local_set,
  2182. a_local_tee:
  2183. begin
  2184. if ops<>1 then
  2185. internalerror(2021092001);
  2186. with oper[0]^ do
  2187. case typ of
  2188. top_ref:
  2189. begin
  2190. if assigned(ref^.symbol) then
  2191. internalerror(2021092005);
  2192. if ref^.base<>NR_STACK_POINTER_REG then
  2193. internalerror(2021092006);
  2194. if ref^.index<>NR_NO then
  2195. internalerror(2021092007);
  2196. result:=1+UlebSize(ref^.offset);
  2197. end;
  2198. top_const:
  2199. result:=1+UlebSize(val);
  2200. else
  2201. internalerror(2021092008);
  2202. end;
  2203. end;
  2204. a_global_get,
  2205. a_global_set:
  2206. begin
  2207. if ops<>1 then
  2208. internalerror(2021092010);
  2209. with oper[0]^ do
  2210. case typ of
  2211. top_ref:
  2212. begin
  2213. if not assigned(ref^.symbol) then
  2214. internalerror(2021092012);
  2215. if (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  2216. internalerror(2021092013);
  2217. result:=6;
  2218. end;
  2219. else
  2220. internalerror(2021092011);
  2221. end;
  2222. end;
  2223. a_block,
  2224. a_loop,
  2225. a_if,
  2226. a_legacy_try,
  2227. a_try_table:
  2228. begin
  2229. if ops=0 then
  2230. result:=2
  2231. else
  2232. begin
  2233. if ops<>1 then
  2234. internalerror(2021092015);
  2235. with oper[0]^ do
  2236. case typ of
  2237. top_functype:
  2238. begin
  2239. if (length(functype.params)=0) and (length(functype.results)<=1) then
  2240. result:=2
  2241. else
  2242. { more complex blocktypes are not yet implemented }
  2243. internalerror(2021092621);
  2244. end;
  2245. else
  2246. internalerror(2021092620);
  2247. end;
  2248. end;
  2249. if opcode=a_try_table then
  2250. begin
  2251. Inc(result,UlebSize(try_table_catch_clauses.Count));
  2252. hp:=taicpu(try_table_catch_clauses.First);
  2253. while Assigned(hp) do
  2254. begin
  2255. case hp.opcode of
  2256. a_catch,
  2257. a_catch_ref:
  2258. begin
  2259. Inc(result);
  2260. if hp.ops<>2 then
  2261. internalerror(2025100508);
  2262. with hp.oper[0]^ do
  2263. case typ of
  2264. top_ref:
  2265. begin
  2266. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  2267. internalerror(2025100509);
  2268. Inc(result,5);
  2269. end;
  2270. else
  2271. internalerror(2025100510);
  2272. end;
  2273. with hp.oper[1]^ do
  2274. case typ of
  2275. top_const:
  2276. Inc(result,UlebSize(val));
  2277. else
  2278. internalerror(2025100511);
  2279. end;
  2280. end;
  2281. a_catch_all,
  2282. a_catch_all_ref:
  2283. begin
  2284. Inc(result);
  2285. if hp.ops<>1 then
  2286. internalerror(2025100512);
  2287. with hp.oper[0]^ do
  2288. case typ of
  2289. top_const:
  2290. Inc(result,UlebSize(val));
  2291. else
  2292. internalerror(2025100513);
  2293. end;
  2294. end;
  2295. else
  2296. internalerror(2025100514);
  2297. end;
  2298. hp:=taicpu(hp.Next);
  2299. end;
  2300. end;
  2301. end;
  2302. a_i32_load,
  2303. a_i64_load,
  2304. a_f32_load,
  2305. a_f64_load,
  2306. a_i32_load8_s,
  2307. a_i32_load8_u,
  2308. a_i32_load16_s,
  2309. a_i32_load16_u,
  2310. a_i64_load8_s,
  2311. a_i64_load8_u,
  2312. a_i64_load16_s,
  2313. a_i64_load16_u,
  2314. a_i64_load32_s,
  2315. a_i64_load32_u,
  2316. a_i32_store,
  2317. a_i64_store,
  2318. a_f32_store,
  2319. a_f64_store,
  2320. a_i32_store8,
  2321. a_i32_store16,
  2322. a_i64_store8,
  2323. a_i64_store16,
  2324. a_i64_store32:
  2325. begin
  2326. if ops<>1 then
  2327. internalerror(2021092016);
  2328. with oper[0]^ do
  2329. case typ of
  2330. top_ref:
  2331. begin
  2332. if assigned(ref^.symbol) then
  2333. begin
  2334. Result:=1+
  2335. UlebSize(natural_alignment_for_load_store(opcode))+
  2336. 5; { relocation, fixed size = 5 bytes }
  2337. end
  2338. else
  2339. begin
  2340. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  2341. internalerror(2021092018);
  2342. Result:=1+
  2343. UlebSize(natural_alignment_for_load_store(opcode))+
  2344. UlebSize(ref^.offset);
  2345. end;
  2346. end;
  2347. top_const:
  2348. begin
  2349. Result:=1+
  2350. UlebSize(natural_alignment_for_load_store(opcode))+
  2351. UlebSize(val);
  2352. end;
  2353. else
  2354. internalerror(2021092017);
  2355. end;
  2356. end;
  2357. a_memory_atomic_notify,
  2358. a_memory_atomic_wait32,
  2359. a_memory_atomic_wait64,
  2360. a_i32_atomic_load,
  2361. a_i64_atomic_load,
  2362. a_i32_atomic_load8_u,
  2363. a_i32_atomic_load16_u,
  2364. a_i64_atomic_load8_u,
  2365. a_i64_atomic_load16_u,
  2366. a_i64_atomic_load32_u,
  2367. a_i32_atomic_store,
  2368. a_i64_atomic_store,
  2369. a_i32_atomic_store8,
  2370. a_i32_atomic_store16,
  2371. a_i64_atomic_store8,
  2372. a_i64_atomic_store16,
  2373. a_i64_atomic_store32,
  2374. a_i32_atomic_rmw_add,
  2375. a_i64_atomic_rmw_add,
  2376. a_i32_atomic_rmw8_add_u,
  2377. a_i32_atomic_rmw16_add_u,
  2378. a_i64_atomic_rmw8_add_u,
  2379. a_i64_atomic_rmw16_add_u,
  2380. a_i64_atomic_rmw32_add_u,
  2381. a_i32_atomic_rmw_sub,
  2382. a_i64_atomic_rmw_sub,
  2383. a_i32_atomic_rmw8_sub_u,
  2384. a_i32_atomic_rmw16_sub_u,
  2385. a_i64_atomic_rmw8_sub_u,
  2386. a_i64_atomic_rmw16_sub_u,
  2387. a_i64_atomic_rmw32_sub_u,
  2388. a_i32_atomic_rmw_and,
  2389. a_i64_atomic_rmw_and,
  2390. a_i32_atomic_rmw8_and_u,
  2391. a_i32_atomic_rmw16_and_u,
  2392. a_i64_atomic_rmw8_and_u,
  2393. a_i64_atomic_rmw16_and_u,
  2394. a_i64_atomic_rmw32_and_u,
  2395. a_i32_atomic_rmw_or,
  2396. a_i64_atomic_rmw_or,
  2397. a_i32_atomic_rmw8_or_u,
  2398. a_i32_atomic_rmw16_or_u,
  2399. a_i64_atomic_rmw8_or_u,
  2400. a_i64_atomic_rmw16_or_u,
  2401. a_i64_atomic_rmw32_or_u,
  2402. a_i32_atomic_rmw_xor,
  2403. a_i64_atomic_rmw_xor,
  2404. a_i32_atomic_rmw8_xor_u,
  2405. a_i32_atomic_rmw16_xor_u,
  2406. a_i64_atomic_rmw8_xor_u,
  2407. a_i64_atomic_rmw16_xor_u,
  2408. a_i64_atomic_rmw32_xor_u,
  2409. a_i32_atomic_rmw_xchg,
  2410. a_i64_atomic_rmw_xchg,
  2411. a_i32_atomic_rmw8_xchg_u,
  2412. a_i32_atomic_rmw16_xchg_u,
  2413. a_i64_atomic_rmw8_xchg_u,
  2414. a_i64_atomic_rmw16_xchg_u,
  2415. a_i64_atomic_rmw32_xchg_u,
  2416. a_i32_atomic_rmw_cmpxchg,
  2417. a_i64_atomic_rmw_cmpxchg,
  2418. a_i32_atomic_rmw8_cmpxchg_u,
  2419. a_i32_atomic_rmw16_cmpxchg_u,
  2420. a_i64_atomic_rmw8_cmpxchg_u,
  2421. a_i64_atomic_rmw16_cmpxchg_u,
  2422. a_i64_atomic_rmw32_cmpxchg_u:
  2423. begin
  2424. if ops<>1 then
  2425. internalerror(2021092016);
  2426. with oper[0]^ do
  2427. case typ of
  2428. top_ref:
  2429. begin
  2430. if assigned(ref^.symbol) then
  2431. begin
  2432. Result:=2+
  2433. UlebSize(natural_alignment_for_load_store(opcode))+
  2434. 5; { relocation, fixed size = 5 bytes }
  2435. end
  2436. else
  2437. begin
  2438. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  2439. internalerror(2021092018);
  2440. Result:=2+
  2441. UlebSize(natural_alignment_for_load_store(opcode))+
  2442. UlebSize(ref^.offset);
  2443. end;
  2444. end;
  2445. top_const:
  2446. begin
  2447. Result:=2+
  2448. UlebSize(natural_alignment_for_load_store(opcode))+
  2449. UlebSize(val);
  2450. end;
  2451. else
  2452. internalerror(2021092017);
  2453. end;
  2454. end;
  2455. a_call:
  2456. begin
  2457. if ops<>2 then
  2458. internalerror(2021092021);
  2459. with oper[0]^ do
  2460. case typ of
  2461. top_ref:
  2462. begin
  2463. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  2464. internalerror(2021092023);
  2465. result:=6;
  2466. end;
  2467. else
  2468. internalerror(2021092022);
  2469. end;
  2470. end;
  2471. a_call_indirect:
  2472. begin
  2473. if ops<>1 then
  2474. internalerror(2021092610);
  2475. with oper[0]^ do
  2476. case typ of
  2477. top_functype:
  2478. begin
  2479. TWasmObjData(objdata).FuncTypes.AddOrGetFuncType(functype);
  2480. result:=6+
  2481. UlebSize(0);
  2482. end;
  2483. else
  2484. internalerror(2021092611);
  2485. end;
  2486. end;
  2487. a_br,
  2488. a_br_if,
  2489. a_legacy_rethrow,
  2490. a_legacy_delegate:
  2491. begin
  2492. if ops<>1 then
  2493. internalerror(2021092610);
  2494. with oper[0]^ do
  2495. case typ of
  2496. top_const:
  2497. result:=1+
  2498. UlebSize(val);
  2499. else
  2500. internalerror(2021092625);
  2501. end;
  2502. end;
  2503. a_legacy_catch,
  2504. a_legacy_throw,
  2505. a_throw:
  2506. begin
  2507. if ops<>1 then
  2508. internalerror(2021092709);
  2509. with oper[0]^ do
  2510. case typ of
  2511. top_ref:
  2512. begin
  2513. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  2514. internalerror(2021092711);
  2515. result:=6;
  2516. end;
  2517. else
  2518. internalerror(2021092710);
  2519. end;
  2520. end;
  2521. a_memory_init:
  2522. begin
  2523. if ops<>1 then
  2524. internalerror(2022052802);
  2525. with oper[0]^ do
  2526. case typ of
  2527. top_const:
  2528. result:=3+UlebSize(val);
  2529. else
  2530. internalerror(2022052803);
  2531. end;
  2532. end;
  2533. a_data_drop:
  2534. begin
  2535. if ops<>1 then
  2536. internalerror(2022052804);
  2537. with oper[0]^ do
  2538. case typ of
  2539. top_const:
  2540. result:=2+UlebSize(val);
  2541. else
  2542. internalerror(2022052805);
  2543. end;
  2544. end;
  2545. else
  2546. internalerror(2021092623);
  2547. end;
  2548. end;
  2549. procedure taicpu.Pass2(objdata: TObjData);
  2550. procedure WriteByte(b: byte);
  2551. begin
  2552. objdata.writebytes(b,1);
  2553. end;
  2554. {$ifdef FPC_LITTLE_ENDIAN}
  2555. procedure WriteSingle(s: single);
  2556. begin
  2557. objdata.writebytes(s,4);
  2558. end;
  2559. procedure WriteDouble(d: double);
  2560. begin
  2561. objdata.writebytes(d,8);
  2562. end;
  2563. {$else FPC_LITTLE_ENDIAN}
  2564. procedure WriteSingle(s: single);
  2565. var
  2566. l: longword;
  2567. begin
  2568. Move(s,l,4);
  2569. l:=SwapEndian(l);
  2570. objdata.writebytes(l,4);
  2571. end;
  2572. procedure WriteDouble(d: double);
  2573. var
  2574. q: qword;
  2575. begin
  2576. Move(d,q,8);
  2577. q:=SwapEndian(q);
  2578. objdata.writebytes(q,8);
  2579. end;
  2580. {$endif FPC_LITTLE_ENDIAN}
  2581. procedure WriteSleb(v: tcgint);
  2582. var
  2583. b: byte;
  2584. Done: Boolean=false;
  2585. begin
  2586. repeat
  2587. b:=byte(v) and 127;
  2588. v:=SarInt64(v,7);
  2589. if ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0)) then
  2590. Done:=true
  2591. else
  2592. b:=b or 128;
  2593. objdata.writebytes(b,1);
  2594. until Done;
  2595. end;
  2596. procedure WriteUleb(v: tcgint);
  2597. var
  2598. b: byte;
  2599. begin
  2600. repeat
  2601. b:=byte(v) and 127;
  2602. v:=v shr 7;
  2603. if v<>0 then
  2604. b:=b or 128;
  2605. objdata.writebytes(b,1);
  2606. until v=0;
  2607. end;
  2608. var
  2609. hp: taicpu;
  2610. begin
  2611. { safety check }
  2612. if objdata.currobjsec.size<>longword(insoffset) then
  2613. internalerror(200130121);
  2614. case opcode of
  2615. a_unreachable:
  2616. WriteByte($00);
  2617. a_nop:
  2618. WriteByte($01);
  2619. a_return:
  2620. WriteByte($0F);
  2621. a_drop:
  2622. WriteByte($1A);
  2623. a_memory_size:
  2624. begin
  2625. WriteByte($3F);
  2626. WriteByte($00);
  2627. end;
  2628. a_memory_grow:
  2629. begin
  2630. WriteByte($40);
  2631. WriteByte($00);
  2632. end;
  2633. a_memory_copy:
  2634. begin
  2635. WriteByte($FC);
  2636. WriteUleb(10);
  2637. WriteByte($00);
  2638. WriteByte($00);
  2639. end;
  2640. a_memory_fill:
  2641. begin
  2642. WriteByte($FC);
  2643. WriteUleb(11);
  2644. WriteByte($00);
  2645. end;
  2646. a_atomic_fence:
  2647. begin
  2648. WriteByte($FE);
  2649. WriteByte($03);
  2650. WriteByte($00);
  2651. end;
  2652. a_i32_eqz:
  2653. WriteByte($45);
  2654. a_i32_eq:
  2655. WriteByte($46);
  2656. a_i32_ne:
  2657. WriteByte($47);
  2658. a_i32_lt_s:
  2659. WriteByte($48);
  2660. a_i32_lt_u:
  2661. WriteByte($49);
  2662. a_i32_gt_s:
  2663. WriteByte($4A);
  2664. a_i32_gt_u:
  2665. WriteByte($4B);
  2666. a_i32_le_s:
  2667. WriteByte($4C);
  2668. a_i32_le_u:
  2669. WriteByte($4D);
  2670. a_i32_ge_s:
  2671. WriteByte($4E);
  2672. a_i32_ge_u:
  2673. WriteByte($4F);
  2674. a_i64_eqz:
  2675. WriteByte($50);
  2676. a_i64_eq:
  2677. WriteByte($51);
  2678. a_i64_ne:
  2679. WriteByte($52);
  2680. a_i64_lt_s:
  2681. WriteByte($53);
  2682. a_i64_lt_u:
  2683. WriteByte($54);
  2684. a_i64_gt_s:
  2685. WriteByte($55);
  2686. a_i64_gt_u:
  2687. WriteByte($56);
  2688. a_i64_le_s:
  2689. WriteByte($57);
  2690. a_i64_le_u:
  2691. WriteByte($58);
  2692. a_i64_ge_s:
  2693. WriteByte($59);
  2694. a_i64_ge_u:
  2695. WriteByte($5A);
  2696. a_f32_eq:
  2697. WriteByte($5B);
  2698. a_f32_ne:
  2699. WriteByte($5C);
  2700. a_f32_lt:
  2701. WriteByte($5D);
  2702. a_f32_gt:
  2703. WriteByte($5E);
  2704. a_f32_le:
  2705. WriteByte($5F);
  2706. a_f32_ge:
  2707. WriteByte($60);
  2708. a_f64_eq:
  2709. WriteByte($61);
  2710. a_f64_ne:
  2711. WriteByte($62);
  2712. a_f64_lt:
  2713. WriteByte($63);
  2714. a_f64_gt:
  2715. WriteByte($64);
  2716. a_f64_le:
  2717. WriteByte($65);
  2718. a_f64_ge:
  2719. WriteByte($66);
  2720. a_i32_clz:
  2721. WriteByte($67);
  2722. a_i32_ctz:
  2723. WriteByte($68);
  2724. a_i32_popcnt:
  2725. WriteByte($69);
  2726. a_i32_add:
  2727. WriteByte($6A);
  2728. a_i32_sub:
  2729. WriteByte($6B);
  2730. a_i32_mul:
  2731. WriteByte($6C);
  2732. a_i32_div_s:
  2733. WriteByte($6D);
  2734. a_i32_div_u:
  2735. WriteByte($6E);
  2736. a_i32_rem_s:
  2737. WriteByte($6F);
  2738. a_i32_rem_u:
  2739. WriteByte($70);
  2740. a_i32_and:
  2741. WriteByte($71);
  2742. a_i32_or:
  2743. WriteByte($72);
  2744. a_i32_xor:
  2745. WriteByte($73);
  2746. a_i32_shl:
  2747. WriteByte($74);
  2748. a_i32_shr_s:
  2749. WriteByte($75);
  2750. a_i32_shr_u:
  2751. WriteByte($76);
  2752. a_i32_rotl:
  2753. WriteByte($77);
  2754. a_i32_rotr:
  2755. WriteByte($78);
  2756. a_i64_clz:
  2757. WriteByte($79);
  2758. a_i64_ctz:
  2759. WriteByte($7A);
  2760. a_i64_popcnt:
  2761. WriteByte($7B);
  2762. a_i64_add:
  2763. WriteByte($7C);
  2764. a_i64_sub:
  2765. WriteByte($7D);
  2766. a_i64_mul:
  2767. WriteByte($7E);
  2768. a_i64_div_s:
  2769. WriteByte($7F);
  2770. a_i64_div_u:
  2771. WriteByte($80);
  2772. a_i64_rem_s:
  2773. WriteByte($81);
  2774. a_i64_rem_u:
  2775. WriteByte($82);
  2776. a_i64_and:
  2777. WriteByte($83);
  2778. a_i64_or:
  2779. WriteByte($84);
  2780. a_i64_xor:
  2781. WriteByte($85);
  2782. a_i64_shl:
  2783. WriteByte($86);
  2784. a_i64_shr_s:
  2785. WriteByte($87);
  2786. a_i64_shr_u:
  2787. WriteByte($88);
  2788. a_i64_rotl:
  2789. WriteByte($89);
  2790. a_i64_rotr:
  2791. WriteByte($8A);
  2792. a_f32_abs:
  2793. WriteByte($8B);
  2794. a_f32_neg:
  2795. WriteByte($8C);
  2796. a_f32_ceil:
  2797. WriteByte($8D);
  2798. a_f32_floor:
  2799. WriteByte($8E);
  2800. a_f32_trunc:
  2801. WriteByte($8F);
  2802. a_f32_nearest:
  2803. WriteByte($90);
  2804. a_f32_sqrt:
  2805. WriteByte($91);
  2806. a_f32_add:
  2807. WriteByte($92);
  2808. a_f32_sub:
  2809. WriteByte($93);
  2810. a_f32_mul:
  2811. WriteByte($94);
  2812. a_f32_div:
  2813. WriteByte($95);
  2814. a_f32_min:
  2815. WriteByte($96);
  2816. a_f32_max:
  2817. WriteByte($97);
  2818. a_f32_copysign:
  2819. WriteByte($98);
  2820. a_f64_abs:
  2821. WriteByte($99);
  2822. a_f64_neg:
  2823. WriteByte($9A);
  2824. a_f64_ceil:
  2825. WriteByte($9B);
  2826. a_f64_floor:
  2827. WriteByte($9C);
  2828. a_f64_trunc:
  2829. WriteByte($9D);
  2830. a_f64_nearest:
  2831. WriteByte($9E);
  2832. a_f64_sqrt:
  2833. WriteByte($9F);
  2834. a_f64_add:
  2835. WriteByte($A0);
  2836. a_f64_sub:
  2837. WriteByte($A1);
  2838. a_f64_mul:
  2839. WriteByte($A2);
  2840. a_f64_div:
  2841. WriteByte($A3);
  2842. a_f64_min:
  2843. WriteByte($A4);
  2844. a_f64_max:
  2845. WriteByte($A5);
  2846. a_f64_copysign:
  2847. WriteByte($A6);
  2848. a_i32_wrap_i64:
  2849. WriteByte($A7);
  2850. a_i32_trunc_f32_s:
  2851. WriteByte($A8);
  2852. a_i32_trunc_f32_u:
  2853. WriteByte($A9);
  2854. a_i32_trunc_f64_s:
  2855. WriteByte($AA);
  2856. a_i32_trunc_f64_u:
  2857. WriteByte($AB);
  2858. a_i64_extend_i32_s:
  2859. WriteByte($AC);
  2860. a_i64_extend_i32_u:
  2861. WriteByte($AD);
  2862. a_i64_trunc_f32_s:
  2863. WriteByte($AE);
  2864. a_i64_trunc_f32_u:
  2865. WriteByte($AF);
  2866. a_i64_trunc_f64_s:
  2867. WriteByte($B0);
  2868. a_i64_trunc_f64_u:
  2869. WriteByte($B1);
  2870. a_f32_convert_i32_s:
  2871. WriteByte($B2);
  2872. a_f32_convert_i32_u:
  2873. WriteByte($B3);
  2874. a_f32_convert_i64_s:
  2875. WriteByte($B4);
  2876. a_f32_convert_i64_u:
  2877. WriteByte($B5);
  2878. a_f32_demote_f64:
  2879. WriteByte($B6);
  2880. a_f64_convert_i32_s:
  2881. WriteByte($B7);
  2882. a_f64_convert_i32_u:
  2883. WriteByte($B8);
  2884. a_f64_convert_i64_s:
  2885. WriteByte($B9);
  2886. a_f64_convert_i64_u:
  2887. WriteByte($BA);
  2888. a_f64_promote_f32:
  2889. WriteByte($BB);
  2890. a_i32_reinterpret_f32:
  2891. WriteByte($BC);
  2892. a_i64_reinterpret_f64:
  2893. WriteByte($BD);
  2894. a_f32_reinterpret_i32:
  2895. WriteByte($BE);
  2896. a_f64_reinterpret_i64:
  2897. WriteByte($BF);
  2898. a_i32_extend8_s:
  2899. WriteByte($C0);
  2900. a_i32_extend16_s:
  2901. WriteByte($C1);
  2902. a_i64_extend8_s:
  2903. WriteByte($C2);
  2904. a_i64_extend16_s:
  2905. WriteByte($C3);
  2906. a_i64_extend32_s:
  2907. WriteByte($C4);
  2908. a_end_block,
  2909. a_end_if,
  2910. a_end_loop,
  2911. a_end_try_table,
  2912. a_end_legacy_try,
  2913. a_end_function:
  2914. WriteByte($0B);
  2915. a_legacy_catch_all:
  2916. WriteByte($19);
  2917. a_throw_ref:
  2918. WriteByte($0A);
  2919. a_i32_const:
  2920. begin
  2921. WriteByte($41);
  2922. if ops<>1 then
  2923. internalerror(2021092001);
  2924. with oper[0]^ do
  2925. case typ of
  2926. top_ref:
  2927. begin
  2928. if assigned(ref^.symbol) then
  2929. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB)
  2930. else
  2931. begin
  2932. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  2933. internalerror(2021092018);
  2934. WriteSleb(longint(ref^.offset));
  2935. end;
  2936. end;
  2937. top_const:
  2938. WriteSleb(longint(val));
  2939. else
  2940. internalerror(2021092615);
  2941. end;
  2942. end;
  2943. a_i64_const:
  2944. begin
  2945. WriteByte($42);
  2946. if ops<>1 then
  2947. internalerror(2021092001);
  2948. with oper[0]^ do
  2949. case typ of
  2950. top_ref:
  2951. begin
  2952. if assigned(ref^.symbol) then
  2953. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB)
  2954. else
  2955. begin
  2956. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  2957. internalerror(2021092018);
  2958. WriteSleb(int64(ref^.offset));
  2959. end;
  2960. end;
  2961. top_const:
  2962. WriteSleb(int64(val));
  2963. else
  2964. internalerror(2021092615);
  2965. end;
  2966. end;
  2967. a_f32_const:
  2968. begin
  2969. if ops<>1 then
  2970. internalerror(2021092619);
  2971. WriteByte($43);
  2972. with oper[0]^ do
  2973. case typ of
  2974. top_single:
  2975. WriteSingle(sval);
  2976. else
  2977. internalerror(2021092618);
  2978. end;
  2979. end;
  2980. a_f64_const:
  2981. begin
  2982. if ops<>1 then
  2983. internalerror(2021092616);
  2984. WriteByte($44);
  2985. with oper[0]^ do
  2986. case typ of
  2987. top_double:
  2988. WriteDouble(dval);
  2989. else
  2990. internalerror(2021092617);
  2991. end;
  2992. end;
  2993. a_local_get,
  2994. a_local_set,
  2995. a_local_tee:
  2996. begin
  2997. case opcode of
  2998. a_local_get:
  2999. WriteByte($20);
  3000. a_local_set:
  3001. WriteByte($21);
  3002. a_local_tee:
  3003. WriteByte($22);
  3004. else
  3005. internalerror(2021092003);
  3006. end;
  3007. if ops<>1 then
  3008. internalerror(2021092004);
  3009. with oper[0]^ do
  3010. case typ of
  3011. top_ref:
  3012. begin
  3013. if assigned(ref^.symbol) then
  3014. internalerror(2021092005);
  3015. if ref^.base<>NR_STACK_POINTER_REG then
  3016. internalerror(2021092006);
  3017. if ref^.index<>NR_NO then
  3018. internalerror(2021092007);
  3019. WriteUleb(ref^.offset);
  3020. end;
  3021. top_const:
  3022. WriteUleb(val);
  3023. else
  3024. internalerror(2021092008);
  3025. end;
  3026. end;
  3027. a_global_get,
  3028. a_global_set:
  3029. begin
  3030. case opcode of
  3031. a_global_get:
  3032. WriteByte($23);
  3033. a_global_set:
  3034. WriteByte($24);
  3035. else
  3036. internalerror(2021092009);
  3037. end;
  3038. if ops<>1 then
  3039. internalerror(2021092010);
  3040. with oper[0]^ do
  3041. case typ of
  3042. top_ref:
  3043. begin
  3044. if not assigned(ref^.symbol) then
  3045. internalerror(2021092012);
  3046. if (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  3047. internalerror(2021092013);
  3048. objdata.writeReloc(0,5,TWasmObjData(ObjData).globalref(ref^.symbol),RELOC_GLOBAL_INDEX_LEB);
  3049. end;
  3050. else
  3051. internalerror(2021092011);
  3052. end;
  3053. end;
  3054. a_block,
  3055. a_loop,
  3056. a_if,
  3057. a_legacy_try,
  3058. a_try_table:
  3059. begin
  3060. case opcode of
  3061. a_block:
  3062. WriteByte($02);
  3063. a_loop:
  3064. WriteByte($03);
  3065. a_if:
  3066. WriteByte($04);
  3067. a_legacy_try:
  3068. WriteByte($06);
  3069. a_try_table:
  3070. WriteByte($1F);
  3071. else
  3072. internalerror(2021092626);
  3073. end;
  3074. if ops=0 then
  3075. WriteByte($40)
  3076. else
  3077. begin
  3078. if ops<>1 then
  3079. internalerror(2021092015);
  3080. with oper[0]^ do
  3081. case typ of
  3082. top_functype:
  3083. begin
  3084. if (length(functype.params)=0) and (length(functype.results)<=1) then
  3085. begin
  3086. if length(functype.results)=1 then
  3087. WriteByte(encode_wasm_basic_type(functype.results[0]))
  3088. else
  3089. WriteByte($40);
  3090. end
  3091. else
  3092. { more complex blocktypes are not yet implemented }
  3093. internalerror(2021092621);
  3094. end;
  3095. else
  3096. internalerror(2021092620);
  3097. end;
  3098. end;
  3099. if opcode=a_try_table then
  3100. begin
  3101. WriteUleb(try_table_catch_clauses.Count);
  3102. hp:=taicpu(try_table_catch_clauses.First);
  3103. while Assigned(hp) do
  3104. begin
  3105. case hp.opcode of
  3106. a_catch:
  3107. WriteByte($00);
  3108. a_catch_ref:
  3109. WriteByte($01);
  3110. a_catch_all:
  3111. WriteByte($02);
  3112. a_catch_all_ref:
  3113. WriteByte($03);
  3114. else
  3115. internalerror(2025100501);
  3116. end;
  3117. if hp.opcode in [a_catch,a_catch_ref] then
  3118. begin
  3119. if hp.ops<>2 then
  3120. internalerror(2025100502);
  3121. with hp.oper[0]^ do
  3122. case typ of
  3123. top_ref:
  3124. begin
  3125. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  3126. internalerror(2025100503);
  3127. objdata.writeReloc(0,5,TWasmObjData(ObjData).ExceptionTagRef(ref^.symbol),RELOC_TAG_INDEX_LEB);
  3128. end;
  3129. else
  3130. internalerror(2025100504);
  3131. end;
  3132. with hp.oper[1]^ do
  3133. case typ of
  3134. top_const:
  3135. WriteUleb(val);
  3136. else
  3137. internalerror(2025100505);
  3138. end;
  3139. end
  3140. else
  3141. begin
  3142. if hp.ops<>1 then
  3143. internalerror(2025100506);
  3144. with hp.oper[0]^ do
  3145. case typ of
  3146. top_const:
  3147. WriteUleb(val);
  3148. else
  3149. internalerror(2025100507);
  3150. end;
  3151. end;
  3152. hp:=taicpu(hp.Next);
  3153. end;
  3154. end;
  3155. end;
  3156. a_else:
  3157. WriteByte($05);
  3158. a_i32_load,
  3159. a_i64_load,
  3160. a_f32_load,
  3161. a_f64_load,
  3162. a_i32_load8_s,
  3163. a_i32_load8_u,
  3164. a_i32_load16_s,
  3165. a_i32_load16_u,
  3166. a_i64_load8_s,
  3167. a_i64_load8_u,
  3168. a_i64_load16_s,
  3169. a_i64_load16_u,
  3170. a_i64_load32_s,
  3171. a_i64_load32_u,
  3172. a_i32_store,
  3173. a_i64_store,
  3174. a_f32_store,
  3175. a_f64_store,
  3176. a_i32_store8,
  3177. a_i32_store16,
  3178. a_i64_store8,
  3179. a_i64_store16,
  3180. a_i64_store32:
  3181. begin
  3182. case opcode of
  3183. a_i32_load:
  3184. WriteByte($28);
  3185. a_i64_load:
  3186. WriteByte($29);
  3187. a_f32_load:
  3188. WriteByte($2A);
  3189. a_f64_load:
  3190. WriteByte($2B);
  3191. a_i32_load8_s:
  3192. WriteByte($2C);
  3193. a_i32_load8_u:
  3194. WriteByte($2D);
  3195. a_i32_load16_s:
  3196. WriteByte($2E);
  3197. a_i32_load16_u:
  3198. WriteByte($2F);
  3199. a_i64_load8_s:
  3200. WriteByte($30);
  3201. a_i64_load8_u:
  3202. WriteByte($31);
  3203. a_i64_load16_s:
  3204. WriteByte($32);
  3205. a_i64_load16_u:
  3206. WriteByte($33);
  3207. a_i64_load32_s:
  3208. WriteByte($34);
  3209. a_i64_load32_u:
  3210. WriteByte($35);
  3211. a_i32_store:
  3212. WriteByte($36);
  3213. a_i64_store:
  3214. WriteByte($37);
  3215. a_f32_store:
  3216. WriteByte($38);
  3217. a_f64_store:
  3218. WriteByte($39);
  3219. a_i32_store8:
  3220. WriteByte($3A);
  3221. a_i32_store16:
  3222. WriteByte($3B);
  3223. a_i64_store8:
  3224. WriteByte($3C);
  3225. a_i64_store16:
  3226. WriteByte($3D);
  3227. a_i64_store32:
  3228. WriteByte($3E);
  3229. else
  3230. internalerror(2021092019);
  3231. end;
  3232. if ops<>1 then
  3233. internalerror(2021092016);
  3234. with oper[0]^ do
  3235. case typ of
  3236. top_ref:
  3237. begin
  3238. if assigned(ref^.symbol) then
  3239. begin
  3240. WriteUleb(natural_alignment_for_load_store(opcode));
  3241. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_LEB);
  3242. end
  3243. else
  3244. begin
  3245. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  3246. internalerror(2021092018);
  3247. WriteUleb(natural_alignment_for_load_store(opcode));
  3248. WriteUleb(ref^.offset);
  3249. end;
  3250. end;
  3251. top_const:
  3252. begin
  3253. WriteUleb(natural_alignment_for_load_store(opcode));
  3254. WriteUleb(val);
  3255. end;
  3256. else
  3257. internalerror(2021092017);
  3258. end;
  3259. end;
  3260. a_memory_atomic_notify,
  3261. a_memory_atomic_wait32,
  3262. a_memory_atomic_wait64,
  3263. a_i32_atomic_load,
  3264. a_i64_atomic_load,
  3265. a_i32_atomic_load8_u,
  3266. a_i32_atomic_load16_u,
  3267. a_i64_atomic_load8_u,
  3268. a_i64_atomic_load16_u,
  3269. a_i64_atomic_load32_u,
  3270. a_i32_atomic_store,
  3271. a_i64_atomic_store,
  3272. a_i32_atomic_store8,
  3273. a_i32_atomic_store16,
  3274. a_i64_atomic_store8,
  3275. a_i64_atomic_store16,
  3276. a_i64_atomic_store32,
  3277. a_i32_atomic_rmw_add,
  3278. a_i64_atomic_rmw_add,
  3279. a_i32_atomic_rmw8_add_u,
  3280. a_i32_atomic_rmw16_add_u,
  3281. a_i64_atomic_rmw8_add_u,
  3282. a_i64_atomic_rmw16_add_u,
  3283. a_i64_atomic_rmw32_add_u,
  3284. a_i32_atomic_rmw_sub,
  3285. a_i64_atomic_rmw_sub,
  3286. a_i32_atomic_rmw8_sub_u,
  3287. a_i32_atomic_rmw16_sub_u,
  3288. a_i64_atomic_rmw8_sub_u,
  3289. a_i64_atomic_rmw16_sub_u,
  3290. a_i64_atomic_rmw32_sub_u,
  3291. a_i32_atomic_rmw_and,
  3292. a_i64_atomic_rmw_and,
  3293. a_i32_atomic_rmw8_and_u,
  3294. a_i32_atomic_rmw16_and_u,
  3295. a_i64_atomic_rmw8_and_u,
  3296. a_i64_atomic_rmw16_and_u,
  3297. a_i64_atomic_rmw32_and_u,
  3298. a_i32_atomic_rmw_or,
  3299. a_i64_atomic_rmw_or,
  3300. a_i32_atomic_rmw8_or_u,
  3301. a_i32_atomic_rmw16_or_u,
  3302. a_i64_atomic_rmw8_or_u,
  3303. a_i64_atomic_rmw16_or_u,
  3304. a_i64_atomic_rmw32_or_u,
  3305. a_i32_atomic_rmw_xor,
  3306. a_i64_atomic_rmw_xor,
  3307. a_i32_atomic_rmw8_xor_u,
  3308. a_i32_atomic_rmw16_xor_u,
  3309. a_i64_atomic_rmw8_xor_u,
  3310. a_i64_atomic_rmw16_xor_u,
  3311. a_i64_atomic_rmw32_xor_u,
  3312. a_i32_atomic_rmw_xchg,
  3313. a_i64_atomic_rmw_xchg,
  3314. a_i32_atomic_rmw8_xchg_u,
  3315. a_i32_atomic_rmw16_xchg_u,
  3316. a_i64_atomic_rmw8_xchg_u,
  3317. a_i64_atomic_rmw16_xchg_u,
  3318. a_i64_atomic_rmw32_xchg_u,
  3319. a_i32_atomic_rmw_cmpxchg,
  3320. a_i64_atomic_rmw_cmpxchg,
  3321. a_i32_atomic_rmw8_cmpxchg_u,
  3322. a_i32_atomic_rmw16_cmpxchg_u,
  3323. a_i64_atomic_rmw8_cmpxchg_u,
  3324. a_i64_atomic_rmw16_cmpxchg_u,
  3325. a_i64_atomic_rmw32_cmpxchg_u:
  3326. begin
  3327. WriteByte($FE);
  3328. case opcode of
  3329. a_memory_atomic_notify:
  3330. WriteByte($00);
  3331. a_memory_atomic_wait32:
  3332. WriteByte($01);
  3333. a_memory_atomic_wait64:
  3334. WriteByte($02);
  3335. a_i32_atomic_load:
  3336. WriteByte($10);
  3337. a_i64_atomic_load:
  3338. WriteByte($11);
  3339. a_i32_atomic_load8_u:
  3340. WriteByte($12);
  3341. a_i32_atomic_load16_u:
  3342. WriteByte($13);
  3343. a_i64_atomic_load8_u:
  3344. WriteByte($14);
  3345. a_i64_atomic_load16_u:
  3346. WriteByte($15);
  3347. a_i64_atomic_load32_u:
  3348. WriteByte($16);
  3349. a_i32_atomic_store:
  3350. WriteByte($17);
  3351. a_i64_atomic_store:
  3352. WriteByte($18);
  3353. a_i32_atomic_store8:
  3354. WriteByte($19);
  3355. a_i32_atomic_store16:
  3356. WriteByte($1A);
  3357. a_i64_atomic_store8:
  3358. WriteByte($1B);
  3359. a_i64_atomic_store16:
  3360. WriteByte($1C);
  3361. a_i64_atomic_store32:
  3362. WriteByte($1D);
  3363. a_i32_atomic_rmw_add:
  3364. WriteByte($1E);
  3365. a_i64_atomic_rmw_add:
  3366. WriteByte($1F);
  3367. a_i32_atomic_rmw8_add_u:
  3368. WriteByte($20);
  3369. a_i32_atomic_rmw16_add_u:
  3370. WriteByte($21);
  3371. a_i64_atomic_rmw8_add_u:
  3372. WriteByte($22);
  3373. a_i64_atomic_rmw16_add_u:
  3374. WriteByte($23);
  3375. a_i64_atomic_rmw32_add_u:
  3376. WriteByte($24);
  3377. a_i32_atomic_rmw_sub:
  3378. WriteByte($25);
  3379. a_i64_atomic_rmw_sub:
  3380. WriteByte($26);
  3381. a_i32_atomic_rmw8_sub_u:
  3382. WriteByte($27);
  3383. a_i32_atomic_rmw16_sub_u:
  3384. WriteByte($28);
  3385. a_i64_atomic_rmw8_sub_u:
  3386. WriteByte($29);
  3387. a_i64_atomic_rmw16_sub_u:
  3388. WriteByte($2A);
  3389. a_i64_atomic_rmw32_sub_u:
  3390. WriteByte($2B);
  3391. a_i32_atomic_rmw_and:
  3392. WriteByte($2C);
  3393. a_i64_atomic_rmw_and:
  3394. WriteByte($2D);
  3395. a_i32_atomic_rmw8_and_u:
  3396. WriteByte($2E);
  3397. a_i32_atomic_rmw16_and_u:
  3398. WriteByte($2F);
  3399. a_i64_atomic_rmw8_and_u:
  3400. WriteByte($30);
  3401. a_i64_atomic_rmw16_and_u:
  3402. WriteByte($31);
  3403. a_i64_atomic_rmw32_and_u:
  3404. WriteByte($32);
  3405. a_i32_atomic_rmw_or:
  3406. WriteByte($33);
  3407. a_i64_atomic_rmw_or:
  3408. WriteByte($34);
  3409. a_i32_atomic_rmw8_or_u:
  3410. WriteByte($35);
  3411. a_i32_atomic_rmw16_or_u:
  3412. WriteByte($36);
  3413. a_i64_atomic_rmw8_or_u:
  3414. WriteByte($37);
  3415. a_i64_atomic_rmw16_or_u:
  3416. WriteByte($38);
  3417. a_i64_atomic_rmw32_or_u:
  3418. WriteByte($39);
  3419. a_i32_atomic_rmw_xor:
  3420. WriteByte($3A);
  3421. a_i64_atomic_rmw_xor:
  3422. WriteByte($3B);
  3423. a_i32_atomic_rmw8_xor_u:
  3424. WriteByte($3C);
  3425. a_i32_atomic_rmw16_xor_u:
  3426. WriteByte($3D);
  3427. a_i64_atomic_rmw8_xor_u:
  3428. WriteByte($3E);
  3429. a_i64_atomic_rmw16_xor_u:
  3430. WriteByte($3F);
  3431. a_i64_atomic_rmw32_xor_u:
  3432. WriteByte($40);
  3433. a_i32_atomic_rmw_xchg:
  3434. WriteByte($41);
  3435. a_i64_atomic_rmw_xchg:
  3436. WriteByte($42);
  3437. a_i32_atomic_rmw8_xchg_u:
  3438. WriteByte($43);
  3439. a_i32_atomic_rmw16_xchg_u:
  3440. WriteByte($44);
  3441. a_i64_atomic_rmw8_xchg_u:
  3442. WriteByte($45);
  3443. a_i64_atomic_rmw16_xchg_u:
  3444. WriteByte($46);
  3445. a_i64_atomic_rmw32_xchg_u:
  3446. WriteByte($47);
  3447. a_i32_atomic_rmw_cmpxchg:
  3448. WriteByte($48);
  3449. a_i64_atomic_rmw_cmpxchg:
  3450. WriteByte($49);
  3451. a_i32_atomic_rmw8_cmpxchg_u:
  3452. WriteByte($4A);
  3453. a_i32_atomic_rmw16_cmpxchg_u:
  3454. WriteByte($4B);
  3455. a_i64_atomic_rmw8_cmpxchg_u:
  3456. WriteByte($4C);
  3457. a_i64_atomic_rmw16_cmpxchg_u:
  3458. WriteByte($4D);
  3459. a_i64_atomic_rmw32_cmpxchg_u:
  3460. WriteByte($4E);
  3461. else
  3462. internalerror(2022052101);
  3463. end;
  3464. if ops<>1 then
  3465. internalerror(2021092016);
  3466. with oper[0]^ do
  3467. case typ of
  3468. top_ref:
  3469. begin
  3470. if assigned(ref^.symbol) then
  3471. begin
  3472. WriteUleb(natural_alignment_for_load_store(opcode));
  3473. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_LEB);
  3474. end
  3475. else
  3476. begin
  3477. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  3478. internalerror(2021092018);
  3479. WriteUleb(natural_alignment_for_load_store(opcode));
  3480. WriteUleb(ref^.offset);
  3481. end;
  3482. end;
  3483. top_const:
  3484. begin
  3485. WriteUleb(natural_alignment_for_load_store(opcode));
  3486. WriteUleb(val);
  3487. end;
  3488. else
  3489. internalerror(2021092017);
  3490. end;
  3491. end;
  3492. a_call:
  3493. begin
  3494. if ops<>2 then
  3495. internalerror(2021092021);
  3496. with oper[0]^ do
  3497. case typ of
  3498. top_ref:
  3499. begin
  3500. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  3501. internalerror(2021092023);
  3502. WriteByte($10);
  3503. objdata.writeReloc(0,5,ObjData.symbolref(ref^.symbol),RELOC_FUNCTION_INDEX_LEB);
  3504. end;
  3505. else
  3506. internalerror(2021092022);
  3507. end;
  3508. end;
  3509. a_call_indirect:
  3510. begin
  3511. if ops<>1 then
  3512. internalerror(2021092610);
  3513. with oper[0]^ do
  3514. case typ of
  3515. top_functype:
  3516. begin
  3517. WriteByte($11);
  3518. objdata.writeReloc(TWasmObjData(objdata).FuncTypes.AddOrGetFuncType(functype),5,nil,RELOC_TYPE_INDEX_LEB);
  3519. WriteUleb(0);
  3520. end;
  3521. else
  3522. internalerror(2021092611);
  3523. end;
  3524. end;
  3525. a_br,
  3526. a_br_if,
  3527. a_legacy_rethrow,
  3528. a_legacy_delegate:
  3529. begin
  3530. case opcode of
  3531. a_br:
  3532. WriteByte($0C);
  3533. a_br_if:
  3534. WriteByte($0D);
  3535. a_legacy_rethrow:
  3536. WriteByte($09);
  3537. a_legacy_delegate:
  3538. WriteByte($18);
  3539. else
  3540. internalerror(2021092622);
  3541. end;
  3542. if ops<>1 then
  3543. internalerror(2021092610);
  3544. with oper[0]^ do
  3545. case typ of
  3546. top_const:
  3547. WriteUleb(val);
  3548. else
  3549. internalerror(2021092625);
  3550. end;
  3551. end;
  3552. a_legacy_catch,
  3553. a_legacy_throw,
  3554. a_throw:
  3555. begin
  3556. case opcode of
  3557. a_legacy_catch:
  3558. WriteByte($07);
  3559. a_legacy_throw,
  3560. a_throw:
  3561. WriteByte($08);
  3562. else
  3563. internalerror(2021092708);
  3564. end;
  3565. if ops<>1 then
  3566. internalerror(2021092709);
  3567. with oper[0]^ do
  3568. case typ of
  3569. top_ref:
  3570. begin
  3571. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  3572. internalerror(2021092711);
  3573. objdata.writeReloc(0,5,TWasmObjData(ObjData).ExceptionTagRef(ref^.symbol),RELOC_TAG_INDEX_LEB);
  3574. end;
  3575. else
  3576. internalerror(2021092710);
  3577. end;
  3578. end;
  3579. a_memory_init:
  3580. begin
  3581. WriteByte($FC);
  3582. WriteByte($08);
  3583. if ops<>1 then
  3584. internalerror(2022052806);
  3585. with oper[0]^ do
  3586. case typ of
  3587. top_const:
  3588. WriteUleb(val);
  3589. else
  3590. internalerror(2022052807);
  3591. end;
  3592. WriteByte($00);
  3593. end;
  3594. a_data_drop:
  3595. begin
  3596. WriteByte($FC);
  3597. WriteByte($09);
  3598. if ops<>1 then
  3599. internalerror(2022052808);
  3600. with oper[0]^ do
  3601. case typ of
  3602. top_const:
  3603. WriteUleb(val);
  3604. else
  3605. internalerror(2022052809);
  3606. end;
  3607. end;
  3608. a_i32_trunc_sat_f32_s:
  3609. begin
  3610. WriteByte($FC);
  3611. WriteByte($00);
  3612. end;
  3613. a_i32_trunc_sat_f32_u:
  3614. begin
  3615. WriteByte($FC);
  3616. WriteByte($01);
  3617. end;
  3618. a_i32_trunc_sat_f64_s:
  3619. begin
  3620. WriteByte($FC);
  3621. WriteByte($02);
  3622. end;
  3623. a_i32_trunc_sat_f64_u:
  3624. begin
  3625. WriteByte($FC);
  3626. WriteByte($03);
  3627. end;
  3628. a_i64_trunc_sat_f32_s:
  3629. begin
  3630. WriteByte($FC);
  3631. WriteByte($04);
  3632. end;
  3633. a_i64_trunc_sat_f32_u:
  3634. begin
  3635. WriteByte($FC);
  3636. WriteByte($05);
  3637. end;
  3638. a_i64_trunc_sat_f64_s:
  3639. begin
  3640. WriteByte($FC);
  3641. WriteByte($06);
  3642. end;
  3643. a_i64_trunc_sat_f64_u:
  3644. begin
  3645. WriteByte($FC);
  3646. WriteByte($07);
  3647. end;
  3648. a_ref_null_funcref:
  3649. begin
  3650. WriteByte($D0);
  3651. WriteByte($70);
  3652. end;
  3653. a_ref_null_externref:
  3654. begin
  3655. WriteByte($D0);
  3656. WriteByte($6F);
  3657. end;
  3658. a_ref_is_null:
  3659. WriteByte($D1);
  3660. else
  3661. internalerror(2021092624);
  3662. end;
  3663. end;
  3664. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  3665. begin
  3666. internalerror(2010122614);
  3667. result:=nil;
  3668. end;
  3669. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  3670. begin
  3671. internalerror(2010122615);
  3672. result:=nil;
  3673. end;
  3674. procedure InitAsm;
  3675. begin
  3676. end;
  3677. procedure DoneAsm;
  3678. begin
  3679. end;
  3680. function wasm_convert_first_item_to_structured(srclist: TAsmList): tai;
  3681. begin
  3682. result:=tai(srclist.First);
  3683. if result<>nil then
  3684. begin
  3685. srclist.Remove(result);
  3686. if result.typ=ait_instruction then
  3687. case taicpu(result).opcode of
  3688. a_if:
  3689. result:=tai_wasmstruc_if.create_from(taicpu(result),srclist);
  3690. a_block:
  3691. result:=tai_wasmstruc_block.create_from(taicpu(result),srclist);
  3692. a_loop:
  3693. result:=tai_wasmstruc_loop.create_from(taicpu(result),srclist);
  3694. a_legacy_try:
  3695. result:=tai_wasmstruc_legacy_try.create_from(srclist);
  3696. a_try_table:
  3697. result:=tai_wasmstruc_try_table.create_from(taicpu(result),srclist);
  3698. a_else,a_end_if,a_end_block,a_end_loop,a_end_legacy_try,a_legacy_catch,a_legacy_catch_all,a_legacy_delegate,
  3699. a_end_try_table,a_catch,a_catch_ref,a_catch_all,a_catch_all_ref:
  3700. internalerror(2023100503);
  3701. else
  3702. ;
  3703. end;
  3704. end;
  3705. end;
  3706. procedure wasm_convert_to_structured_asmlist_internal(srclist, destlist: TAsmList);
  3707. begin
  3708. while not srclist.Empty do
  3709. destlist.Concat(wasm_convert_first_item_to_structured(srclist));
  3710. end;
  3711. procedure wasm_convert_to_structured_asmlist(var asmlist: TAsmList);
  3712. var
  3713. tmplist: TAsmList;
  3714. begin
  3715. tmplist:=TAsmList.Create;
  3716. wasm_convert_to_structured_asmlist_internal(asmlist,tmplist);
  3717. asmlist.Free;
  3718. asmlist:=tmplist;
  3719. end;
  3720. procedure wasm_convert_to_flat_asmlist_internal(srclist, destlist: TAsmList);
  3721. var
  3722. p: tai;
  3723. tmplist: TAsmList;
  3724. begin
  3725. tmplist:=TAsmList.Create;
  3726. while not srclist.Empty do
  3727. begin
  3728. p:=tai(srclist.First);
  3729. srclist.Remove(p);
  3730. if p.typ=ait_wasm_structured_instruction then
  3731. begin
  3732. taicpu_wasm_structured_instruction(p).ConvertToFlatList(tmplist);
  3733. srclist.insertList(tmplist);
  3734. end
  3735. else
  3736. destlist.Concat(p);
  3737. end;
  3738. tmplist.free;
  3739. end;
  3740. procedure wasm_convert_to_flat_asmlist(var asmlist: TAsmList);
  3741. var
  3742. tmplist: TAsmList;
  3743. begin
  3744. tmplist:=TAsmList.Create;
  3745. wasm_convert_to_flat_asmlist_internal(asmlist,tmplist);
  3746. asmlist.Free;
  3747. asmlist:=tmplist;
  3748. end;
  3749. procedure map_structured_asmlist_inner(l: TAsmList; f: TAsmMapFunc; blockstack: twasmstruc_stack);
  3750. var
  3751. p, q: tai;
  3752. mapres: TAsmMapFuncResult;
  3753. begin
  3754. if not assigned(l) then
  3755. exit;
  3756. p:=tai(l.First);
  3757. while p<>nil do
  3758. begin
  3759. if p.typ=ait_wasm_structured_instruction then
  3760. begin
  3761. mapres:=f(p,blockstack);
  3762. case mapres.typ of
  3763. amfrtNoChange:
  3764. begin
  3765. taicpu_wasm_structured_instruction(p).Map(f,blockstack);
  3766. p:=tai(p.next);
  3767. end;
  3768. amfrtNewAi:
  3769. begin
  3770. q:=mapres.newai;
  3771. if q<>p then
  3772. begin
  3773. l.InsertAfter(q,p);
  3774. l.Remove(p);
  3775. p:=q;
  3776. end;
  3777. p:=tai(p.next);
  3778. end;
  3779. amfrtNewList:
  3780. begin
  3781. q:=tai(mapres.newlist.First);
  3782. l.insertListAfter(p,mapres.newlist);
  3783. mapres.newlist.free;
  3784. l.Remove(p);
  3785. p:=q;
  3786. end;
  3787. amfrtDeleteAi:
  3788. begin
  3789. q:=p;
  3790. p:=tai(p.next);
  3791. l.Remove(q);
  3792. end;
  3793. end;
  3794. end
  3795. else
  3796. begin
  3797. mapres:=f(p,blockstack);
  3798. case mapres.typ of
  3799. amfrtNoChange:
  3800. p:=tai(p.next);
  3801. amfrtNewAi:
  3802. begin
  3803. q:=mapres.newai;
  3804. if q<>p then
  3805. begin
  3806. l.InsertAfter(q,p);
  3807. l.Remove(p);
  3808. p:=tai(q.next);
  3809. end
  3810. else
  3811. p:=tai(p.next);
  3812. end;
  3813. amfrtNewList:
  3814. begin
  3815. q:=tai(mapres.newlist.First);
  3816. l.insertListAfter(p,mapres.newlist);
  3817. mapres.newlist.free;
  3818. l.Remove(p);
  3819. p:=q;
  3820. end;
  3821. amfrtDeleteAi:
  3822. begin
  3823. q:=p;
  3824. p:=tai(p.next);
  3825. l.Remove(q);
  3826. end;
  3827. end;
  3828. end;
  3829. end;
  3830. end;
  3831. procedure map_structured_asmlist(l: TAsmList; f: TAsmMapFunc);
  3832. var
  3833. blockstack: twasmstruc_stack;
  3834. begin
  3835. blockstack:=twasmstruc_stack.create;
  3836. map_structured_asmlist_inner(l,f,blockstack);
  3837. blockstack.free;
  3838. end;
  3839. initialization
  3840. cai_cpu:=taicpu;
  3841. cai_align:=tai_align;
  3842. end.