aasmcpu.pas 88 KB

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