aasmcpu.pas 106 KB

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