aasmcpu.pas 121 KB

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