aasmcpu.pas 108 KB

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