aasmcpu.pas 75 KB

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