aasmcpu.pas 115 KB

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