aasmcpu.pas 75 KB

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