aasmcpu.pas 55 KB

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