aasmcpu.pas 115 KB

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