aasmcpu.pas 116 KB

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