aasmcpu.pas 64 KB

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