aasmcpu.pas 74 KB

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