aasmcpu.pas 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708
  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:
  865. begin
  866. if a.ops<>2 then
  867. internalerror(2024022501);
  868. if a.oper[1]^.typ<>top_functype then
  869. internalerror(2024022502);
  870. PopVals(a.oper[1]^.functype.params);
  871. PushVals(a.oper[1]^.functype.results);
  872. end;
  873. a_call_indirect:
  874. begin
  875. if a.ops<>1 then
  876. internalerror(2024022401);
  877. if a.oper[0]^.typ<>top_functype then
  878. internalerror(2024022402);
  879. PopVal(wbt_i32);
  880. PopVals(a.oper[0]^.functype.params);
  881. PushVals(a.oper[0]^.functype.results);
  882. end;
  883. else
  884. internalerror(2024030502);
  885. end;
  886. end;
  887. { twasmstruc_stack }
  888. function twasmstruc_stack.Get(Index: Integer): taicpu_wasm_structured_instruction;
  889. begin
  890. {$push}{$r+,q+}
  891. Result:=FStack[High(FStack)-Index];
  892. {$pop}
  893. end;
  894. procedure twasmstruc_stack.push(ins: taicpu_wasm_structured_instruction);
  895. begin
  896. SetLength(FStack,Length(FStack)+1);
  897. FStack[High(FStack)]:=ins;
  898. end;
  899. procedure twasmstruc_stack.pop;
  900. begin
  901. SetLength(FStack,Length(FStack)-1);
  902. end;
  903. { taicpu_wasm_structured_instruction }
  904. constructor taicpu_wasm_structured_instruction.Create;
  905. begin
  906. inherited;
  907. typ:=ait_wasm_structured_instruction;
  908. end;
  909. function taicpu_wasm_structured_instruction.getlabel: TAsmLabel;
  910. begin
  911. if not assigned(FLabel) then
  912. begin
  913. current_asmdata.getjumplabel(FLabel);
  914. FLabelIsNew:=true;
  915. end;
  916. result:=FLabel;
  917. end;
  918. { tai_wasmstruc_if }
  919. constructor tai_wasmstruc_if.create_from(a_if_instr: taicpu; srclist: TAsmList);
  920. var
  921. p: tai;
  922. ThenDone, ElsePresent, ElseDone: Boolean;
  923. begin
  924. wstyp:=aitws_if;
  925. inherited Create;
  926. if assigned(a_if_instr.Previous) or assigned(a_if_instr.Next) then
  927. internalerror(2023100301);
  928. if_instr:=a_if_instr;
  929. then_asmlist:=TAsmList.Create;
  930. ThenDone:=False;
  931. ElsePresent:=False;
  932. repeat
  933. p:=tai(srclist.First);
  934. if not assigned(p) then
  935. internalerror(2023100302);
  936. 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
  937. begin
  938. srclist.Remove(p);
  939. case taicpu(p).opcode of
  940. a_else:
  941. begin
  942. ThenDone:=True;
  943. ElsePresent:=True;
  944. end;
  945. a_end_if:
  946. ThenDone:=True;
  947. else
  948. internalerror(2023100501);
  949. end;
  950. end
  951. else
  952. then_asmlist.Concat(wasm_convert_first_item_to_structured(srclist));
  953. until ThenDone;
  954. if ElsePresent then
  955. begin
  956. else_asmlist:=TAsmList.Create;
  957. ElseDone:=False;
  958. repeat
  959. p:=tai(srclist.First);
  960. if not assigned(p) then
  961. internalerror(2023100303);
  962. if (p.typ=ait_instruction) and (taicpu(p).opcode=a_end_if) then
  963. begin
  964. srclist.Remove(p);
  965. ElseDone:=True;
  966. end
  967. else
  968. else_asmlist.Concat(wasm_convert_first_item_to_structured(srclist));
  969. until ElseDone;
  970. end;
  971. end;
  972. destructor tai_wasmstruc_if.Destroy;
  973. begin
  974. then_asmlist.free;
  975. else_asmlist.free;
  976. if_instr.free;
  977. inherited Destroy;
  978. end;
  979. function tai_wasmstruc_if.getcopy: TLinkedListItem;
  980. var
  981. p: tai_wasmstruc_if;
  982. begin
  983. p:=tai_wasmstruc_if(inherited getcopy);
  984. if assigned(if_instr) then
  985. p.if_instr:=taicpu(if_instr.getcopy);
  986. if assigned(then_asmlist) then
  987. begin
  988. p.then_asmlist:=TAsmList.Create;
  989. p.then_asmlist.concatListcopy(then_asmlist);
  990. end;
  991. if assigned(else_asmlist) then
  992. begin
  993. p.else_asmlist:=TAsmList.Create;
  994. p.else_asmlist.concatListcopy(else_asmlist);
  995. end;
  996. getcopy:=p;
  997. end;
  998. procedure tai_wasmstruc_if.Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);
  999. begin
  1000. blockstack.push(self);
  1001. map_structured_asmlist_inner(then_asmlist,f,blockstack);
  1002. map_structured_asmlist_inner(else_asmlist,f,blockstack);
  1003. blockstack.pop;
  1004. end;
  1005. procedure tai_wasmstruc_if.ConvertToFlatList(l: TAsmList);
  1006. begin
  1007. l.Concat(if_instr);
  1008. if_instr:=nil;
  1009. l.concatList(then_asmlist);
  1010. if assigned(else_asmlist) then
  1011. begin
  1012. l.Concat(taicpu.op_none(A_ELSE));
  1013. l.concatList(else_asmlist);
  1014. end;
  1015. l.Concat(taicpu.op_none(a_end_if));
  1016. if FLabelIsNew then
  1017. l.concat(tai_label.create(FLabel));
  1018. end;
  1019. procedure tai_wasmstruc_if.ConvertToBrIf(list: TAsmList; local_alloc: TWasmLocalAllocator);
  1020. var
  1021. res_ft: TWasmFuncType;
  1022. save_if_reg: Integer;
  1023. save_param_reg: array of Integer;
  1024. save_result_reg: array of Integer;
  1025. procedure AllocateLocalsForSavingParamsAndResult;
  1026. var
  1027. i: Integer;
  1028. begin
  1029. if not assigned(res_ft) then
  1030. exit;
  1031. if length(res_ft.params)<>0 then
  1032. begin
  1033. save_if_reg:=local_alloc(wbt_i32);
  1034. SetLength(save_param_reg,length(res_ft.params));
  1035. for i:=low(res_ft.params) to high(res_ft.params) do
  1036. save_param_reg[i]:=local_alloc(res_ft.params[i]);
  1037. end;
  1038. if length(res_ft.results)<>0 then
  1039. begin
  1040. SetLength(save_result_reg,length(res_ft.results));
  1041. for i:=low(res_ft.results) to high(res_ft.results) do
  1042. save_result_reg[i]:=local_alloc(res_ft.results[i]);
  1043. end;
  1044. end;
  1045. procedure SaveParams;
  1046. var
  1047. i: Integer;
  1048. begin
  1049. if (not assigned(res_ft)) or (length(res_ft.params)=0) then
  1050. exit;
  1051. list.concat(taicpu.op_const(a_local_set,save_if_reg));
  1052. for i:=high(res_ft.params) downto low(res_ft.params) do
  1053. list.concat(taicpu.op_const(a_local_set,save_param_reg[i]));
  1054. list.concat(taicpu.op_const(a_local_get,save_if_reg));
  1055. end;
  1056. procedure RestoreParams;
  1057. var
  1058. i: Integer;
  1059. begin
  1060. if (not assigned(res_ft)) or (length(res_ft.params)=0) then
  1061. exit;
  1062. for i:=low(res_ft.params) to high(res_ft.params) do
  1063. list.concat(taicpu.op_const(a_local_get,save_param_reg[i]));
  1064. end;
  1065. procedure SaveResults;
  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:=high(res_ft.results) downto low(res_ft.results) do
  1072. list.concat(taicpu.op_const(a_local_set,save_result_reg[i]));
  1073. end;
  1074. procedure RestoreResults;
  1075. var
  1076. i: Integer;
  1077. begin
  1078. if (not assigned(res_ft)) or (length(res_ft.results)=0) then
  1079. exit;
  1080. for i:=low(res_ft.results) to high(res_ft.results) do
  1081. list.concat(taicpu.op_const(a_local_get,save_result_reg[i]));
  1082. end;
  1083. var
  1084. then_label: TAsmLabel;
  1085. begin
  1086. if if_instr.ops>1 then
  1087. internalerror(2023101701);
  1088. if (if_instr.ops=1) and (if_instr.oper[0]^.typ=top_functype) then
  1089. res_ft:=if_instr.oper[0]^.functype
  1090. else
  1091. res_ft:=nil;
  1092. AllocateLocalsForSavingParamsAndResult;
  1093. current_asmdata.getjumplabel(then_label);
  1094. SaveParams;
  1095. list.concat(taicpu.op_sym(a_br_if,then_label));
  1096. RestoreParams;
  1097. if assigned(else_asmlist) then
  1098. list.concatList(else_asmlist);
  1099. SaveResults;
  1100. list.concat(taicpu.op_sym(a_br, GetLabel));
  1101. list.concat(tai_label.create(then_label));
  1102. RestoreParams;
  1103. list.concatList(then_asmlist);
  1104. SaveResults;
  1105. list.concat(tai_label.create(GetLabel));
  1106. RestoreResults;
  1107. end;
  1108. { tai_wasmstruc_block }
  1109. constructor tai_wasmstruc_block.create_from(a_block_instr: taicpu; srclist: TAsmList);
  1110. var
  1111. Done: Boolean;
  1112. p: tai;
  1113. begin
  1114. wstyp:=aitws_block;
  1115. inherited Create;
  1116. if assigned(a_block_instr.Previous) or assigned(a_block_instr.Next) then
  1117. internalerror(2023100304);
  1118. block_instr:=a_block_instr;
  1119. inner_asmlist:=TAsmList.Create;
  1120. Done:=False;
  1121. repeat
  1122. p:=tai(srclist.First);
  1123. if not assigned(p) then
  1124. internalerror(2023100305);
  1125. if (p.typ=ait_instruction) and (taicpu(p).opcode=a_end_block) then
  1126. begin
  1127. srclist.Remove(p);
  1128. Done:=True;
  1129. end
  1130. else
  1131. inner_asmlist.Concat(wasm_convert_first_item_to_structured(srclist));
  1132. until Done;
  1133. end;
  1134. destructor tai_wasmstruc_block.Destroy;
  1135. begin
  1136. inner_asmlist.free;
  1137. block_instr.free;
  1138. inherited Destroy;
  1139. end;
  1140. function tai_wasmstruc_block.getcopy: TLinkedListItem;
  1141. var
  1142. p: tai_wasmstruc_block;
  1143. begin
  1144. p:=tai_wasmstruc_block(inherited getcopy);
  1145. if assigned(block_instr) then
  1146. p.block_instr:=taicpu(block_instr.getcopy);
  1147. if assigned(inner_asmlist) then
  1148. begin
  1149. p.inner_asmlist:=TAsmList.Create;
  1150. p.inner_asmlist.concatListcopy(inner_asmlist);
  1151. end;
  1152. getcopy:=p;
  1153. end;
  1154. procedure tai_wasmstruc_block.Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);
  1155. begin
  1156. blockstack.push(self);
  1157. map_structured_asmlist_inner(inner_asmlist,f,blockstack);
  1158. blockstack.pop;
  1159. end;
  1160. procedure tai_wasmstruc_block.ConvertToFlatList(l: TAsmList);
  1161. begin
  1162. l.Concat(block_instr);
  1163. block_instr:=nil;
  1164. l.concatList(inner_asmlist);
  1165. l.Concat(taicpu.op_none(a_end_block));
  1166. if FLabelIsNew then
  1167. l.concat(tai_label.create(FLabel));
  1168. end;
  1169. { tai_wasmstruc_loop }
  1170. constructor tai_wasmstruc_loop.create_from(a_loop_instr: taicpu; srclist: TAsmList);
  1171. var
  1172. Done: Boolean;
  1173. p: tai;
  1174. begin
  1175. wstyp:=aitws_loop;
  1176. inherited Create;
  1177. if assigned(a_loop_instr.Previous) or assigned(a_loop_instr.Next) then
  1178. internalerror(2023100306);
  1179. loop_instr:=a_loop_instr;
  1180. inner_asmlist:=TAsmList.Create;
  1181. Done:=False;
  1182. repeat
  1183. p:=tai(srclist.First);
  1184. if not assigned(p) then
  1185. internalerror(2023100307);
  1186. if (p.typ=ait_instruction) and (taicpu(p).opcode=a_end_loop) then
  1187. begin
  1188. srclist.Remove(p);
  1189. Done:=True;
  1190. end
  1191. else
  1192. inner_asmlist.Concat(wasm_convert_first_item_to_structured(srclist));
  1193. until Done;
  1194. end;
  1195. destructor tai_wasmstruc_loop.Destroy;
  1196. begin
  1197. inner_asmlist.free;
  1198. loop_instr.free;
  1199. inherited Destroy;
  1200. end;
  1201. function tai_wasmstruc_loop.getcopy: TLinkedListItem;
  1202. var
  1203. p: tai_wasmstruc_loop;
  1204. begin
  1205. p:=tai_wasmstruc_loop(inherited getcopy);
  1206. if assigned(loop_instr) then
  1207. p.loop_instr:=taicpu(loop_instr.getcopy);
  1208. if assigned(inner_asmlist) then
  1209. begin
  1210. p.inner_asmlist:=TAsmList.Create;
  1211. p.inner_asmlist.concatListcopy(inner_asmlist);
  1212. end;
  1213. getcopy:=p;
  1214. end;
  1215. procedure tai_wasmstruc_loop.Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);
  1216. begin
  1217. blockstack.push(self);
  1218. map_structured_asmlist_inner(inner_asmlist,f,blockstack);
  1219. blockstack.pop;
  1220. end;
  1221. procedure tai_wasmstruc_loop.ConvertToBr(list: TAsmList);
  1222. begin
  1223. list.concat(tai_label.create(GetLabel));
  1224. list.concatList(inner_asmlist);
  1225. list.concat(taicpu.op_sym(a_br,GetLabel));
  1226. end;
  1227. procedure tai_wasmstruc_loop.ConvertToFlatList(l: TAsmList);
  1228. begin
  1229. l.Concat(loop_instr);
  1230. loop_instr:=nil;
  1231. if FLabelIsNew then
  1232. l.concat(tai_label.create(FLabel));
  1233. l.concatList(inner_asmlist);
  1234. l.Concat(taicpu.op_none(a_end_loop));
  1235. end;
  1236. { tai_wasmstruc_try }
  1237. class function tai_wasmstruc_try.create_from(srclist: TAsmList): tai_wasmstruc_try;
  1238. var
  1239. Done: Boolean;
  1240. p: tai;
  1241. tmp_asmlist: TAsmList;
  1242. begin
  1243. result:=nil;
  1244. tmp_asmlist:=TAsmList.Create;
  1245. Done:=False;
  1246. repeat
  1247. p:=tai(srclist.First);
  1248. if not assigned(p) then
  1249. internalerror(2023100308);
  1250. if (p.typ=ait_instruction) and (taicpu(p).opcode in [a_end_try,a_catch,a_catch_all,a_delegate]) then
  1251. begin
  1252. srclist.Remove(p);
  1253. Done:=True;
  1254. end
  1255. else
  1256. tmp_asmlist.Concat(wasm_convert_first_item_to_structured(srclist));
  1257. until Done;
  1258. case taicpu(p).opcode of
  1259. a_end_try,a_catch,a_catch_all:
  1260. result:=tai_wasmstruc_try_catch.internal_create(taicpu(p),tmp_asmlist,srclist);
  1261. a_delegate:
  1262. result:=tai_wasmstruc_try_delegate.internal_create(taicpu(p),tmp_asmlist,srclist);
  1263. else
  1264. internalerror(2023100502);
  1265. end;
  1266. end;
  1267. constructor tai_wasmstruc_try.internal_create(a_try_asmlist: TAsmList);
  1268. begin
  1269. inherited Create;
  1270. try_asmlist:=a_try_asmlist;
  1271. end;
  1272. destructor tai_wasmstruc_try.Destroy;
  1273. begin
  1274. try_asmlist.free;
  1275. inherited Destroy;
  1276. end;
  1277. function tai_wasmstruc_try.getcopy: TLinkedListItem;
  1278. var
  1279. p: tai_wasmstruc_try;
  1280. begin
  1281. p:=tai_wasmstruc_try(inherited getcopy);
  1282. if assigned(try_asmlist) then
  1283. begin
  1284. p.try_asmlist:=TAsmList.Create;
  1285. p.try_asmlist.concatListcopy(try_asmlist);
  1286. end;
  1287. getcopy:=p;
  1288. end;
  1289. procedure tai_wasmstruc_try.ConvertToFlatList(l: TAsmList);
  1290. begin
  1291. l.Concat(taicpu.op_none(A_TRY));
  1292. l.concatList(try_asmlist);
  1293. end;
  1294. { tai_wasmstruc_try_catch }
  1295. constructor tai_wasmstruc_try_catch.internal_create(first_ins: taicpu; a_try_asmlist, srclist: TAsmList);
  1296. var
  1297. p: tai;
  1298. procedure parse_next_catch_block;
  1299. var
  1300. new_catch_index: Integer;
  1301. al: TAsmList;
  1302. Done: Boolean;
  1303. pp: tai;
  1304. begin
  1305. SetLength(catch_list,Length(catch_list)+1);
  1306. new_catch_index:=High(catch_list);
  1307. catch_list[new_catch_index].catch_instr:=taicpu(p);
  1308. al:=TAsmList.Create;
  1309. catch_list[new_catch_index].asmlist:=al;
  1310. Done:=False;
  1311. repeat
  1312. pp:=tai(srclist.First);
  1313. if (pp.typ=ait_instruction) and (taicpu(pp).opcode in [a_catch,a_catch_all,a_end_try]) then
  1314. Done:=True
  1315. else
  1316. al.Concat(wasm_convert_first_item_to_structured(srclist));
  1317. until Done;
  1318. end;
  1319. procedure parse_catch_all;
  1320. var
  1321. Done: Boolean;
  1322. pp: tai;
  1323. begin
  1324. catch_all_asmlist:=TAsmList.Create;
  1325. Done:=False;
  1326. repeat
  1327. pp:=tai(srclist.First);
  1328. if (pp.typ=ait_instruction) and (taicpu(pp).opcode=a_end_try) then
  1329. begin
  1330. srclist.Remove(pp);
  1331. Done:=True;
  1332. end
  1333. else
  1334. catch_all_asmlist.Concat(wasm_convert_first_item_to_structured(srclist));
  1335. until Done;
  1336. end;
  1337. var
  1338. Done: Boolean;
  1339. begin
  1340. wstyp:=aitws_try_catch;
  1341. inherited internal_create(a_try_asmlist);
  1342. if assigned(first_ins.Previous) or assigned(first_ins.Next) then
  1343. internalerror(2023100310);
  1344. Done:=False;
  1345. p:=first_ins;
  1346. repeat
  1347. if p.typ=ait_instruction then
  1348. case taicpu(p).opcode of
  1349. a_catch:
  1350. begin
  1351. parse_next_catch_block;
  1352. p:=tai(srclist.First);
  1353. srclist.Remove(p);
  1354. end;
  1355. a_catch_all:
  1356. begin
  1357. parse_catch_all;
  1358. Done:=True;
  1359. end;
  1360. a_end_try:
  1361. Done:=True;
  1362. else
  1363. internalerror(2023100311);
  1364. end
  1365. else
  1366. internalerror(2023100312);
  1367. until Done;
  1368. end;
  1369. destructor tai_wasmstruc_try_catch.Destroy;
  1370. var
  1371. i: Integer;
  1372. begin
  1373. for i:=low(catch_list) to high(catch_list) do
  1374. begin
  1375. catch_list[i].asmlist.free;
  1376. catch_list[i].catch_instr.free;
  1377. end;
  1378. catch_all_asmlist.free;
  1379. inherited Destroy;
  1380. end;
  1381. function tai_wasmstruc_try_catch.getcopy: TLinkedListItem;
  1382. var
  1383. p: tai_wasmstruc_try_catch;
  1384. i: Integer;
  1385. begin
  1386. p:=tai_wasmstruc_try_catch(inherited getcopy);
  1387. p.catch_list:=Copy(catch_list);
  1388. for i:=0 to length(catch_list)-1 do
  1389. begin
  1390. if assigned(catch_list[i].asmlist) then
  1391. begin
  1392. p.catch_list[i].asmlist:=TAsmList.Create;
  1393. p.catch_list[i].asmlist.concatListcopy(catch_list[i].asmlist);
  1394. end;
  1395. if assigned(catch_list[i].catch_instr) then
  1396. p.catch_list[i].catch_instr:=taicpu(catch_list[i].catch_instr.getcopy);
  1397. end;
  1398. if assigned(catch_all_asmlist) then
  1399. begin
  1400. p.catch_all_asmlist:=TAsmList.Create;
  1401. p.catch_all_asmlist.concatListcopy(catch_all_asmlist);
  1402. end;
  1403. getcopy:=p;
  1404. end;
  1405. procedure tai_wasmstruc_try_catch.Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);
  1406. var
  1407. i: Integer;
  1408. begin
  1409. blockstack.push(self);
  1410. map_structured_asmlist_inner(try_asmlist,f,blockstack);
  1411. for i:=low(catch_list) to high(catch_list) do
  1412. map_structured_asmlist_inner(catch_list[i].asmlist,f,blockstack);
  1413. map_structured_asmlist_inner(catch_all_asmlist,f,blockstack);
  1414. blockstack.pop;
  1415. end;
  1416. procedure tai_wasmstruc_try_catch.ConvertToFlatList(l: TAsmList);
  1417. var
  1418. i: Integer;
  1419. begin
  1420. inherited ConvertToFlatList(l);
  1421. for i:=low(catch_list) to high(catch_list) do
  1422. begin
  1423. l.Concat(catch_list[i].catch_instr);
  1424. catch_list[i].catch_instr:=nil;
  1425. l.concatList(catch_list[i].asmlist);
  1426. end;
  1427. if assigned(catch_all_asmlist) then
  1428. begin
  1429. l.Concat(taicpu.op_none(a_catch_all));
  1430. l.concatList(catch_all_asmlist);
  1431. end;
  1432. l.Concat(taicpu.op_none(a_end_try));
  1433. if FLabelIsNew then
  1434. l.concat(tai_label.create(FLabel));
  1435. end;
  1436. { tai_wasmstruc_try_delegate }
  1437. constructor tai_wasmstruc_try_delegate.internal_create(first_ins: taicpu; a_try_asmlist, srclist: TAsmList);
  1438. begin
  1439. wstyp:=aitws_try_delegate;
  1440. inherited internal_create(a_try_asmlist);
  1441. if assigned(first_ins.Previous) or assigned(first_ins.Next) then
  1442. internalerror(2023100309);
  1443. delegate_instr:=first_ins;
  1444. end;
  1445. destructor tai_wasmstruc_try_delegate.Destroy;
  1446. begin
  1447. delegate_instr.free;
  1448. inherited Destroy;
  1449. end;
  1450. function tai_wasmstruc_try_delegate.getcopy: TLinkedListItem;
  1451. var
  1452. p: tai_wasmstruc_try_delegate;
  1453. begin
  1454. p:=tai_wasmstruc_try_delegate(inherited getcopy);
  1455. if assigned(delegate_instr) then
  1456. p.delegate_instr:=taicpu(delegate_instr.getcopy);
  1457. getcopy:=p;
  1458. end;
  1459. procedure tai_wasmstruc_try_delegate.Map(f: TAsmMapFunc; blockstack: twasmstruc_stack);
  1460. begin
  1461. blockstack.push(self);
  1462. map_structured_asmlist_inner(try_asmlist,f,blockstack);
  1463. blockstack.pop;
  1464. end;
  1465. procedure tai_wasmstruc_try_delegate.ConvertToFlatList(l: TAsmList);
  1466. begin
  1467. inherited ConvertToFlatList(l);
  1468. l.Concat(delegate_instr);
  1469. delegate_instr:=nil;
  1470. if FLabelIsNew then
  1471. l.concat(tai_label.create(FLabel));
  1472. end;
  1473. { tai_globaltype }
  1474. constructor tai_globaltype.create(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean);
  1475. begin
  1476. inherited Create;
  1477. sym:=current_asmdata.RefAsmSymbol(aglobalname,AT_WASM_GLOBAL);
  1478. typ:=ait_globaltype;
  1479. globalname:=aglobalname;
  1480. gtype:=atype;
  1481. immutable:=aimmutable;
  1482. is_external:=true;
  1483. is_global:=false;
  1484. end;
  1485. constructor tai_globaltype.create_local(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
  1486. begin
  1487. inherited Create;
  1488. sym:=current_asmdata.DefineAsmSymbol(aglobalname,AB_LOCAL,AT_WASM_GLOBAL,def);
  1489. typ:=ait_globaltype;
  1490. globalname:=aglobalname;
  1491. gtype:=atype;
  1492. immutable:=aimmutable;
  1493. is_external:=false;
  1494. is_global:=false;
  1495. end;
  1496. constructor tai_globaltype.create_global(const aglobalname: string; atype: TWasmBasicType; aimmutable: boolean; def: tdef);
  1497. begin
  1498. inherited Create;
  1499. sym:=current_asmdata.DefineAsmSymbol(aglobalname,AB_GLOBAL,AT_WASM_GLOBAL,def);
  1500. typ:=ait_globaltype;
  1501. globalname:=aglobalname;
  1502. gtype:=atype;
  1503. immutable:=aimmutable;
  1504. is_external:=false;
  1505. is_global:=true;
  1506. end;
  1507. { tai_import_name }
  1508. constructor tai_import_name.create(const asymname, aimportname: string);
  1509. begin
  1510. inherited Create;
  1511. typ:=ait_import_name;
  1512. symname:=asymname;
  1513. importname:=aimportname;
  1514. end;
  1515. { tai_import_module }
  1516. constructor tai_import_module.create(const asymname, aimportmodule: string);
  1517. begin
  1518. inherited Create;
  1519. typ:=ait_import_module;
  1520. symname:=asymname;
  1521. importmodule:=aimportmodule;
  1522. end;
  1523. { tai_functype }
  1524. constructor tai_functype.create(const afuncname: string; afunctype: TWasmFuncType);
  1525. begin
  1526. inherited Create;
  1527. typ:=ait_functype;
  1528. funcname:=afuncname;
  1529. functype:=afunctype;
  1530. end;
  1531. destructor tai_functype.destroy;
  1532. begin
  1533. functype.free;
  1534. inherited;
  1535. end;
  1536. { tai_tagtype }
  1537. constructor tai_tagtype.create(const atagname: string; aparams: TWasmResultType);
  1538. begin
  1539. typ:=ait_tagtype;
  1540. tagname:=atagname;
  1541. params:=aparams;
  1542. end;
  1543. { tai_local }
  1544. constructor tai_local.create(abasictype: TWasmBasicType; const aname: string);
  1545. begin
  1546. inherited Create;
  1547. bastyp := abasictype;
  1548. typ := ait_local;
  1549. name := aname;
  1550. end;
  1551. { timpexp_ai }
  1552. constructor tai_export_name.create(const aextname, aintname: ansistring;
  1553. asymtype: timpexptype);
  1554. begin
  1555. inherited create;
  1556. typ := ait_export_name;
  1557. extname := aextname;
  1558. intname := aintname;
  1559. symstype:= asymtype;
  1560. end;
  1561. {*****************************************************************************
  1562. taicpu Constructors
  1563. *****************************************************************************}
  1564. constructor taicpu.op_none(op : tasmop);
  1565. begin
  1566. inherited create(op);
  1567. end;
  1568. constructor taicpu.op_reg(op : tasmop;_op1 : tregister);
  1569. begin
  1570. inherited create(op);
  1571. ops:=1;
  1572. {$ifdef EXTDEBUG}
  1573. if getregtype(_op1)=R_INVALIDREGISTER then
  1574. InternalError(2021011901);
  1575. {$endif EXTDEBUG}
  1576. loadreg(0,_op1);
  1577. end;
  1578. constructor taicpu.op_ref(op : tasmop;const _op1 : treference);
  1579. begin
  1580. inherited create(op);
  1581. ops:=1;
  1582. loadref(0,_op1);
  1583. if op in [a_local_get,a_local_set,a_local_tee] then
  1584. begin
  1585. if (_op1.base<>NR_LOCAL_STACK_POINTER_REG) or (_op1.index<>NR_NO) then
  1586. internalerror(2021010201);
  1587. end
  1588. else
  1589. begin
  1590. if (_op1.base<>NR_NO) or (_op1.index<>NR_NO) then
  1591. internalerror(2021010202);
  1592. end;
  1593. end;
  1594. constructor taicpu.op_const(op : tasmop;_op1 : aint);
  1595. begin
  1596. inherited create(op);
  1597. ops:=1;
  1598. loadconst(0,_op1);
  1599. end;
  1600. constructor taicpu.op_sym(op : tasmop;_op1 : tasmsymbol);
  1601. begin
  1602. inherited create(op);
  1603. ops:=1;
  1604. loadsymbol(0,_op1,0);
  1605. end;
  1606. constructor taicpu.op_sym_const(op: tasmop; _op1: tasmsymbol; _op2: aint);
  1607. begin
  1608. inherited create(op);
  1609. ops:=2;
  1610. loadsymbol(0,_op1,0);
  1611. loadconst(1,_op2);
  1612. end;
  1613. constructor taicpu.op_sym_functype(op : tasmop;_op1 : tasmsymbol;_op2 : TWasmFuncType);
  1614. begin
  1615. inherited create(op);
  1616. ops:=2;
  1617. loadsymbol(0,_op1,0);
  1618. loadfunctype(1,_op2);
  1619. end;
  1620. constructor taicpu.op_single(op: tasmop; _op1: single);
  1621. begin
  1622. inherited create(op);
  1623. ops:=1;
  1624. loadsingle(0,_op1);
  1625. end;
  1626. constructor taicpu.op_double(op: tasmop; _op1: double);
  1627. begin
  1628. inherited create(op);
  1629. ops:=1;
  1630. loaddouble(0,_op1);
  1631. end;
  1632. constructor taicpu.op_functype(op: tasmop; _op1: TWasmFuncType);
  1633. begin
  1634. inherited create(op);
  1635. ops:=1;
  1636. loadfunctype(0,_op1);
  1637. end;
  1638. procedure taicpu.loadfunctype(opidx: longint; ft: TWasmFuncType);
  1639. begin
  1640. allocate_oper(opidx+1);
  1641. with oper[opidx]^ do
  1642. begin
  1643. if typ<>top_functype then
  1644. clearop(opidx);
  1645. functype:=ft;
  1646. typ:=top_functype;
  1647. end;
  1648. end;
  1649. procedure taicpu.loadsingle(opidx:longint;f:single);
  1650. begin
  1651. allocate_oper(opidx+1);
  1652. with oper[opidx]^ do
  1653. begin
  1654. if typ<>top_single then
  1655. clearop(opidx);
  1656. sval:=f;
  1657. typ:=top_single;
  1658. end;
  1659. end;
  1660. procedure taicpu.loaddouble(opidx: longint; d: double);
  1661. begin
  1662. allocate_oper(opidx+1);
  1663. with oper[opidx]^ do
  1664. begin
  1665. if typ<>top_double then
  1666. clearop(opidx);
  1667. dval:=d;
  1668. typ:=top_double;
  1669. end;
  1670. end;
  1671. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  1672. begin
  1673. result:=false;
  1674. end;
  1675. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  1676. begin
  1677. if opcode in AsmOp_Store then
  1678. result:=operand_write
  1679. else
  1680. result:=operand_read;
  1681. end;
  1682. function taicpu.Pass1(objdata: TObjData): longint;
  1683. function SlebSize(v: tcgint): longint;
  1684. begin
  1685. result:=0;
  1686. repeat
  1687. v:=SarInt64(v,7);
  1688. Inc(result);
  1689. until ((v=0) and ((byte(v) and 64)=0)) or ((v=-1) and ((byte(v) and 64)<>0));
  1690. end;
  1691. function UlebSize(v: tcgint): longint;
  1692. begin
  1693. result:=0;
  1694. repeat
  1695. v:=v shr 7;
  1696. Inc(result);
  1697. until v=0;
  1698. end;
  1699. begin
  1700. result:=0;
  1701. case opcode of
  1702. a_unreachable,
  1703. a_nop,
  1704. a_return,
  1705. a_drop,
  1706. a_i32_eqz,
  1707. a_i32_eq,
  1708. a_i32_ne,
  1709. a_i32_lt_s,
  1710. a_i32_lt_u,
  1711. a_i32_gt_s,
  1712. a_i32_gt_u,
  1713. a_i32_le_s,
  1714. a_i32_le_u,
  1715. a_i32_ge_s,
  1716. a_i32_ge_u,
  1717. a_i64_eqz,
  1718. a_i64_eq,
  1719. a_i64_ne,
  1720. a_i64_lt_s,
  1721. a_i64_lt_u,
  1722. a_i64_gt_s,
  1723. a_i64_gt_u,
  1724. a_i64_le_s,
  1725. a_i64_le_u,
  1726. a_i64_ge_s,
  1727. a_i64_ge_u,
  1728. a_f32_eq,
  1729. a_f32_ne,
  1730. a_f32_lt,
  1731. a_f32_gt,
  1732. a_f32_le,
  1733. a_f32_ge,
  1734. a_f64_eq,
  1735. a_f64_ne,
  1736. a_f64_lt,
  1737. a_f64_gt,
  1738. a_f64_le,
  1739. a_f64_ge,
  1740. a_i32_clz,
  1741. a_i32_ctz,
  1742. a_i32_popcnt,
  1743. a_i32_add,
  1744. a_i32_sub,
  1745. a_i32_mul,
  1746. a_i32_div_s,
  1747. a_i32_div_u,
  1748. a_i32_rem_s,
  1749. a_i32_rem_u,
  1750. a_i32_and,
  1751. a_i32_or,
  1752. a_i32_xor,
  1753. a_i32_shl,
  1754. a_i32_shr_s,
  1755. a_i32_shr_u,
  1756. a_i32_rotl,
  1757. a_i32_rotr,
  1758. a_i64_clz,
  1759. a_i64_ctz,
  1760. a_i64_popcnt,
  1761. a_i64_add,
  1762. a_i64_sub,
  1763. a_i64_mul,
  1764. a_i64_div_s,
  1765. a_i64_div_u,
  1766. a_i64_rem_s,
  1767. a_i64_rem_u,
  1768. a_i64_and,
  1769. a_i64_or,
  1770. a_i64_xor,
  1771. a_i64_shl,
  1772. a_i64_shr_s,
  1773. a_i64_shr_u,
  1774. a_i64_rotl,
  1775. a_i64_rotr,
  1776. a_f32_abs,
  1777. a_f32_neg,
  1778. a_f32_ceil,
  1779. a_f32_floor,
  1780. a_f32_trunc,
  1781. a_f32_nearest,
  1782. a_f32_sqrt,
  1783. a_f32_add,
  1784. a_f32_sub,
  1785. a_f32_mul,
  1786. a_f32_div,
  1787. a_f32_min,
  1788. a_f32_max,
  1789. a_f32_copysign,
  1790. a_f64_abs,
  1791. a_f64_neg,
  1792. a_f64_ceil,
  1793. a_f64_floor,
  1794. a_f64_trunc,
  1795. a_f64_nearest,
  1796. a_f64_sqrt,
  1797. a_f64_add,
  1798. a_f64_sub,
  1799. a_f64_mul,
  1800. a_f64_div,
  1801. a_f64_min,
  1802. a_f64_max,
  1803. a_f64_copysign,
  1804. a_i32_wrap_i64,
  1805. a_i32_trunc_f32_s,
  1806. a_i32_trunc_f32_u,
  1807. a_i32_trunc_f64_s,
  1808. a_i32_trunc_f64_u,
  1809. a_i64_extend_i32_s,
  1810. a_i64_extend_i32_u,
  1811. a_i64_trunc_f32_s,
  1812. a_i64_trunc_f32_u,
  1813. a_i64_trunc_f64_s,
  1814. a_i64_trunc_f64_u,
  1815. a_f32_convert_i32_s,
  1816. a_f32_convert_i32_u,
  1817. a_f32_convert_i64_s,
  1818. a_f32_convert_i64_u,
  1819. a_f32_demote_f64,
  1820. a_f64_convert_i32_s,
  1821. a_f64_convert_i32_u,
  1822. a_f64_convert_i64_s,
  1823. a_f64_convert_i64_u,
  1824. a_f64_promote_f32,
  1825. a_i32_reinterpret_f32,
  1826. a_i64_reinterpret_f64,
  1827. a_f32_reinterpret_i32,
  1828. a_f64_reinterpret_i64,
  1829. a_i32_extend8_s,
  1830. a_i32_extend16_s,
  1831. a_i64_extend8_s,
  1832. a_i64_extend16_s,
  1833. a_i64_extend32_s,
  1834. a_else,
  1835. a_end_block,
  1836. a_end_if,
  1837. a_end_loop,
  1838. a_end_try,
  1839. a_catch_all,
  1840. a_ref_is_null:
  1841. result:=1;
  1842. a_i32_trunc_sat_f32_s,
  1843. a_i32_trunc_sat_f32_u,
  1844. a_i32_trunc_sat_f64_s,
  1845. a_i32_trunc_sat_f64_u,
  1846. a_i64_trunc_sat_f32_s,
  1847. a_i64_trunc_sat_f32_u,
  1848. a_i64_trunc_sat_f64_s,
  1849. a_i64_trunc_sat_f64_u,
  1850. a_memory_size,
  1851. a_memory_grow,
  1852. a_ref_null_funcref,
  1853. a_ref_null_externref:
  1854. result:=2;
  1855. a_memory_copy:
  1856. result:=4;
  1857. a_memory_fill,
  1858. a_atomic_fence:
  1859. result:=3;
  1860. a_i32_const:
  1861. begin
  1862. if ops<>1 then
  1863. internalerror(2021092001);
  1864. with oper[0]^ do
  1865. case typ of
  1866. top_ref:
  1867. begin
  1868. if assigned(ref^.symbol) then
  1869. result:=6
  1870. else
  1871. begin
  1872. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  1873. internalerror(2021092018);
  1874. result:=1+SlebSize(longint(ref^.offset));
  1875. end;
  1876. end;
  1877. top_const:
  1878. result:=1+SlebSize(longint(val));
  1879. else
  1880. internalerror(2021092615);
  1881. end;
  1882. end;
  1883. a_i64_const:
  1884. begin
  1885. if ops<>1 then
  1886. internalerror(2021092001);
  1887. with oper[0]^ do
  1888. case typ of
  1889. top_ref:
  1890. begin
  1891. if assigned(ref^.symbol) then
  1892. result:=6
  1893. else
  1894. begin
  1895. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  1896. internalerror(2021092018);
  1897. result:=1+SlebSize(int64(ref^.offset));
  1898. end;
  1899. end;
  1900. top_const:
  1901. result:=1+SlebSize(int64(val));
  1902. else
  1903. internalerror(2021092615);
  1904. end;
  1905. end;
  1906. a_f32_const:
  1907. result:=5;
  1908. a_f64_const:
  1909. result:=9;
  1910. a_local_get,
  1911. a_local_set,
  1912. a_local_tee:
  1913. begin
  1914. if ops<>1 then
  1915. internalerror(2021092001);
  1916. with oper[0]^ do
  1917. case typ of
  1918. top_ref:
  1919. begin
  1920. if assigned(ref^.symbol) then
  1921. internalerror(2021092005);
  1922. if ref^.base<>NR_STACK_POINTER_REG then
  1923. internalerror(2021092006);
  1924. if ref^.index<>NR_NO then
  1925. internalerror(2021092007);
  1926. result:=1+UlebSize(ref^.offset);
  1927. end;
  1928. top_const:
  1929. result:=1+UlebSize(val);
  1930. else
  1931. internalerror(2021092008);
  1932. end;
  1933. end;
  1934. a_global_get,
  1935. a_global_set:
  1936. begin
  1937. if ops<>1 then
  1938. internalerror(2021092010);
  1939. with oper[0]^ do
  1940. case typ of
  1941. top_ref:
  1942. begin
  1943. if not assigned(ref^.symbol) then
  1944. internalerror(2021092012);
  1945. if (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  1946. internalerror(2021092013);
  1947. result:=6;
  1948. end;
  1949. else
  1950. internalerror(2021092011);
  1951. end;
  1952. end;
  1953. a_end_function:
  1954. result:=0;
  1955. a_block,
  1956. a_loop,
  1957. a_if,
  1958. a_try:
  1959. begin
  1960. if ops=0 then
  1961. result:=2
  1962. else
  1963. begin
  1964. if ops<>1 then
  1965. internalerror(2021092015);
  1966. with oper[0]^ do
  1967. case typ of
  1968. top_functype:
  1969. begin
  1970. if (length(functype.params)=0) and (length(functype.results)<=1) then
  1971. result:=2
  1972. else
  1973. { more complex blocktypes are not yet implemented }
  1974. internalerror(2021092621);
  1975. end;
  1976. else
  1977. internalerror(2021092620);
  1978. end;
  1979. end;
  1980. end;
  1981. a_i32_load,
  1982. a_i64_load,
  1983. a_f32_load,
  1984. a_f64_load,
  1985. a_i32_load8_s,
  1986. a_i32_load8_u,
  1987. a_i32_load16_s,
  1988. a_i32_load16_u,
  1989. a_i64_load8_s,
  1990. a_i64_load8_u,
  1991. a_i64_load16_s,
  1992. a_i64_load16_u,
  1993. a_i64_load32_s,
  1994. a_i64_load32_u,
  1995. a_i32_store,
  1996. a_i64_store,
  1997. a_f32_store,
  1998. a_f64_store,
  1999. a_i32_store8,
  2000. a_i32_store16,
  2001. a_i64_store8,
  2002. a_i64_store16,
  2003. a_i64_store32:
  2004. begin
  2005. if ops<>1 then
  2006. internalerror(2021092016);
  2007. with oper[0]^ do
  2008. case typ of
  2009. top_ref:
  2010. begin
  2011. if assigned(ref^.symbol) then
  2012. begin
  2013. Result:=1+
  2014. UlebSize(natural_alignment_for_load_store(opcode))+
  2015. 5; { relocation, fixed size = 5 bytes }
  2016. end
  2017. else
  2018. begin
  2019. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  2020. internalerror(2021092018);
  2021. Result:=1+
  2022. UlebSize(natural_alignment_for_load_store(opcode))+
  2023. UlebSize(ref^.offset);
  2024. end;
  2025. end;
  2026. top_const:
  2027. begin
  2028. Result:=1+
  2029. UlebSize(natural_alignment_for_load_store(opcode))+
  2030. UlebSize(val);
  2031. end;
  2032. else
  2033. internalerror(2021092017);
  2034. end;
  2035. end;
  2036. a_memory_atomic_notify,
  2037. a_memory_atomic_wait32,
  2038. a_memory_atomic_wait64,
  2039. a_i32_atomic_load,
  2040. a_i64_atomic_load,
  2041. a_i32_atomic_load8_u,
  2042. a_i32_atomic_load16_u,
  2043. a_i64_atomic_load8_u,
  2044. a_i64_atomic_load16_u,
  2045. a_i64_atomic_load32_u,
  2046. a_i32_atomic_store,
  2047. a_i64_atomic_store,
  2048. a_i32_atomic_store8,
  2049. a_i32_atomic_store16,
  2050. a_i64_atomic_store8,
  2051. a_i64_atomic_store16,
  2052. a_i64_atomic_store32,
  2053. a_i32_atomic_rmw_add,
  2054. a_i64_atomic_rmw_add,
  2055. a_i32_atomic_rmw8_add_u,
  2056. a_i32_atomic_rmw16_add_u,
  2057. a_i64_atomic_rmw8_add_u,
  2058. a_i64_atomic_rmw16_add_u,
  2059. a_i64_atomic_rmw32_add_u,
  2060. a_i32_atomic_rmw_sub,
  2061. a_i64_atomic_rmw_sub,
  2062. a_i32_atomic_rmw8_sub_u,
  2063. a_i32_atomic_rmw16_sub_u,
  2064. a_i64_atomic_rmw8_sub_u,
  2065. a_i64_atomic_rmw16_sub_u,
  2066. a_i64_atomic_rmw32_sub_u,
  2067. a_i32_atomic_rmw_and,
  2068. a_i64_atomic_rmw_and,
  2069. a_i32_atomic_rmw8_and_u,
  2070. a_i32_atomic_rmw16_and_u,
  2071. a_i64_atomic_rmw8_and_u,
  2072. a_i64_atomic_rmw16_and_u,
  2073. a_i64_atomic_rmw32_and_u,
  2074. a_i32_atomic_rmw_or,
  2075. a_i64_atomic_rmw_or,
  2076. a_i32_atomic_rmw8_or_u,
  2077. a_i32_atomic_rmw16_or_u,
  2078. a_i64_atomic_rmw8_or_u,
  2079. a_i64_atomic_rmw16_or_u,
  2080. a_i64_atomic_rmw32_or_u,
  2081. a_i32_atomic_rmw_xor,
  2082. a_i64_atomic_rmw_xor,
  2083. a_i32_atomic_rmw8_xor_u,
  2084. a_i32_atomic_rmw16_xor_u,
  2085. a_i64_atomic_rmw8_xor_u,
  2086. a_i64_atomic_rmw16_xor_u,
  2087. a_i64_atomic_rmw32_xor_u,
  2088. a_i32_atomic_rmw_xchg,
  2089. a_i64_atomic_rmw_xchg,
  2090. a_i32_atomic_rmw8_xchg_u,
  2091. a_i32_atomic_rmw16_xchg_u,
  2092. a_i64_atomic_rmw8_xchg_u,
  2093. a_i64_atomic_rmw16_xchg_u,
  2094. a_i64_atomic_rmw32_xchg_u,
  2095. a_i32_atomic_rmw_cmpxchg,
  2096. a_i64_atomic_rmw_cmpxchg,
  2097. a_i32_atomic_rmw8_cmpxchg_u,
  2098. a_i32_atomic_rmw16_cmpxchg_u,
  2099. a_i64_atomic_rmw8_cmpxchg_u,
  2100. a_i64_atomic_rmw16_cmpxchg_u,
  2101. a_i64_atomic_rmw32_cmpxchg_u:
  2102. begin
  2103. if ops<>1 then
  2104. internalerror(2021092016);
  2105. with oper[0]^ do
  2106. case typ of
  2107. top_ref:
  2108. begin
  2109. if assigned(ref^.symbol) then
  2110. begin
  2111. Result:=2+
  2112. UlebSize(natural_alignment_for_load_store(opcode))+
  2113. 5; { relocation, fixed size = 5 bytes }
  2114. end
  2115. else
  2116. begin
  2117. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  2118. internalerror(2021092018);
  2119. Result:=2+
  2120. UlebSize(natural_alignment_for_load_store(opcode))+
  2121. UlebSize(ref^.offset);
  2122. end;
  2123. end;
  2124. top_const:
  2125. begin
  2126. Result:=2+
  2127. UlebSize(natural_alignment_for_load_store(opcode))+
  2128. UlebSize(val);
  2129. end;
  2130. else
  2131. internalerror(2021092017);
  2132. end;
  2133. end;
  2134. a_call:
  2135. begin
  2136. if ops<>2 then
  2137. internalerror(2021092021);
  2138. with oper[0]^ do
  2139. case typ of
  2140. top_ref:
  2141. begin
  2142. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  2143. internalerror(2021092023);
  2144. result:=6;
  2145. end;
  2146. else
  2147. internalerror(2021092022);
  2148. end;
  2149. end;
  2150. a_call_indirect:
  2151. begin
  2152. if ops<>1 then
  2153. internalerror(2021092610);
  2154. with oper[0]^ do
  2155. case typ of
  2156. top_functype:
  2157. begin
  2158. TWasmObjData(objdata).FuncTypes.AddOrGetFuncType(functype);
  2159. result:=6+
  2160. UlebSize(0);
  2161. end;
  2162. else
  2163. internalerror(2021092611);
  2164. end;
  2165. end;
  2166. a_br,
  2167. a_br_if,
  2168. a_rethrow,
  2169. a_delegate:
  2170. begin
  2171. if ops<>1 then
  2172. internalerror(2021092610);
  2173. with oper[0]^ do
  2174. case typ of
  2175. top_const:
  2176. result:=1+
  2177. UlebSize(val);
  2178. else
  2179. internalerror(2021092625);
  2180. end;
  2181. end;
  2182. a_catch,
  2183. a_throw:
  2184. begin
  2185. if ops<>1 then
  2186. internalerror(2021092709);
  2187. with oper[0]^ do
  2188. case typ of
  2189. top_ref:
  2190. begin
  2191. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  2192. internalerror(2021092711);
  2193. result:=6;
  2194. end;
  2195. else
  2196. internalerror(2021092710);
  2197. end;
  2198. end;
  2199. a_memory_init:
  2200. begin
  2201. if ops<>1 then
  2202. internalerror(2022052802);
  2203. with oper[0]^ do
  2204. case typ of
  2205. top_const:
  2206. result:=3+UlebSize(val);
  2207. else
  2208. internalerror(2022052803);
  2209. end;
  2210. end;
  2211. a_data_drop:
  2212. begin
  2213. if ops<>1 then
  2214. internalerror(2022052804);
  2215. with oper[0]^ do
  2216. case typ of
  2217. top_const:
  2218. result:=2+UlebSize(val);
  2219. else
  2220. internalerror(2022052805);
  2221. end;
  2222. end;
  2223. else
  2224. internalerror(2021092623);
  2225. end;
  2226. end;
  2227. procedure taicpu.Pass2(objdata: TObjData);
  2228. procedure WriteByte(b: byte);
  2229. begin
  2230. objdata.writebytes(b,1);
  2231. end;
  2232. {$ifdef FPC_LITTLE_ENDIAN}
  2233. procedure WriteSingle(s: single);
  2234. begin
  2235. objdata.writebytes(s,4);
  2236. end;
  2237. procedure WriteDouble(d: double);
  2238. begin
  2239. objdata.writebytes(d,8);
  2240. end;
  2241. {$else FPC_LITTLE_ENDIAN}
  2242. procedure WriteSingle(s: single);
  2243. var
  2244. l: longword;
  2245. begin
  2246. Move(s,l,4);
  2247. l:=SwapEndian(l);
  2248. objdata.writebytes(l,4);
  2249. end;
  2250. procedure WriteDouble(d: double);
  2251. var
  2252. q: qword;
  2253. begin
  2254. Move(d,q,8);
  2255. q:=SwapEndian(q);
  2256. objdata.writebytes(q,8);
  2257. end;
  2258. {$endif FPC_LITTLE_ENDIAN}
  2259. procedure WriteSleb(v: tcgint);
  2260. var
  2261. b: byte;
  2262. Done: Boolean=false;
  2263. begin
  2264. repeat
  2265. b:=byte(v) and 127;
  2266. v:=SarInt64(v,7);
  2267. if ((v=0) and ((b and 64)=0)) or ((v=-1) and ((b and 64)<>0)) then
  2268. Done:=true
  2269. else
  2270. b:=b or 128;
  2271. objdata.writebytes(b,1);
  2272. until Done;
  2273. end;
  2274. procedure WriteUleb(v: tcgint);
  2275. var
  2276. b: byte;
  2277. begin
  2278. repeat
  2279. b:=byte(v) and 127;
  2280. v:=v shr 7;
  2281. if v<>0 then
  2282. b:=b or 128;
  2283. objdata.writebytes(b,1);
  2284. until v=0;
  2285. end;
  2286. begin
  2287. case opcode of
  2288. a_unreachable:
  2289. WriteByte($00);
  2290. a_nop:
  2291. WriteByte($01);
  2292. a_return:
  2293. WriteByte($0F);
  2294. a_drop:
  2295. WriteByte($1A);
  2296. a_memory_size:
  2297. begin
  2298. WriteByte($3F);
  2299. WriteByte($00);
  2300. end;
  2301. a_memory_grow:
  2302. begin
  2303. WriteByte($40);
  2304. WriteByte($00);
  2305. end;
  2306. a_memory_copy:
  2307. begin
  2308. WriteByte($FC);
  2309. WriteUleb(10);
  2310. WriteByte($00);
  2311. WriteByte($00);
  2312. end;
  2313. a_memory_fill:
  2314. begin
  2315. WriteByte($FC);
  2316. WriteUleb(11);
  2317. WriteByte($00);
  2318. end;
  2319. a_atomic_fence:
  2320. begin
  2321. WriteByte($FE);
  2322. WriteByte($03);
  2323. WriteByte($00);
  2324. end;
  2325. a_i32_eqz:
  2326. WriteByte($45);
  2327. a_i32_eq:
  2328. WriteByte($46);
  2329. a_i32_ne:
  2330. WriteByte($47);
  2331. a_i32_lt_s:
  2332. WriteByte($48);
  2333. a_i32_lt_u:
  2334. WriteByte($49);
  2335. a_i32_gt_s:
  2336. WriteByte($4A);
  2337. a_i32_gt_u:
  2338. WriteByte($4B);
  2339. a_i32_le_s:
  2340. WriteByte($4C);
  2341. a_i32_le_u:
  2342. WriteByte($4D);
  2343. a_i32_ge_s:
  2344. WriteByte($4E);
  2345. a_i32_ge_u:
  2346. WriteByte($4F);
  2347. a_i64_eqz:
  2348. WriteByte($50);
  2349. a_i64_eq:
  2350. WriteByte($51);
  2351. a_i64_ne:
  2352. WriteByte($52);
  2353. a_i64_lt_s:
  2354. WriteByte($53);
  2355. a_i64_lt_u:
  2356. WriteByte($54);
  2357. a_i64_gt_s:
  2358. WriteByte($55);
  2359. a_i64_gt_u:
  2360. WriteByte($56);
  2361. a_i64_le_s:
  2362. WriteByte($57);
  2363. a_i64_le_u:
  2364. WriteByte($58);
  2365. a_i64_ge_s:
  2366. WriteByte($59);
  2367. a_i64_ge_u:
  2368. WriteByte($5A);
  2369. a_f32_eq:
  2370. WriteByte($5B);
  2371. a_f32_ne:
  2372. WriteByte($5C);
  2373. a_f32_lt:
  2374. WriteByte($5D);
  2375. a_f32_gt:
  2376. WriteByte($5E);
  2377. a_f32_le:
  2378. WriteByte($5F);
  2379. a_f32_ge:
  2380. WriteByte($60);
  2381. a_f64_eq:
  2382. WriteByte($61);
  2383. a_f64_ne:
  2384. WriteByte($62);
  2385. a_f64_lt:
  2386. WriteByte($63);
  2387. a_f64_gt:
  2388. WriteByte($64);
  2389. a_f64_le:
  2390. WriteByte($65);
  2391. a_f64_ge:
  2392. WriteByte($66);
  2393. a_i32_clz:
  2394. WriteByte($67);
  2395. a_i32_ctz:
  2396. WriteByte($68);
  2397. a_i32_popcnt:
  2398. WriteByte($69);
  2399. a_i32_add:
  2400. WriteByte($6A);
  2401. a_i32_sub:
  2402. WriteByte($6B);
  2403. a_i32_mul:
  2404. WriteByte($6C);
  2405. a_i32_div_s:
  2406. WriteByte($6D);
  2407. a_i32_div_u:
  2408. WriteByte($6E);
  2409. a_i32_rem_s:
  2410. WriteByte($6F);
  2411. a_i32_rem_u:
  2412. WriteByte($70);
  2413. a_i32_and:
  2414. WriteByte($71);
  2415. a_i32_or:
  2416. WriteByte($72);
  2417. a_i32_xor:
  2418. WriteByte($73);
  2419. a_i32_shl:
  2420. WriteByte($74);
  2421. a_i32_shr_s:
  2422. WriteByte($75);
  2423. a_i32_shr_u:
  2424. WriteByte($76);
  2425. a_i32_rotl:
  2426. WriteByte($77);
  2427. a_i32_rotr:
  2428. WriteByte($78);
  2429. a_i64_clz:
  2430. WriteByte($79);
  2431. a_i64_ctz:
  2432. WriteByte($7A);
  2433. a_i64_popcnt:
  2434. WriteByte($7B);
  2435. a_i64_add:
  2436. WriteByte($7C);
  2437. a_i64_sub:
  2438. WriteByte($7D);
  2439. a_i64_mul:
  2440. WriteByte($7E);
  2441. a_i64_div_s:
  2442. WriteByte($7F);
  2443. a_i64_div_u:
  2444. WriteByte($80);
  2445. a_i64_rem_s:
  2446. WriteByte($81);
  2447. a_i64_rem_u:
  2448. WriteByte($82);
  2449. a_i64_and:
  2450. WriteByte($83);
  2451. a_i64_or:
  2452. WriteByte($84);
  2453. a_i64_xor:
  2454. WriteByte($85);
  2455. a_i64_shl:
  2456. WriteByte($86);
  2457. a_i64_shr_s:
  2458. WriteByte($87);
  2459. a_i64_shr_u:
  2460. WriteByte($88);
  2461. a_i64_rotl:
  2462. WriteByte($89);
  2463. a_i64_rotr:
  2464. WriteByte($8A);
  2465. a_f32_abs:
  2466. WriteByte($8B);
  2467. a_f32_neg:
  2468. WriteByte($8C);
  2469. a_f32_ceil:
  2470. WriteByte($8D);
  2471. a_f32_floor:
  2472. WriteByte($8E);
  2473. a_f32_trunc:
  2474. WriteByte($8F);
  2475. a_f32_nearest:
  2476. WriteByte($90);
  2477. a_f32_sqrt:
  2478. WriteByte($91);
  2479. a_f32_add:
  2480. WriteByte($92);
  2481. a_f32_sub:
  2482. WriteByte($93);
  2483. a_f32_mul:
  2484. WriteByte($94);
  2485. a_f32_div:
  2486. WriteByte($95);
  2487. a_f32_min:
  2488. WriteByte($96);
  2489. a_f32_max:
  2490. WriteByte($97);
  2491. a_f32_copysign:
  2492. WriteByte($98);
  2493. a_f64_abs:
  2494. WriteByte($99);
  2495. a_f64_neg:
  2496. WriteByte($9A);
  2497. a_f64_ceil:
  2498. WriteByte($9B);
  2499. a_f64_floor:
  2500. WriteByte($9C);
  2501. a_f64_trunc:
  2502. WriteByte($9D);
  2503. a_f64_nearest:
  2504. WriteByte($9E);
  2505. a_f64_sqrt:
  2506. WriteByte($9F);
  2507. a_f64_add:
  2508. WriteByte($A0);
  2509. a_f64_sub:
  2510. WriteByte($A1);
  2511. a_f64_mul:
  2512. WriteByte($A2);
  2513. a_f64_div:
  2514. WriteByte($A3);
  2515. a_f64_min:
  2516. WriteByte($A4);
  2517. a_f64_max:
  2518. WriteByte($A5);
  2519. a_f64_copysign:
  2520. WriteByte($A6);
  2521. a_i32_wrap_i64:
  2522. WriteByte($A7);
  2523. a_i32_trunc_f32_s:
  2524. WriteByte($A8);
  2525. a_i32_trunc_f32_u:
  2526. WriteByte($A9);
  2527. a_i32_trunc_f64_s:
  2528. WriteByte($AA);
  2529. a_i32_trunc_f64_u:
  2530. WriteByte($AB);
  2531. a_i64_extend_i32_s:
  2532. WriteByte($AC);
  2533. a_i64_extend_i32_u:
  2534. WriteByte($AD);
  2535. a_i64_trunc_f32_s:
  2536. WriteByte($AE);
  2537. a_i64_trunc_f32_u:
  2538. WriteByte($AF);
  2539. a_i64_trunc_f64_s:
  2540. WriteByte($B0);
  2541. a_i64_trunc_f64_u:
  2542. WriteByte($B1);
  2543. a_f32_convert_i32_s:
  2544. WriteByte($B2);
  2545. a_f32_convert_i32_u:
  2546. WriteByte($B3);
  2547. a_f32_convert_i64_s:
  2548. WriteByte($B4);
  2549. a_f32_convert_i64_u:
  2550. WriteByte($B5);
  2551. a_f32_demote_f64:
  2552. WriteByte($B6);
  2553. a_f64_convert_i32_s:
  2554. WriteByte($B7);
  2555. a_f64_convert_i32_u:
  2556. WriteByte($B8);
  2557. a_f64_convert_i64_s:
  2558. WriteByte($B9);
  2559. a_f64_convert_i64_u:
  2560. WriteByte($BA);
  2561. a_f64_promote_f32:
  2562. WriteByte($BB);
  2563. a_i32_reinterpret_f32:
  2564. WriteByte($BC);
  2565. a_i64_reinterpret_f64:
  2566. WriteByte($BD);
  2567. a_f32_reinterpret_i32:
  2568. WriteByte($BE);
  2569. a_f64_reinterpret_i64:
  2570. WriteByte($BF);
  2571. a_i32_extend8_s:
  2572. WriteByte($C0);
  2573. a_i32_extend16_s:
  2574. WriteByte($C1);
  2575. a_i64_extend8_s:
  2576. WriteByte($C2);
  2577. a_i64_extend16_s:
  2578. WriteByte($C3);
  2579. a_i64_extend32_s:
  2580. WriteByte($C4);
  2581. a_end_block,
  2582. a_end_if,
  2583. a_end_loop,
  2584. a_end_try:
  2585. WriteByte($0B);
  2586. a_catch_all:
  2587. WriteByte($19);
  2588. a_i32_const:
  2589. begin
  2590. WriteByte($41);
  2591. if ops<>1 then
  2592. internalerror(2021092001);
  2593. with oper[0]^ do
  2594. case typ of
  2595. top_ref:
  2596. begin
  2597. if assigned(ref^.symbol) then
  2598. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB)
  2599. else
  2600. begin
  2601. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  2602. internalerror(2021092018);
  2603. WriteSleb(longint(ref^.offset));
  2604. end;
  2605. end;
  2606. top_const:
  2607. WriteSleb(longint(val));
  2608. else
  2609. internalerror(2021092615);
  2610. end;
  2611. end;
  2612. a_i64_const:
  2613. begin
  2614. WriteByte($42);
  2615. if ops<>1 then
  2616. internalerror(2021092001);
  2617. with oper[0]^ do
  2618. case typ of
  2619. top_ref:
  2620. begin
  2621. if assigned(ref^.symbol) then
  2622. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_OR_TABLE_INDEX_SLEB)
  2623. else
  2624. begin
  2625. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  2626. internalerror(2021092018);
  2627. WriteSleb(int64(ref^.offset));
  2628. end;
  2629. end;
  2630. top_const:
  2631. WriteSleb(int64(val));
  2632. else
  2633. internalerror(2021092615);
  2634. end;
  2635. end;
  2636. a_f32_const:
  2637. begin
  2638. if ops<>1 then
  2639. internalerror(2021092619);
  2640. WriteByte($43);
  2641. with oper[0]^ do
  2642. case typ of
  2643. top_single:
  2644. WriteSingle(sval);
  2645. else
  2646. internalerror(2021092618);
  2647. end;
  2648. end;
  2649. a_f64_const:
  2650. begin
  2651. if ops<>1 then
  2652. internalerror(2021092616);
  2653. WriteByte($44);
  2654. with oper[0]^ do
  2655. case typ of
  2656. top_double:
  2657. WriteDouble(dval);
  2658. else
  2659. internalerror(2021092617);
  2660. end;
  2661. end;
  2662. a_local_get,
  2663. a_local_set,
  2664. a_local_tee:
  2665. begin
  2666. case opcode of
  2667. a_local_get:
  2668. WriteByte($20);
  2669. a_local_set:
  2670. WriteByte($21);
  2671. a_local_tee:
  2672. WriteByte($22);
  2673. else
  2674. internalerror(2021092003);
  2675. end;
  2676. if ops<>1 then
  2677. internalerror(2021092004);
  2678. with oper[0]^ do
  2679. case typ of
  2680. top_ref:
  2681. begin
  2682. if assigned(ref^.symbol) then
  2683. internalerror(2021092005);
  2684. if ref^.base<>NR_STACK_POINTER_REG then
  2685. internalerror(2021092006);
  2686. if ref^.index<>NR_NO then
  2687. internalerror(2021092007);
  2688. WriteUleb(ref^.offset);
  2689. end;
  2690. top_const:
  2691. WriteUleb(val);
  2692. else
  2693. internalerror(2021092008);
  2694. end;
  2695. end;
  2696. a_global_get,
  2697. a_global_set:
  2698. begin
  2699. case opcode of
  2700. a_global_get:
  2701. WriteByte($23);
  2702. a_global_set:
  2703. WriteByte($24);
  2704. else
  2705. internalerror(2021092009);
  2706. end;
  2707. if ops<>1 then
  2708. internalerror(2021092010);
  2709. with oper[0]^ do
  2710. case typ of
  2711. top_ref:
  2712. begin
  2713. if not assigned(ref^.symbol) then
  2714. internalerror(2021092012);
  2715. if (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  2716. internalerror(2021092013);
  2717. objdata.writeReloc(0,5,TWasmObjData(ObjData).globalref(ref^.symbol),RELOC_GLOBAL_INDEX_LEB);
  2718. end;
  2719. else
  2720. internalerror(2021092011);
  2721. end;
  2722. end;
  2723. a_end_function:
  2724. ;
  2725. a_block,
  2726. a_loop,
  2727. a_if,
  2728. a_try:
  2729. begin
  2730. case opcode of
  2731. a_block:
  2732. WriteByte($02);
  2733. a_loop:
  2734. WriteByte($03);
  2735. a_if:
  2736. WriteByte($04);
  2737. a_try:
  2738. WriteByte($06);
  2739. else
  2740. internalerror(2021092626);
  2741. end;
  2742. if ops=0 then
  2743. WriteByte($40)
  2744. else
  2745. begin
  2746. if ops<>1 then
  2747. internalerror(2021092015);
  2748. with oper[0]^ do
  2749. case typ of
  2750. top_functype:
  2751. begin
  2752. if (length(functype.params)=0) and (length(functype.results)<=1) then
  2753. begin
  2754. if length(functype.results)=1 then
  2755. WriteByte(encode_wasm_basic_type(functype.results[0]))
  2756. else
  2757. WriteByte($40);
  2758. end
  2759. else
  2760. { more complex blocktypes are not yet implemented }
  2761. internalerror(2021092621);
  2762. end;
  2763. else
  2764. internalerror(2021092620);
  2765. end;
  2766. end;
  2767. end;
  2768. a_else:
  2769. WriteByte($05);
  2770. a_i32_load,
  2771. a_i64_load,
  2772. a_f32_load,
  2773. a_f64_load,
  2774. a_i32_load8_s,
  2775. a_i32_load8_u,
  2776. a_i32_load16_s,
  2777. a_i32_load16_u,
  2778. a_i64_load8_s,
  2779. a_i64_load8_u,
  2780. a_i64_load16_s,
  2781. a_i64_load16_u,
  2782. a_i64_load32_s,
  2783. a_i64_load32_u,
  2784. a_i32_store,
  2785. a_i64_store,
  2786. a_f32_store,
  2787. a_f64_store,
  2788. a_i32_store8,
  2789. a_i32_store16,
  2790. a_i64_store8,
  2791. a_i64_store16,
  2792. a_i64_store32:
  2793. begin
  2794. case opcode of
  2795. a_i32_load:
  2796. WriteByte($28);
  2797. a_i64_load:
  2798. WriteByte($29);
  2799. a_f32_load:
  2800. WriteByte($2A);
  2801. a_f64_load:
  2802. WriteByte($2B);
  2803. a_i32_load8_s:
  2804. WriteByte($2C);
  2805. a_i32_load8_u:
  2806. WriteByte($2D);
  2807. a_i32_load16_s:
  2808. WriteByte($2E);
  2809. a_i32_load16_u:
  2810. WriteByte($2F);
  2811. a_i64_load8_s:
  2812. WriteByte($30);
  2813. a_i64_load8_u:
  2814. WriteByte($31);
  2815. a_i64_load16_s:
  2816. WriteByte($32);
  2817. a_i64_load16_u:
  2818. WriteByte($33);
  2819. a_i64_load32_s:
  2820. WriteByte($34);
  2821. a_i64_load32_u:
  2822. WriteByte($35);
  2823. a_i32_store:
  2824. WriteByte($36);
  2825. a_i64_store:
  2826. WriteByte($37);
  2827. a_f32_store:
  2828. WriteByte($38);
  2829. a_f64_store:
  2830. WriteByte($39);
  2831. a_i32_store8:
  2832. WriteByte($3A);
  2833. a_i32_store16:
  2834. WriteByte($3B);
  2835. a_i64_store8:
  2836. WriteByte($3C);
  2837. a_i64_store16:
  2838. WriteByte($3D);
  2839. a_i64_store32:
  2840. WriteByte($3E);
  2841. else
  2842. internalerror(2021092019);
  2843. end;
  2844. if ops<>1 then
  2845. internalerror(2021092016);
  2846. with oper[0]^ do
  2847. case typ of
  2848. top_ref:
  2849. begin
  2850. if assigned(ref^.symbol) then
  2851. begin
  2852. WriteUleb(natural_alignment_for_load_store(opcode));
  2853. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_LEB);
  2854. end
  2855. else
  2856. begin
  2857. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  2858. internalerror(2021092018);
  2859. WriteUleb(natural_alignment_for_load_store(opcode));
  2860. WriteUleb(ref^.offset);
  2861. end;
  2862. end;
  2863. top_const:
  2864. begin
  2865. WriteUleb(natural_alignment_for_load_store(opcode));
  2866. WriteUleb(val);
  2867. end;
  2868. else
  2869. internalerror(2021092017);
  2870. end;
  2871. end;
  2872. a_memory_atomic_notify,
  2873. a_memory_atomic_wait32,
  2874. a_memory_atomic_wait64,
  2875. a_i32_atomic_load,
  2876. a_i64_atomic_load,
  2877. a_i32_atomic_load8_u,
  2878. a_i32_atomic_load16_u,
  2879. a_i64_atomic_load8_u,
  2880. a_i64_atomic_load16_u,
  2881. a_i64_atomic_load32_u,
  2882. a_i32_atomic_store,
  2883. a_i64_atomic_store,
  2884. a_i32_atomic_store8,
  2885. a_i32_atomic_store16,
  2886. a_i64_atomic_store8,
  2887. a_i64_atomic_store16,
  2888. a_i64_atomic_store32,
  2889. a_i32_atomic_rmw_add,
  2890. a_i64_atomic_rmw_add,
  2891. a_i32_atomic_rmw8_add_u,
  2892. a_i32_atomic_rmw16_add_u,
  2893. a_i64_atomic_rmw8_add_u,
  2894. a_i64_atomic_rmw16_add_u,
  2895. a_i64_atomic_rmw32_add_u,
  2896. a_i32_atomic_rmw_sub,
  2897. a_i64_atomic_rmw_sub,
  2898. a_i32_atomic_rmw8_sub_u,
  2899. a_i32_atomic_rmw16_sub_u,
  2900. a_i64_atomic_rmw8_sub_u,
  2901. a_i64_atomic_rmw16_sub_u,
  2902. a_i64_atomic_rmw32_sub_u,
  2903. a_i32_atomic_rmw_and,
  2904. a_i64_atomic_rmw_and,
  2905. a_i32_atomic_rmw8_and_u,
  2906. a_i32_atomic_rmw16_and_u,
  2907. a_i64_atomic_rmw8_and_u,
  2908. a_i64_atomic_rmw16_and_u,
  2909. a_i64_atomic_rmw32_and_u,
  2910. a_i32_atomic_rmw_or,
  2911. a_i64_atomic_rmw_or,
  2912. a_i32_atomic_rmw8_or_u,
  2913. a_i32_atomic_rmw16_or_u,
  2914. a_i64_atomic_rmw8_or_u,
  2915. a_i64_atomic_rmw16_or_u,
  2916. a_i64_atomic_rmw32_or_u,
  2917. a_i32_atomic_rmw_xor,
  2918. a_i64_atomic_rmw_xor,
  2919. a_i32_atomic_rmw8_xor_u,
  2920. a_i32_atomic_rmw16_xor_u,
  2921. a_i64_atomic_rmw8_xor_u,
  2922. a_i64_atomic_rmw16_xor_u,
  2923. a_i64_atomic_rmw32_xor_u,
  2924. a_i32_atomic_rmw_xchg,
  2925. a_i64_atomic_rmw_xchg,
  2926. a_i32_atomic_rmw8_xchg_u,
  2927. a_i32_atomic_rmw16_xchg_u,
  2928. a_i64_atomic_rmw8_xchg_u,
  2929. a_i64_atomic_rmw16_xchg_u,
  2930. a_i64_atomic_rmw32_xchg_u,
  2931. a_i32_atomic_rmw_cmpxchg,
  2932. a_i64_atomic_rmw_cmpxchg,
  2933. a_i32_atomic_rmw8_cmpxchg_u,
  2934. a_i32_atomic_rmw16_cmpxchg_u,
  2935. a_i64_atomic_rmw8_cmpxchg_u,
  2936. a_i64_atomic_rmw16_cmpxchg_u,
  2937. a_i64_atomic_rmw32_cmpxchg_u:
  2938. begin
  2939. WriteByte($FE);
  2940. case opcode of
  2941. a_memory_atomic_notify:
  2942. WriteByte($00);
  2943. a_memory_atomic_wait32:
  2944. WriteByte($01);
  2945. a_memory_atomic_wait64:
  2946. WriteByte($02);
  2947. a_i32_atomic_load:
  2948. WriteByte($10);
  2949. a_i64_atomic_load:
  2950. WriteByte($11);
  2951. a_i32_atomic_load8_u:
  2952. WriteByte($12);
  2953. a_i32_atomic_load16_u:
  2954. WriteByte($13);
  2955. a_i64_atomic_load8_u:
  2956. WriteByte($14);
  2957. a_i64_atomic_load16_u:
  2958. WriteByte($15);
  2959. a_i64_atomic_load32_u:
  2960. WriteByte($16);
  2961. a_i32_atomic_store:
  2962. WriteByte($17);
  2963. a_i64_atomic_store:
  2964. WriteByte($18);
  2965. a_i32_atomic_store8:
  2966. WriteByte($19);
  2967. a_i32_atomic_store16:
  2968. WriteByte($1A);
  2969. a_i64_atomic_store8:
  2970. WriteByte($1B);
  2971. a_i64_atomic_store16:
  2972. WriteByte($1C);
  2973. a_i64_atomic_store32:
  2974. WriteByte($1D);
  2975. a_i32_atomic_rmw_add:
  2976. WriteByte($1E);
  2977. a_i64_atomic_rmw_add:
  2978. WriteByte($1F);
  2979. a_i32_atomic_rmw8_add_u:
  2980. WriteByte($20);
  2981. a_i32_atomic_rmw16_add_u:
  2982. WriteByte($21);
  2983. a_i64_atomic_rmw8_add_u:
  2984. WriteByte($22);
  2985. a_i64_atomic_rmw16_add_u:
  2986. WriteByte($23);
  2987. a_i64_atomic_rmw32_add_u:
  2988. WriteByte($24);
  2989. a_i32_atomic_rmw_sub:
  2990. WriteByte($25);
  2991. a_i64_atomic_rmw_sub:
  2992. WriteByte($26);
  2993. a_i32_atomic_rmw8_sub_u:
  2994. WriteByte($27);
  2995. a_i32_atomic_rmw16_sub_u:
  2996. WriteByte($28);
  2997. a_i64_atomic_rmw8_sub_u:
  2998. WriteByte($29);
  2999. a_i64_atomic_rmw16_sub_u:
  3000. WriteByte($2A);
  3001. a_i64_atomic_rmw32_sub_u:
  3002. WriteByte($2B);
  3003. a_i32_atomic_rmw_and:
  3004. WriteByte($2C);
  3005. a_i64_atomic_rmw_and:
  3006. WriteByte($2D);
  3007. a_i32_atomic_rmw8_and_u:
  3008. WriteByte($2E);
  3009. a_i32_atomic_rmw16_and_u:
  3010. WriteByte($2F);
  3011. a_i64_atomic_rmw8_and_u:
  3012. WriteByte($30);
  3013. a_i64_atomic_rmw16_and_u:
  3014. WriteByte($31);
  3015. a_i64_atomic_rmw32_and_u:
  3016. WriteByte($32);
  3017. a_i32_atomic_rmw_or:
  3018. WriteByte($33);
  3019. a_i64_atomic_rmw_or:
  3020. WriteByte($34);
  3021. a_i32_atomic_rmw8_or_u:
  3022. WriteByte($35);
  3023. a_i32_atomic_rmw16_or_u:
  3024. WriteByte($36);
  3025. a_i64_atomic_rmw8_or_u:
  3026. WriteByte($37);
  3027. a_i64_atomic_rmw16_or_u:
  3028. WriteByte($38);
  3029. a_i64_atomic_rmw32_or_u:
  3030. WriteByte($39);
  3031. a_i32_atomic_rmw_xor:
  3032. WriteByte($3A);
  3033. a_i64_atomic_rmw_xor:
  3034. WriteByte($3B);
  3035. a_i32_atomic_rmw8_xor_u:
  3036. WriteByte($3C);
  3037. a_i32_atomic_rmw16_xor_u:
  3038. WriteByte($3D);
  3039. a_i64_atomic_rmw8_xor_u:
  3040. WriteByte($3E);
  3041. a_i64_atomic_rmw16_xor_u:
  3042. WriteByte($3F);
  3043. a_i64_atomic_rmw32_xor_u:
  3044. WriteByte($40);
  3045. a_i32_atomic_rmw_xchg:
  3046. WriteByte($41);
  3047. a_i64_atomic_rmw_xchg:
  3048. WriteByte($42);
  3049. a_i32_atomic_rmw8_xchg_u:
  3050. WriteByte($43);
  3051. a_i32_atomic_rmw16_xchg_u:
  3052. WriteByte($44);
  3053. a_i64_atomic_rmw8_xchg_u:
  3054. WriteByte($45);
  3055. a_i64_atomic_rmw16_xchg_u:
  3056. WriteByte($46);
  3057. a_i64_atomic_rmw32_xchg_u:
  3058. WriteByte($47);
  3059. a_i32_atomic_rmw_cmpxchg:
  3060. WriteByte($48);
  3061. a_i64_atomic_rmw_cmpxchg:
  3062. WriteByte($49);
  3063. a_i32_atomic_rmw8_cmpxchg_u:
  3064. WriteByte($4A);
  3065. a_i32_atomic_rmw16_cmpxchg_u:
  3066. WriteByte($4B);
  3067. a_i64_atomic_rmw8_cmpxchg_u:
  3068. WriteByte($4C);
  3069. a_i64_atomic_rmw16_cmpxchg_u:
  3070. WriteByte($4D);
  3071. a_i64_atomic_rmw32_cmpxchg_u:
  3072. WriteByte($4E);
  3073. else
  3074. internalerror(2022052101);
  3075. end;
  3076. if ops<>1 then
  3077. internalerror(2021092016);
  3078. with oper[0]^ do
  3079. case typ of
  3080. top_ref:
  3081. begin
  3082. if assigned(ref^.symbol) then
  3083. begin
  3084. WriteUleb(natural_alignment_for_load_store(opcode));
  3085. objdata.writeReloc(ref^.offset,5,ObjData.symbolref(ref^.symbol),RELOC_MEMORY_ADDR_LEB);
  3086. end
  3087. else
  3088. begin
  3089. if assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) then
  3090. internalerror(2021092018);
  3091. WriteUleb(natural_alignment_for_load_store(opcode));
  3092. WriteUleb(ref^.offset);
  3093. end;
  3094. end;
  3095. top_const:
  3096. begin
  3097. WriteUleb(natural_alignment_for_load_store(opcode));
  3098. WriteUleb(val);
  3099. end;
  3100. else
  3101. internalerror(2021092017);
  3102. end;
  3103. end;
  3104. a_call:
  3105. begin
  3106. if ops<>2 then
  3107. internalerror(2021092021);
  3108. with oper[0]^ do
  3109. case typ of
  3110. top_ref:
  3111. begin
  3112. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  3113. internalerror(2021092023);
  3114. WriteByte($10);
  3115. objdata.writeReloc(0,5,ObjData.symbolref(ref^.symbol),RELOC_FUNCTION_INDEX_LEB);
  3116. end;
  3117. else
  3118. internalerror(2021092022);
  3119. end;
  3120. end;
  3121. a_call_indirect:
  3122. begin
  3123. if ops<>1 then
  3124. internalerror(2021092610);
  3125. with oper[0]^ do
  3126. case typ of
  3127. top_functype:
  3128. begin
  3129. WriteByte($11);
  3130. objdata.writeReloc(TWasmObjData(objdata).FuncTypes.AddOrGetFuncType(functype),5,nil,RELOC_TYPE_INDEX_LEB);
  3131. WriteUleb(0);
  3132. end;
  3133. else
  3134. internalerror(2021092611);
  3135. end;
  3136. end;
  3137. a_br,
  3138. a_br_if,
  3139. a_rethrow,
  3140. a_delegate:
  3141. begin
  3142. case opcode of
  3143. a_br:
  3144. WriteByte($0C);
  3145. a_br_if:
  3146. WriteByte($0D);
  3147. a_rethrow:
  3148. WriteByte($09);
  3149. a_delegate:
  3150. WriteByte($18);
  3151. else
  3152. internalerror(2021092622);
  3153. end;
  3154. if ops<>1 then
  3155. internalerror(2021092610);
  3156. with oper[0]^ do
  3157. case typ of
  3158. top_const:
  3159. WriteUleb(val);
  3160. else
  3161. internalerror(2021092625);
  3162. end;
  3163. end;
  3164. a_catch,
  3165. a_throw:
  3166. begin
  3167. case opcode of
  3168. a_catch:
  3169. WriteByte($07);
  3170. a_throw:
  3171. WriteByte($08);
  3172. else
  3173. internalerror(2021092708);
  3174. end;
  3175. if ops<>1 then
  3176. internalerror(2021092709);
  3177. with oper[0]^ do
  3178. case typ of
  3179. top_ref:
  3180. begin
  3181. if not assigned(ref^.symbol) or (ref^.base<>NR_NO) or (ref^.index<>NR_NO) or (ref^.offset<>0) then
  3182. internalerror(2021092711);
  3183. objdata.writeReloc(0,5,TWasmObjData(ObjData).ExceptionTagRef(ref^.symbol),RELOC_TAG_INDEX_LEB);
  3184. end;
  3185. else
  3186. internalerror(2021092710);
  3187. end;
  3188. end;
  3189. a_memory_init:
  3190. begin
  3191. WriteByte($FC);
  3192. WriteByte($08);
  3193. if ops<>1 then
  3194. internalerror(2022052806);
  3195. with oper[0]^ do
  3196. case typ of
  3197. top_const:
  3198. WriteUleb(val);
  3199. else
  3200. internalerror(2022052807);
  3201. end;
  3202. WriteByte($00);
  3203. end;
  3204. a_data_drop:
  3205. begin
  3206. WriteByte($FC);
  3207. WriteByte($09);
  3208. if ops<>1 then
  3209. internalerror(2022052808);
  3210. with oper[0]^ do
  3211. case typ of
  3212. top_const:
  3213. WriteUleb(val);
  3214. else
  3215. internalerror(2022052809);
  3216. end;
  3217. end;
  3218. a_i32_trunc_sat_f32_s:
  3219. begin
  3220. WriteByte($FC);
  3221. WriteByte($00);
  3222. end;
  3223. a_i32_trunc_sat_f32_u:
  3224. begin
  3225. WriteByte($FC);
  3226. WriteByte($01);
  3227. end;
  3228. a_i32_trunc_sat_f64_s:
  3229. begin
  3230. WriteByte($FC);
  3231. WriteByte($02);
  3232. end;
  3233. a_i32_trunc_sat_f64_u:
  3234. begin
  3235. WriteByte($FC);
  3236. WriteByte($03);
  3237. end;
  3238. a_i64_trunc_sat_f32_s:
  3239. begin
  3240. WriteByte($FC);
  3241. WriteByte($04);
  3242. end;
  3243. a_i64_trunc_sat_f32_u:
  3244. begin
  3245. WriteByte($FC);
  3246. WriteByte($05);
  3247. end;
  3248. a_i64_trunc_sat_f64_s:
  3249. begin
  3250. WriteByte($FC);
  3251. WriteByte($06);
  3252. end;
  3253. a_i64_trunc_sat_f64_u:
  3254. begin
  3255. WriteByte($FC);
  3256. WriteByte($07);
  3257. end;
  3258. a_ref_null_funcref:
  3259. begin
  3260. WriteByte($D0);
  3261. WriteByte($70);
  3262. end;
  3263. a_ref_null_externref:
  3264. begin
  3265. WriteByte($D0);
  3266. WriteByte($6F);
  3267. end;
  3268. a_ref_is_null:
  3269. WriteByte($D1);
  3270. else
  3271. internalerror(2021092624);
  3272. end;
  3273. end;
  3274. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  3275. begin
  3276. internalerror(2010122614);
  3277. result:=nil;
  3278. end;
  3279. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  3280. begin
  3281. internalerror(2010122615);
  3282. result:=nil;
  3283. end;
  3284. procedure InitAsm;
  3285. begin
  3286. end;
  3287. procedure DoneAsm;
  3288. begin
  3289. end;
  3290. function wasm_convert_first_item_to_structured(srclist: TAsmList): tai;
  3291. begin
  3292. result:=tai(srclist.First);
  3293. if result<>nil then
  3294. begin
  3295. srclist.Remove(result);
  3296. if result.typ=ait_instruction then
  3297. case taicpu(result).opcode of
  3298. a_if:
  3299. result:=tai_wasmstruc_if.create_from(taicpu(result),srclist);
  3300. a_block:
  3301. result:=tai_wasmstruc_block.create_from(taicpu(result),srclist);
  3302. a_loop:
  3303. result:=tai_wasmstruc_loop.create_from(taicpu(result),srclist);
  3304. a_try:
  3305. result:=tai_wasmstruc_try.create_from(srclist);
  3306. a_else,a_end_if,a_end_block,a_end_loop,a_end_try,a_catch,a_catch_all,a_delegate:
  3307. internalerror(2023100503);
  3308. else
  3309. ;
  3310. end;
  3311. end;
  3312. end;
  3313. procedure wasm_convert_to_structured_asmlist_internal(srclist, destlist: TAsmList);
  3314. begin
  3315. while not srclist.Empty do
  3316. destlist.Concat(wasm_convert_first_item_to_structured(srclist));
  3317. end;
  3318. procedure wasm_convert_to_structured_asmlist(var asmlist: TAsmList);
  3319. var
  3320. tmplist: TAsmList;
  3321. begin
  3322. tmplist:=TAsmList.Create;
  3323. wasm_convert_to_structured_asmlist_internal(asmlist,tmplist);
  3324. asmlist.Free;
  3325. asmlist:=tmplist;
  3326. end;
  3327. procedure wasm_convert_to_flat_asmlist_internal(srclist, destlist: TAsmList);
  3328. var
  3329. p: tai;
  3330. tmplist: TAsmList;
  3331. begin
  3332. tmplist:=TAsmList.Create;
  3333. while not srclist.Empty do
  3334. begin
  3335. p:=tai(srclist.First);
  3336. srclist.Remove(p);
  3337. if p.typ=ait_wasm_structured_instruction then
  3338. begin
  3339. taicpu_wasm_structured_instruction(p).ConvertToFlatList(tmplist);
  3340. srclist.insertList(tmplist);
  3341. end
  3342. else
  3343. destlist.Concat(p);
  3344. end;
  3345. tmplist.free;
  3346. end;
  3347. procedure wasm_convert_to_flat_asmlist(var asmlist: TAsmList);
  3348. var
  3349. tmplist: TAsmList;
  3350. begin
  3351. tmplist:=TAsmList.Create;
  3352. wasm_convert_to_flat_asmlist_internal(asmlist,tmplist);
  3353. asmlist.Free;
  3354. asmlist:=tmplist;
  3355. end;
  3356. procedure map_structured_asmlist_inner(l: TAsmList; f: TAsmMapFunc; blockstack: twasmstruc_stack);
  3357. var
  3358. p, q: tai;
  3359. mapres: TAsmMapFuncResult;
  3360. begin
  3361. if not assigned(l) then
  3362. exit;
  3363. p:=tai(l.First);
  3364. while p<>nil do
  3365. begin
  3366. if p.typ=ait_wasm_structured_instruction then
  3367. begin
  3368. mapres:=f(p,blockstack);
  3369. case mapres.typ of
  3370. amfrtNoChange:
  3371. begin
  3372. taicpu_wasm_structured_instruction(p).Map(f,blockstack);
  3373. p:=tai(p.next);
  3374. end;
  3375. amfrtNewAi:
  3376. begin
  3377. q:=mapres.newai;
  3378. if q<>p then
  3379. begin
  3380. l.InsertAfter(q,p);
  3381. l.Remove(p);
  3382. p:=q;
  3383. end;
  3384. p:=tai(p.next);
  3385. end;
  3386. amfrtNewList:
  3387. begin
  3388. q:=tai(mapres.newlist.First);
  3389. l.insertListAfter(p,mapres.newlist);
  3390. mapres.newlist.free;
  3391. l.Remove(p);
  3392. p:=q;
  3393. end;
  3394. amfrtDeleteAi:
  3395. begin
  3396. q:=p;
  3397. p:=tai(p.next);
  3398. l.Remove(q);
  3399. end;
  3400. end;
  3401. end
  3402. else
  3403. begin
  3404. mapres:=f(p,blockstack);
  3405. case mapres.typ of
  3406. amfrtNoChange:
  3407. p:=tai(p.next);
  3408. amfrtNewAi:
  3409. begin
  3410. q:=mapres.newai;
  3411. if q<>p then
  3412. begin
  3413. l.InsertAfter(q,p);
  3414. l.Remove(p);
  3415. p:=tai(q.next);
  3416. end
  3417. else
  3418. p:=tai(p.next);
  3419. end;
  3420. amfrtNewList:
  3421. begin
  3422. q:=tai(mapres.newlist.First);
  3423. l.insertListAfter(p,mapres.newlist);
  3424. mapres.newlist.free;
  3425. l.Remove(p);
  3426. p:=q;
  3427. end;
  3428. amfrtDeleteAi:
  3429. begin
  3430. q:=p;
  3431. p:=tai(p.next);
  3432. l.Remove(q);
  3433. end;
  3434. end;
  3435. end;
  3436. end;
  3437. end;
  3438. procedure map_structured_asmlist(l: TAsmList; f: TAsmMapFunc);
  3439. var
  3440. blockstack: twasmstruc_stack;
  3441. begin
  3442. blockstack:=twasmstruc_stack.create;
  3443. map_structured_asmlist_inner(l,f,blockstack);
  3444. blockstack.free;
  3445. end;
  3446. initialization
  3447. cai_cpu:=taicpu;
  3448. cai_align:=tai_align;
  3449. end.