aasmcpu.pas 120 KB

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