aasmcpu.pas 114 KB

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