aasmcpu.pas 74 KB

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