aasmcpu.pas 114 KB

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