aasmcpu.pas 88 KB

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