aasmcpu.pas 124 KB

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