aasmcpu.pas 93 KB

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