aasmcpu.pas 108 KB

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