aasmcpu.pas 114 KB

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