aasmcpu.pas 74 KB

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